java pojo 中的 joda.time.DateTime 支持休眠
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5065367/
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
joda.time.DateTime in pojo with hibernate support
提问by Ravi Parekh
hibernate - 3.6.0.Final joda - 1.4 how to support direct joda.DateTime in pojo for hibernate
hibernate - 3.6.0.Final joda - 1.4 如何在 pojo 中支持直接 joda.DateTime 进行休眠
so can be session.saveOrUpdate(rateCodeId);
所以可以 session.saveOrUpdate(rateCodeId);
pojo
波乔
public class RateCodeId implements java.io.Serializable {
private int roomId;
private org.joda.Time.DateTime date;
}
hbm.xml
hbm.xml
<key-property name="date" type="org.joda.time.contrib.hibernate.PersistentDateTime">
<column length="10" name="date" />
</key-property>
ERROR:
错误:
org.hibernate.MappingException: Could not determine type for: org.joda.time.contrib.hibernate.PersistentDateTime
org.hibernate.MappingException:无法确定类型:org.joda.time.contrib.hibernate.PersistentDateTime
AND without "type=" ERROR
并且没有“类型=”错误
java.lang.ClassCastException: org.joda.time.DateTime cannot be cast to java.util.Date
java.lang.ClassCastException: org.joda.time.DateTime 不能转换为 java.util.Date
回答by axtavt
Perhaps you don't have Joda-Time Hibernatein the classpath. It's needed for type="org.joda.time.contrib.hibernate.PersistentDateTime"
.
也许您的类路径中没有Joda-Time Hibernate。需要它type="org.joda.time.contrib.hibernate.PersistentDateTime"
。
UPDATE:
更新:
It appears that Joda-time Hibernate support currently doesn't work with Hibernate 3.6, see PersistentDateTime doesn't work in hibernate 3.6 - ID: 3090209and Recompile Joda-Time Hibernate to make it work with Hibernate 3.6.
看来 Joda-time Hibernate 支持目前不适用于 Hibernate 3.6,请参阅PersistentDateTime 在 hibernate 3.6 中不起作用 - ID:3090209和重新编译 Joda-Time Hibernate 以使其与 Hibernate 3.6 一起工作。
You can use User Type projectinstead (type attribute becomes type="org.jadira.usertype.dateandtime.joda.PersistentDateTime"
).
您可以改用用户类型项目(类型属性变为type="org.jadira.usertype.dateandtime.joda.PersistentDateTime"
)。