簡體   English   中英

數據截斷:錯誤的日期時間值:org.joda.time.DateTime

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

通過Hibernate將DateTime傳遞給我的插入查詢時,出現SQL異常。

請在嘗試傳遞DateTime地方找到我的Java代碼:

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

我在sysout時的格式為DateTime2019-01-10T13:59:36.700+05:30

請找到我的插入查詢

請找到我的例外

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

您應在實體類中使用以下注釋,以保留joda DateTime:

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

還要確保您在類路徑中有jadira-usertype-core.jar:

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

為了幫助hibernate DateTime持久化到數據庫,您應該在項目中使用joda-time-hibernate而不是joda-time

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

修改您的實體:

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

請注意,版本1.4適用於Hibernate 3.6。 因此,當您使用更高級別joda-time-hibernate時,請更新joda-time-hibernate hibernate。

備擇方案:

pom.xml聲明一個額外的依賴關系,它可以支持joda-time的持久性。

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

修改實體:

@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