通过Hibernate保持Joda时间的DateTime [英] Persist Joda-time's DateTime via Hibernate

查看:470
本文介绍了通过Hibernate保持Joda时间的DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Play应用中使用Jodatime,但目前必须做一堆来回/转换为 java.util.Date 和<$ c

由于jodatime包含在Play发行版中,我认为可能有更好的方法做这个。有什么办法可以让我的模型字段 DateTime s而不是 java.util.Date java.sql.Time 所以转换是自动完成的?是否有另一种简化这种方法?

解决方案

对于Hibernate 3,将以下注释添加到日期字段中:

  @Type(type =org.joda.time.contrib.hibernate.PersistentDateTime)

Hibernate现在会为你做一些肮脏的工作。



(确保你有 joda-time-hibernate.jar 在你的classpath中)



更新:



对于Hibernate 4和5,添加以下注释:

  @Type(type =org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime)

(确保你有 classpath中的jadira-usertype-core.jar)


I'm using Jodatime in my Play app, but currently having to do a bunch of converting back and forth from/to java.util.Date and java.sql.Time.

Since jodatime is included in the Play distribution, I'm thinking there's probably a better way to do this. Is there any way I can make my Model fields DateTimes instead of java.util.Date and java.sql.Time so the conversion is done automatically? Is there another way of streamlining this?

解决方案

For Hibernate 3 add the following annotation to your date field:

@Type(type="org.joda.time.contrib.hibernate.PersistentDateTime")

Hibernate will now do the dirty work for you.

(Make sure you have joda-time-hibernate.jar in your classpath)

UPDATE:

For Hibernate 4 and 5 add the following annotation:

@Type(type="org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime")

(Make sure you have jadira-usertype-core.jar in your classpath)

这篇关于通过Hibernate保持Joda时间的DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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