简体   繁体   English

数据截断:错误的日期时间值:org.joda.time.DateTime

[英]Data truncation: Incorrect datetime value: org.joda.time.DateTime

I am getting an SQL exception while am passing DateTime to my Insert query via Hibernate. 通过Hibernate将DateTime传递给我的插入查询时,出现SQL异常。

Please find my Java code where am trying to pass DateTime : 请在尝试传递DateTime地方找到我的Java代码:

claimGroupingHistory.setCreatedAt(new DateTime());
claimGroupMappingRepository.insertClaimGroupingHistory(claimGroupingHistory.getDealerCode(),
        claimGroupingHistory.getCreatedAt(),
        claimGroupingHistory.getCreatedBy());

I am getting DateTime in the format when I sysout: 2019-01-10T13:59:36.700+05:30 . 我在sysout时的格式为DateTime2019-01-10T13:59:36.700+05:30

Please find my insert query 请找到我的插入查询

Please find my exception am getting 请找到我的例外

2019-01-10 13:59:36,754 [http-9292-1] ERROR   org.hibernate.engine.jdbc.spi.SqlExceptionHelper: 146 - Data truncation: Incorrect datetime value: '\xAC\xED\x00\x05sr\x00\x16org.joda.time.DateTime\xB8<xdj[\xDD\xF9\x02\x00\x00xr\x00\x1Forg.joda.time.base.BaseDateTime\xFF\xFF\x' for column 'created_at' at row 1
2019-01-10 13:59:36,774 [http-9292-1] ERROR         com.cat.pscs.api.controller.BaseController:  57 - Data Integrity Violation Exception : org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.DataException: could not execute statement

You should use the following annotation in your entity class in order to persist joda DateTime: 您应在实体类中使用以下注释,以保留joda DateTime:

@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
private DateTime createdAt;

Also make sure that your have jadira-usertype-core.jar in your classpath: 还要确保您在类路径中有jadira-usertype-core.jar:

<dependency>
    <groupId>org.jadira.usertype</groupId>
    <artifactId>usertype.core</artifactId>
    <version>3.1.0.CR1</version>
</dependency>

To help hibernate persist DateTime to a database, you should use joda-time-hibernate rather than joda-time in the project. 为了帮助hibernate DateTime持久化到数据库,您应该在项目中使用joda-time-hibernate而不是joda-time

<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time-hibernate</artifactId>
    <version>1.4</version>
</dependency>

Modify your entity: 修改您的实体:

@Column
@Type(type="org.joda.time.contrib.hibernate.PersistentDateTime")
private DateTime createdAt;

Note that version 1.4 is for Hibernate 3.6. 请注意,版本1.4适用于Hibernate 3.6。 So update joda-time-hibernate when you use a higher level hibernate. 因此,当您使用更高级别joda-time-hibernate时,请更新joda-time-hibernate hibernate。

Alternatives: 备择方案:

Declare an extra dependency in the pom.xml , it can support the persistence for joda-time . pom.xml声明一个额外的依赖关系,它可以支持joda-time的持久性。

<dependency>
    <groupId>org.jadira.usertype</groupId>
    <artifactId>usertype.extended</artifactId>
    <version>5.0.0.GA</version>
</dependency>

Modify the entity: 修改实体:

@Column
@Type(type="org.jadira.usertype.dateandtime.joda.PersistentLocalDate")
private DateTime createdAt;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM