Hibernate映射MySQL DateTime [英] Hibernate mapping MySQL DateTime
问题描述
我在mysql数据库中有一个具有以下架构的表: [ID,值,日期],其中ID和值是int字段,日期是TIMESTAMP. 我定义了这样的Java类:
I've a table in a mysql db with the following schema: [ID, Value, Date] where ID and Value are int field and Date is a TIMESTAMP. I defined a Java class like this:
class myClass{
private int ID;
private int value;
private java.sql.Timestamp date;
}
如果我尝试使用为Hibernate定义的映射从db中读取记录,那么一切看起来都很好,无论如何,如果我创建了一个新对象并将其插入到db中,则会出错. 我读了很多关于此的文章,但我真的很困惑.
If I try to read records from the db using the map that I defined for Hibernate everything looks good, anyway if I create a new object and I try to insert it in my db I get errors. I read a lot of post about this but I'm really confused.
推荐答案
使用java.util.Date类而不是java.sql.Date. 然后它应该像这样:
Use the java.util.Date class instead of java.sql.Date. Then it should look like this :
public class myClass {
private int ID;
private int value;
@Temporal(TemporalType.TIMESTAMP)
private java.util.date date;
}
这篇关于Hibernate映射MySQL DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!