H2 データベースにタイムスタンプ フィールドがありますが、Hibernate 4.3 を使用して Bean から Date ではなく String を取得する必要があるため、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 を返すと、すべてうまくいきます。注釈で columnDefinition を使用した後でも、Hibernate が Date タイプが返されるのを待つのはなぜですか? これを正しく行うにはどうすればよいですか?