坚持约达时间';通过Hibernate设置日期时间

坚持约达时间';通过Hibernate设置日期时间,hibernate,jodatime,Hibernate,Jodatime,我在我的Play应用程序中使用了Jodatime,但目前必须进行一系列从/到java.util.Date和java.sql.Time的来回转换 由于jodatime包含在Play发行版中,我认为可能有更好的方法来实现这一点。有没有办法让我的模型字段DateTimes而不是java.util.Date和java.sql.Time自动转换?是否有其他方法可以简化此过程?对于Hibernate 3,请在日期字段中添加以下注释: @Type(type="org.joda.time.contrib.hib

我在我的Play应用程序中使用了Jodatime,但目前必须进行一系列从/到
java.util.Date
java.sql.Time
的来回转换


由于jodatime包含在Play发行版中,我认为可能有更好的方法来实现这一点。有没有办法让我的模型字段
DateTime
s而不是
java.util.Date
java.sql.Time
自动转换?是否有其他方法可以简化此过程?

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

@Type(type="org.joda.time.contrib.hibernate.PersistentDateTime")
Hibernate现在将为您完成这些肮脏的工作

(确保您的类路径中有)

更新:

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

@Type(type="org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime")
(确保您的类路径中有)

  • Joda建议在Hibernate4.0中使用用户类型库,Hibernate4.0是与Play1.2.x捆绑的Hibernate版本(请参阅:)

  • 处理依赖关系的正确方法是使用
    dependencies.yml
    文件,包括这样一行:

    - org.jadira.usertype -> usertype.jodatime 2.0.1
    

  • 我们应该在Hibernate 4项目中启用两个JAR:

    • 编译(“joda时间:joda时间:2.8”)
    • 编译(“org.jadira.usertype:usertype.jodatime:2.0.1”)

    您可以在jpa属性中添加注释,而不是在每个Joda属性中添加
    @Type
    注释

    #Hibernate config
    jadira.usertype.autoRegisterUserTypes=true
    
    它应该可以正常工作


    ps.应该在您的类路径中。

    我添加了它并导入了
    org.hibernate.annotations.Type
    ,但得到了“无法确定类型:org.joda.time.contrib.hibernate.PersistentLocalDate”。还有什么我需要安排的吗?或者这可能是通过JPA仅使用Hibernate而不是直接使用Hibernate的问题?您应该在类路径中包含joda time Hibernate jar。在
    framework/lib
    中删除jar,重新启动,并能够使其正常工作。他们的网站一开始确实欺骗了我,大的“获取最新版本”链接是针对joda time的,而不是joda-time-hibernate。有什么理由不使用
    org.joda.time.contrib.hibernate.PersistentDateTime
    ,因为这是要求的数据类型吗?如果使用此选项,也可以让字段使用fixture,因为有一个用于
    DateTime
    的活页夹,而不是
    LocalDate
    @KeesdeKooter,这个问题需要JPA解决方案。您的答案使用@Type(…)。我猜你指的是org.hibernate.annotations.Type。使用此注释会创建对Hibernate的依赖关系。您知道使用更通用的JPA而不是Hibernate的解决方案吗?我建议您就此提出一个新问题。此解决方案减少了对Hibernate特定注释的依赖。美好的