我在 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 等待 Date 类型返回,即使在注释中使用 columnDefinition 之后?我应该如何正确地做到这一点?