Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why org.hibernate.dialect.PostgreSQLDialect cannot be cast to org.hibernate.dialect.Dialect?

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!

like image 664
Marc Avatar asked Dec 04 '25 14:12

Marc


2 Answers

Yep. The problem, it seems, was that I was trying to mix incompatible versions of various hibernate dependencies. I just replaced section of the pom.xml cited above with :

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-search</artifactId>
        <version>4.3.0.Final</version>
    </dependency>

And I'm on to my next issue!

like image 79
Marc Avatar answered Dec 06 '25 09:12

Marc


That was a Hibernate known bug that has been fixed in 4.1.4.Final

Hibernate bug report: https://hibernate.atlassian.net/browse/HHH-7084

like image 31
Daniel Hári Avatar answered Dec 06 '25 09:12

Daniel Hári



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!