![](/img/trans.png)
[英]Cannot convert of type class java.sql.Timestamp to class org.joda.time.DateTime
[英]Can't convert type: value: java.sql.Timestamp to org.joda.time.DateTime
在持續時間內-一切正常,但是當我嘗試執行下一個查詢時,我將遇到異常
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
在這種情況下,我選擇使用像這樣的實體查詢:
Query query = sessionFactory.getCurrentSession()
.createSQLQuery("Select * From QUOTE")
.addEntity(QuoteEntity.class);
return query.list();
因為,非托管實體的行為很奇怪
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.