3

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!

0

2 Answers 2

5

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!

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.