为什么 org.hibernate.dialect.PostgreSQLDialect 不能转换为 org.hibernate.dialect.Dialect?

Mar*_*arc 3 postgresql hibernate

在 hibernate.xml 中,我有:

<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
Run Code Online (Sandbox Code Playgroud)

这就是我想做的:

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();
}
Run Code Online (Sandbox Code Playgroud)

这一切看起来非常简单,但我得到:

引起原因:java.lang.ClassCastException:org.hibernate.dialect.PostgreSQLDialect 无法在 org.hibernate.service.jdbc.dialect.internal 处转换为 org.hibernate.dialect.Dialect。

这是我的 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>
Run Code Online (Sandbox Code Playgroud)

我已将问题追溯到 org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.constructDialect() 的方法,我们发现:

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 );
    }
}
Run Code Online (Sandbox Code Playgroud)

尝试在此处实例化 Dialog 会导致上述 ClassCastException。

为了额外确保我拥有正确的类,我在其层次结构中显示了所有超类......这正是我在控制台中看到的内容:

类 org.hibernate.dialect.PostgreSQLDialect

类 org.hibernate.dialect.PostgreSQL82Dialect

类 org.hibernate.dialect.PostgreSQL81Dialect

类 org.hibernate.dialect.Dialect

然而......果然, org.hibernate.dialect.Dialect.class.isAssignableFrom(classLoaderService.classForName( dialectName ) ) 返回 false。

这可能是 hibernate 版本与 Postgresql 提供的驱动程序之间存在某种不兼容吗?

一直在撕扯我的头发。非常感谢任何帮助!

Mar*_*arc 5

是的。问题似乎是我试图混合各种休眠依赖项的不兼容版本。我刚刚将上面引用的 pom.xml 部分替换为:

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-search</artifactId>
        <version>4.3.0.Final</version>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

我正在讨论下一期!