全部,
我正在使用 Spring、Hibernate 和 MySQL 将一些记录保存到我稍后处理的队列中。
由于我使用的是 Java 8,建议使用 JodaTime 库而不是 Java SQL Date c.f. here
型号
@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();
}
表格
`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,
依赖版本
- Spring 框架 - 4.3.3
- hibernate - 4.3.5
- hibernate JPA2 API - 1
- MySQL 连接器 - 5.1.40
当我尝试保存一个实体时,我收到此堆栈跟踪,提示截断。
Data truncation: Incorrect datetime value: '��' for column 'creation_date' at row 1 Error while processing request: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.DataException: could not execute statement
我也尝试过使用 Java SQL Date,但不确定如何解决这个问题。a
最佳答案
尝试在 java 中使用标准日期:
import java.util.Date;
java.sql.Timestamp
应该也可以,但我自己从未尝试过。
关于java - hibernate + Mysql : Data truncation: Incorrect datetime value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39975069/