I am doing a native SQL query in JPA, but I get an error if I try to set a java.util.Date as a parameter.
Code:
[JDBCExceptionReporter] The value of input host variable or parameter number "1" cannot be used because of its data type.
The following code demonstrates the problem:
Code:
@PersistenceContext
HibernateEntityManager em;
String sql="SELECT * FROM TABLE1 WHERE DATE_CHANGED >= :changeDate";
public List getRecordsChanged() {
Date date = DateUtils.addMonths(new Date(), -1);
return em.createNativeQuery(sql, Table1.class)
.setParameter("changeDate", date)
.getResultList();
}
It works if I change it to:
Code:
@PersistenceContext
HibernateEntityManager em;
String sql="SELECT * FROM TABLE1 WHERE DATE_CHANGED >= :changeDate";
public List getRecordsChanged() {
Date date = DateUtils.addMonths(new Date(), -1);
return em.createNativeQuery(sql, Table1.class)
.setParameter("changeDate", new java.sql.Date(date.getTime()))
.getResultList();
}
Is there a property that I can set so that Hibernate will do this conversion automatically?
EDIT: Nevermind. Should have been:
Code:
@PersistenceContext
HibernateEntityManager em;
String sql="SELECT * FROM TABLE1 WHERE DATE_CHANGED >= :changeDate";
public List getRecordsChanged() {
Date date = DateUtils.addMonths(new Date(), -1);
return em.createNativeQuery(sql, Table1.class)
.setParameter("changeDate", date, TemporalType.DATE)
.getResultList();
}