简体   繁体   English

Hibernate + Mysql:数据截断:日期时间值不正确

[英]Hibernate + Mysql : Data truncation: Incorrect datetime value

All,全部,

I am using Spring, Hibernate and MySQL to persist some records to a queue which I process later.我正在使用 Spring、Hibernate 和 MySQL 将一些记录保存到我稍后处理的队列中。

Since I am using Java 8, the recommendation is to use JodaTime library rather than Java SQL Date cf here由于我使用的是 Java 8,因此建议使用 JodaTime 库而不是 Java SQL Date cf here

Model模型

    @Entity
@Table(name = "order_job_queue")
public class CancelReward implements Serializable {
    private static final long serialVersionUID = 1L;


    @Id
    @GeneratedValue
    @Column(name = "id")
    private int id;

    @Column(name = "processed_state")
    private String processedState;

    @Column(name = "order_reference_id")
    private int orderReferenceId;

    @Column(name = "creation_date", columnDefinition = "DATETIME DEFAULT")
    private DateTime creationDate;

    @Column(name = "last_checked_date", columnDefinition = "DATETIME DEFAULT")
    private DateTime lastCheckedDate; ...

@PrePersist
protected void onCreate() {
    creationDate = new DateTime().toDateTime();
}

@PreUpdate
protected void onUpdate() {
    lastCheckedDate = new DateTime().toDateTime();
}

Table桌子

`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`processed_state` VARCHAR(45) NOT NULL,
`order_reference_id` INT UNSIGNED NOT NULL,
`creation_date` DATETIME NOT NULL,
`last_checked_date` DATETIME NOT NULL,

Dependency Versions依赖版本

  • Spring Framework - 4.3.3 Spring 框架 - 4.3.3
  • Hibernate - 4.3.5休眠 - 4.3.5
  • Hibernate JPA2 API - 1休眠 JPA2 API - 1
  • MySQL Connector - 5.1.40 MySQL 连接器 - 5.1.40

When I try to save an entity I get this stack trace complaining about truncation.当我尝试保存一个实体时,我得到这个堆栈跟踪抱怨截断。

Data truncation: Incorrect datetime value: ' ' for column 'creation_date' at row 1 Error while processing request: could not execute statement;数据截断:日期时间值不正确:第 1 行“creation_date”列的“ ”处理请求时出错:无法执行语句; SQL [n/a]; SQL [不适用]; nested exception is org.hibernate.exception.DataException: could not execute statement嵌套异常是 org.hibernate.exception.DataException: 无法执行语句

I've tried using Java SQL Date as well, not sure how to fix this one.a我也尝试过使用 Java SQL Date,不知道如何解决这个问题。a

Try using the standard date in java:尝试在 Java 中使用标准日期:

import java.util.Date;

java.sql.Timestamp should also work but I never tried it myself. java.sql.Timestamp也应该可以工作,但我自己从未尝试过。

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

相关问题 休眠-错误:数据截断:错误的日期时间值 - Hibernate - ERROR: Data truncation: Incorrect datetime value 数据截断:错误的日期时间值 - Data truncation: Incorrect datetime value 数据截断:错误的日期时间值:“ - Data truncation: Incorrect datetime value: '' 数据截断:错误的日期时间值:“ - Data truncation: Incorrect datetime value: ' com.mysql.jdbc.MysqlDataTruncation:数据截断:不正确的日期时间值:'' - com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value: ' ' 数据截断:日期时间值不正确:Java中的“null” - Data truncation: Incorrect datetime value: 'null' in Java 数据截断:错误的日期时间值:org.joda.time.DateTime - Data truncation: Incorrect datetime value: org.joda.time.DateTime 为什么会出现“ com.mysql.jdbc.MysqlDataTruncation:数据截断:不正确的datetime值:”错误? - Why do I get a “com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value:” error? 数据截断:错误的日期时间值:第1行的“日期”列为“ - Data truncation: Incorrect datetime value: '' for column 'date' at row 1 com.mysql.jdbc.MysqlDataTruncation:数据截断:错误的日期时间值:第1行的'lastchange'列的'0000-00-00 00:00:00' - com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value: '0000-00-00 00:00:00' for column 'lastchange' at row 1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM