无法将type:value:java.sql.Timestamp转换为org.joda.time.DateTime

时间:2015-11-20 13:24:11

标签: java hibernate jodatime

在持续时间内 - 一切正常,但当我尝试执行下一个查询时,我将有异常

Query query = sessionFactory.getCurrentSession()
        .createSQLQuery("Select * From QUOTE").addScalar("id", StandardBasicTypes.LONG)
        .addScalar("date", StandardBasicTypes.TIMESTAMP)  <-- this throw exception
        .addScalar("interval", myEnumType)
        .addScalar("open", StandardBasicTypes.DOUBLE)
        .addScalar("high", StandardBasicTypes.DOUBLE)
        .addScalar("low", StandardBasicTypes.DOUBLE)
        .addScalar("close", StandardBasicTypes.DOUBLE)
        .addScalar("volume", StandardBasicTypes.INTEGER).setResultTransformer(Transformers.aliasToBean(QuoteEntity.class));

    return query.list();

执行后我得到错误:

2015-11-20 14:03:08 ERROR BasicPropertyAccessor:121 - HHH000123: IllegalArgumentException in class: com.usanin.financedata.entity.QuoteEntity, setter method of property: date
2015-11-20 14:03:08 ERROR BasicPropertyAccessor:122 - HHH000091: Expected type: org.joda.time.DateTime, actual value: java.sql.Timestamp
IllegalArgumentException occurred while calling setter for property [com.usanin.financedata.entity.QuoteEntity.date (expected type = org.joda.time.DateTime)]; target = [com.usanin.financedata.entity.QuoteEntity@7cf3b5ad], property value = [2015-11-11 10:01:00.0]

我也有:Hibernate - 4.3.11.Final,和org.jadira.usertype:usertype.core:4.0.0.GA

我在我的

中调整了一下
    <bean id="sessionFactory"
    .....
    <property name="hibernateProperties">
     <props>
      <prop key="jadira.usertype.autoRegisterUserTypes">true</prop>
        ...

和实体

...
import org.joda.time.DateTime;
...

@Entity
@Table (name="QUOTE")
public class QuoteEntity {

    @Column
    private DateTime date;

... getters and setters

1 个答案:

答案 0 :(得分:0)

在这种情况下,我选择使用像这样的实体查询:

Query query = sessionFactory.getCurrentSession()
                .createSQLQuery("Select * From QUOTE")
                .addEntity(QuoteEntity.class);

return query.list();

因为,非托管实体的行为非常奇怪