我在H2数据库中有一个时间戳字段,但我需要使用Hibernate 4.3从我的bean中获取String而不是Date,所以我试图在我的bean中这样做:
import java.util.Date;
...
private Date msgDate;
@Column(name = "msgDate", columnDefinition="timestamp")
public String getMsgDate() {
SimpleDateFormat df = new SimpleDateFormat("yyyy MM dd HH mm ss");
return df.format(msgDate);
}
public void setMsgDate(Date msgDate) {
this.msgDate = msgDate;
}
但我有下一个错误:
ERROR: HHH000123: IllegalArgumentException in class: Messages, setter method of property: msgDate
июл 03, 2013 11:39:56 AM org.hibernate.property.BasicPropertyAccessor$BasicSetter set
ERROR: HHH000091: Expected type: java.util.Date, actual value: java.lang.String
июл 03, 2013 11:39:56 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [MessageList] in context with path [/messenger] threw exception
org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of com.epam.epamlab.messenger.beans.Messages.msgDate
at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:119)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.java:711)
at org.hibernate.tuple.entity.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:371)
at org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:4519)
at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:188)
at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:144)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1102)
at org.hibernate.loader.Loader.processResultSet(Loader.java:959)
at org.hibernate.loader.Loader.doQuery(Loader.java:906)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:340)
at org.hibernate.loader.Loader.doList(Loader.java:2523)
at org.hibernate.loader.Loader.doList(Loader.java:2509)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2339)
at org.hibernate.loader.Loader.list(Loader.java:2334)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:491)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:369)
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:222)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1270)
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:101)
...
Caused by: java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:65)
... 40 more
如果我从getter返回Date,一切都很好。为什么Hibernate会等待返回日期类型,即使在注释中使用columnDefinition后也是如此?我该如何正确地做到这一点?
答案 0 :(得分:2)
让getters / setters保持不变。 Hibernate需要这些方法与字段类型一致才能执行操作。没有什么能阻止你在课堂上定义你自己的特殊get
方法。
private Date msgDate;
@Column(name = "msgDate", columnDefinition="timestamp")
public Date getMsgDate() {
return this.msgDate;
}
public void setMsgDate(Date msgDate) {
this.msgDate = msgDate;
}
public Date getMsgDateFormatted() {
SimpleDateFormat df = new SimpleDateFormat("yyyy MM dd HH mm ss");
return df.format(msgDate);
}
答案 1 :(得分:0)
为什么不编写一个将Date作为输入并返回formattedString的实用程序方法。 Hibernate在内部使用这些setter和getter,并期望实体属性和数据库字段的数据类型兼容。
否则,如果您希望此格式在数据库中完全相同,请将此字段设置为数据库中的varchar和实体类中的字符串。
这就是你的方法的样子:
public String getFormattedMsgDate(Date msgDate) {
SimpleDateFormat df = new SimpleDateFormat("yyyy MM dd HH mm ss");
return df.format(msgDate);
}