Had to employ some native SQL to get around this oracle date query problem. Here's what I had to do to get around the mismatch on an oracle date query. I have this in my DAO, so it's only in one place. Hopefully better support for oracle dates will be included in a future Hibernate release.
Code:
public MyDBEntity findById( com.cji.noah.entities.MyDBEntityId id)
{
log.debug("getting MyDBEntity instance with id: " + id);
try {
MyDBEntity instance = getInstanceUsingNativeSQL(id);
if (instance==null) {
log.debug("get successful, no instance found");
}
else {
log.debug("get successful, instance found");
}
return instance;
}
catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
catch (SQLException sqle)
{
log.error("get failed", sqle);
throw new RuntimeException(sqle);
}
}
private MyDBEntity getInstanceUsingNativeSQL(MyDBEntityId id) throws HibernateException, SQLException
{
String myEntitySQL = "Select * from SCHEMA.TABLE where srch_col1 = " + id.getSrchCol1() + " and " +
"srch_col2 = " + id.getSrchCol2() + " and " +
"srch_col3 = " + id.getSrchCol3() + " and " +
"trunc(MY_DATE) = to_date('" + id.getSrchDate() + "','yyyy-mm-dd')";
List queryResults = sessionFactory.getCurrentSession().createSQLQuery(myEntitySQL).addEntity(MyDBEntity.class).list();
if(queryResults != null && !(queryResults.isEmpty()))
{
return (MyDBEntity) queryResults.get(0);
}
else
return null;
}