【问题标题】:Can't convert type: value: java.sql.Timestamp to org.joda.time.DateTime无法将类型:值:java.sql.Timestamp 转换为 org.joda.time.DateTime
【发布时间】:2016-02-23 00:13:32
【问题描述】:

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

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

【问题讨论】:

    标签: java hibernate jodatime


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-27
      • 1970-01-01
      • 2019-10-20
      • 2016-12-07
      • 1970-01-01
      • 2013-01-27
      相关资源
      最近更新 更多