使用Hibernate将java.util.Date持久化到MySql中 [英] Problem persisting a java.util.Date into MySql using Hibernate

查看:93
本文介绍了使用Hibernate将java.util.Date持久化到MySql中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在过去几个小时内一直在调试这个问题,没有成功,并且认为我会把它扔出去,看看那里。

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.

我正在开发一个使用Hibernate和DAO / DTO模式将数据保存到MySql数据库中的Java程序。在我的数据库中,我有一个 memberprofile 表与一个 firstLoginDate 列。在数据库中,该列的SQL类型是DateTime。 Hibernate XML文件的相应部分是

I'm developing a Java program that persists data into a MySql database using Hibernate and the DAO/DTO pattern. In my database, I have a memberprofile table with a firstLoginDate column. In the database, the SQL type of that column is a DateTime. The corresponding section of the Hibernate XML file is

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

但是,当我尝试保存 Date (在Java中),日期(年/月/日)部分保持正确,但时间部分(小时:分钟:秒)不是。例如,如果我尝试保存代表 2009-09-01 14:02:23 的Java日期,那么数据库中的最终结果是 2009-09-01 00:00:00

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. 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.

我已经确认我自己的代码没有踩踏时间组件;只要我可以看到源代码(调试时)时间组件保持正确。但是,提交更改后,我可以使用MySql查询浏览器检查相关行(或者只是从我的Java代码中的数据库中取出),实际上缺少时间组件。任何想法?

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. Any ideas?

我确实尝试坚持一个 java.sql.Timestamp 而不是一个 java.util.Date ,但问题依然存在。此外,我在另一张表中有一个非常相似的列,完全没有表现出这种行为。

I did try persisting a java.sql.Timestamp instead of a java.util.Date, but the problem remained. 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!

编辑@Nate:

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

MemberProfile 几乎是DTO的包装类;设置第一个登录日期设置DTO的字段,然后提交更改。

MemberProfile is pretty much a wrapper class for the DTO; setting the first login date sets a field of the DTO and then commits the changes.

编辑2: / strong>似乎只发生在我的机器上。我已经尝试重建表格,并清除我所有的本地资源,并从CVS重新检出,没有任何改进。现在我真的很累。

Edit 2: It seems to only occur on my machine. 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. Now I'm really stumped.

编辑3:完全擦除MySql安装,重新安装它和从已知的好的副本恢复数据库也没有解决问题。

Edit 3: Completely wiping my MySql installation, reinstalling it, and restoring the database from a known good copy also did not fix the problem.

推荐答案

我有一个类似的设置(我的工程除外),我的映射文件如下所示:

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>

我的数据库将列定义为datetime。

My database shows the column definition as datetime.

编辑:

还有一些要检查的东西...

Some more things to check...


  • 检查mysql驱动程序与本地工作机器上的
    一样。

  • 尝试删除该表,并且
    hibernate为您重新创建。如果
    有效,那么映射中
    有一个问题。

这篇关于使用Hibernate将java.util.Date持久化到MySql中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆