Avoid implicit conversion from date to timestamp for selects with Oracle using Hibernate
Posted
by sapporo
on Stack Overflow
See other posts from Stack Overflow
or by sapporo
Published on 2010-03-17T17:40:47Z
Indexed on
2010/03/19
16:11 UTC
Read the original article
Hit count: 321
I'm using Hibernate 3.2.7.GA criteria queries to select rows from an Oracle Enterprise Edition 10.2.0.4.0 database, filtering by a timestamp field. The field in question is of type java.util.Date
in Java, and DATE
in Oracle.
It turns out that the field gets mapped to java.sql.Timestamp
, and Oracle converts all rows to TIMESTAMP
before comparing to the passed in value, bypassing the index and thereby ruining performance.
One solution would be to use Hibernate's sqlRestriction()
along with Oracle's TO_DATE
function. That would fix performance, but requires rewriting the application code (lots of queries).
So is there a more elegant solution? Since Hibernate already does type mapping, could it be configured to do the right thing?
Update: The problem occurs in a variety of configurations, but here's one specific example:
- Oracle Enterprise Edition 10.2.0.4.0
- Oracle JDBC Driver 11.1.0.7.0
- Hibernate 3.2.7.GA
- Hibernate's Oracle10gDialect
- Java 1.6.0_16
© Stack Overflow or respective owner