java 休眠日期不保存毫秒
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6200972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate date don't save the milliseconds
提问by rascio
I'm saving an entity in hibernate with a creation date
我正在用创建日期在休眠状态下保存一个实体
@Id @Column(name = "dtcreation") @Type(type="timestamp") private Date creation;我在这个字段上放了一个新的日期:
entity.setCreation(new Date()); entityDao.persist(entity);但是当它保存在数据库上时,时间不包含毫秒,但是如果我尝试使用查询更新它工作的毫秒值,则将其设为 0...有人可以帮助我吗?
after the persist method i have a record with 01/06/2011 15:00:00.0 but if i made an UPDATE i can change the milliseconds, so the db supports it.. the database is informix
在persist方法之后,我有一个记录为01/06/2011 15:00:00.0,但如果我进行了更新,我可以更改毫秒,因此数据库支持它..数据库是informix
回答by Stevi Deter
This is consistent with the documented behavior of java.util.Dateand java.sql.Timestamp
这与java.util.Date和java.sql.Timestamp的记录行为一致
java.util.Date stores down to the second, and java.sql.Timestamp is a thin wrapper to accommodate the nanosecond value of a SQL timestamp value. If you read the note on the Timestamp javadoc, it clearly states this difference.
java.util.Date 存储到秒,而 java.sql.Timestamp 是一个瘦包装器,用于容纳 SQL 时间戳值的纳秒值。如果您阅读 Timestamp javadoc 上的注释,它会清楚地说明这种差异。
If you don't want to lose your second fractions, and don't want to investigate alternative date libraries (e.g. the aforementioned Joda Time) you'll need to make the field a java.sql.Timestamp, and use the milisecond value of the current date to construct the initial value
如果您不想丢失第二个分数,并且不想调查替代日期库(例如前面提到的 Joda Time),您需要将该字段设为 java.sql.Timestamp,并使用构造初始值的当前日期
java.util.Date date = new java.util.Date();
java.sql.Timestamp timestamp = new java.sql.Timestamp(date.getTime());
entity.setCreation(timestamp);
回答by Chad Wilson
Without your code to test it's difficult for me to definitively answer, but in taking a look at the Javadoc for java.sql.Timestamp, it appears that you're mixing types with annotating the field as a timestamp. Change the java type from Date to Timestamp and see if that solves your problem.
如果没有您的代码进行测试,我很难明确回答,但是在查看java.sql.Timestamp的 Javadoc 时,您似乎将类型与将字段注释为时间戳的方法混合在一起。将 java 类型从 Date 更改为 Timestamp ,看看是否能解决您的问题。