简体   繁体   English

使用Hibernate将java.util.Date持久化到MySql中的问题

[英]Problem persisting a java.util.Date into MySql using Hibernate

I've been debugging this problem for the last couple of hours with no success and figured I'd throw it out to SO and see where that goes. 我在过去的几个小时里一直在调试这个问题而没有成功,并且认为我会把它扔到SO那里看看它到底发生了什么。

I'm developing a Java program that persists data into a MySql database using Hibernate and the DAO/DTO pattern. 我正在开发一个Java程序,它使用Hibernate和DAO / DTO模式将数据保存到MySql数据库中。 In my database, I have a memberprofile table with a firstLoginDate column. 在我的数据库中,我有一个带有firstLoginDate列的memberprofile表。 In the database, the SQL type of that column is a DateTime. 在数据库中,该列的SQL类型是DateTime。 The corresponding section of the Hibernate XML file is Hibernate XML文件的相应部分是

<property name="firstLoginDate" type="timestamp">
    <column name="firstLoginDate" sql-type="DATETIME"/>
</property>

However, when I try to save a Date into that table (in Java), the "date" (year/month/day) part is persisted correctly, but the "time of day" part (hours:minutes:seconds) is not. 但是,当我尝试将Date保存到该表(在Java中)时,“日期”(年/月/日)部分正确保留,但“时间”部分(小时:分钟:秒)不是。 For instance, if I try to save a Java date representing 2009-09-01 14:02:23 , what ends up in the database is instead 2009-09-01 00:00:00 . 例如,如果我尝试保存代表2009-09-01 14:02:23的Java日期,那么数据库中的最终结果是2009-09-01 00:00:00

I've already confirmed that my own code isn't stomping on the time component; 我已经确认我自己的代码没有踩到时间组件; as far as I can see source code (while debugging) the time component remains correct. 据我所知,源代码(调试时)时间组件保持正确。 However, after committing changes, I can examine the relevant row using the MySql Query Browser (or just grabbing back out from the database in my Java code), and indeed the time component is missing. 但是,在提交更改之后,我可以使用MySql查询浏览器检查相关行(或者只是从我的Java代码中的数据库中取回),实际上时间组件丢失了。 Any ideas? 有任何想法吗?

I did try persisting a java.sql.Timestamp instead of a java.util.Date , but the problem remained. 我确实尝试过持久化java.sql.Timestamp而不是java.util.Date ,但问题仍然存在。 Also, I have a very similar column in another table that does not exhibit this behavior at all. 另外,我在另一个表中有一个非常相似的列,根本没有表现出这种行为。

I expect you guys will have questions, so I'll edit this as needed. 我希望你们有问题,所以我会根据需要编辑。 Thanks! 谢谢!


Edit @Nate: 编辑@Nate:

...
MemberProfile mp = ...
Date now = new Date();
mp.setFirstLoginDate(now);
...

MemberProfile is pretty much a wrapper class for the DTO; MemberProfile几乎是DTO的包装类; setting the first login date sets a field of the DTO and then commits the changes. 设置第一个登录日期会设置DTO的字段,然后提交更改。


Edit 2: It seems to only occur on my machine. 编辑2:它似乎只发生在我的机器上。 I've already tried rebuilding the table schema and wiping out all of my local source and re-checking-out from CVS, with no improvement. 我已经尝试重建表模式并清除所有本地源并从CVS重新检出,没有任何改进。 Now I'm really stumped. 现在我真的很难过。


Edit 3: Completely wiping my MySql installation, reinstalling it, and restoring the database from a known good copy also did not fix the problem. 编辑3:完全擦除我的MySql安装,重新安装它,并从已知的良好副本还原数据库也无法解决问题。

I have a similar setup to you (except mine works), and my mapping file looks like this: 我有类似的设置(除了我的工作),我的映射文件如下所示:

<property name="firstLoginDate" type="timestamp">
    <column name="firstLoginDate" length="19"/>
</property>

My database shows the column definition as datetime. 我的数据库将列定义显示为datetime。

Edit: 编辑:

Some more things to check... 还有一些事要检查......

  • Check that the mysql driver the same on your local as on the working machines. 检查本地和工作机器上的mysql驱动程序是否相同。
  • Try dropping the table, and have hibernate recreate it for you. 尝试删除表,然后让hibernate为您重新创建它。 If that works, then there's a problem in the mapping. 如果可行,则映射中存在问题。

This may or may not be your problem, but we have had serious problems with date/time info - if your database server is on a different time zone than the machine submitting the data, you can have inconsistencies in the data saved. 这可能是您的问题,也可能不是,但我们在日期/时间信息方面遇到了严重问题 - 如果您的数据库服务器与提交数据的机器位于不同的时区,则可能会导致数据保存不一致。

Beyond that, with our annotation configuration, it looks something like the following: 除此之外,使用我们的注释配置,它看起来如下所示:

@Column(name="COLUMN_NAME", length=11)

If it is viable for you, consider using the JodaTime DateTime class which is much nicer than the built in classes and you can also persist them using Hibernate with their Hibernate Support 如果它适合你,可以考虑使用比内置类好得多的JodaTime DateTime类,你也可以使用Hibernate支持Hibernate支持它们

Using them I mark my fields or getters with the annotation for custom Hibernate Types as: 使用它们我用自定义Hibernate类型的注释标记我的字段或getter:

@org.hibernate.annotations.Type(type = "org.joda.time.contrib.hibernate.PersistentDateTime") @Column(name = "date") @ org.hibernate.annotations.Type(type =“org.joda.time.contrib.hibernate.PersistentDateTime”)@Column(name =“date”)

This works fine for me and it also generates correct schema generation sql 这对我来说很好,它也生成正确的模式生成sql

This works fine in MySQL 这在MySQL中运行良好

Use TemporalType.TIMESTAMP beside your Temporal annonation. 在Temporal annonation旁边使用TemporalType.TIMESTAMP。

Please check the example below. 请查看以下示例。

@Temporal(TemporalType.TIMESTAMP)
public Date getCreated() {
    return this.created;
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM