In hibernate.xml, I have :
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
and here is what I'm trying to do:
final Session sess = sessionFactory.openSession();
Transaction tx = null;
try {
tx = sess.beginTransaction();
Collection<Rfc> rfcs;
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Rfc.class);
criteria = criteria.add(Restrictions.like(Rfc.RFC_IDENTIFIER, input.replace('*', '%')));
rfcs = criteria.list();
// ...
tx.commit();
} catch (final Exception e) {
if (tx != null) {
tx.rollback();
}
throw new IllegalArgumentException(e);
} finally {
sess.close();
}
This all seems very straightforward, but I'm getting:
Caused by: java.lang.ClassCastException: org.hibernate.dialect.PostgreSQLDialect cannot be cast to org.hibernate.dialect.Dialect at org.hibernate.service.jdbc.dialect.internal.
Here is the relevant bit of my pom.xml:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<!-- <version>4.1.9.Final</version>-->
<version>4.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.6.3.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.5.6-Final</version>
</dependency>
I've traced the issue to a method of org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.constructDialect(), where we find:
private Dialect constructDialect(String dialectName) {
try {
return ( Dialect ) classLoaderService.classForName( dialectName ).newInstance();
}
catch ( ClassLoadingException e ) {
throw new HibernateException( "Dialect class not found: " + dialectName, e );
}
catch ( HibernateException e ) {
throw e;
}
catch ( Exception e ) {
throw new HibernateException( "Could not instantiate dialect class", e );
}
}
Attempting to instantiate a Dialog here results in the above ClassCastException.
Just to be extra sure I have the right class, I displayed all the superclasses in its hierarchy... and here is exactly what I see in my console:
class org.hibernate.dialect.PostgreSQLDialect
class org.hibernate.dialect.PostgreSQL82Dialect
class org.hibernate.dialect.PostgreSQL81Dialect
class org.hibernate.dialect.Dialect
yet... sure enough, org.hibernate.dialect.Dialect.class.isAssignableFrom(classLoaderService.classForName( dialectName ) ) returns false.
Could this be some sort of incompatibility between versions of hibernate and the drivers provided by Postgresql?
Been tearing my hair out. Any help much appreciated!