3

在 hibernate.xml 中,我有:

<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>

这就是我想要做的:

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();
}

这一切似乎都非常简单,但我得到:

原因: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>

我已经将问题追溯到 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 );
    }
}

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

为了更加确定我有正确的类,我在其层次结构中显示了所有超类......这正是我在控制台中看到的:

类 org.hibernate.dialect.PostgreSQLDialect

类 org.hibernate.dialect.PostgreSQL82Dialect

类 org.hibernate.dialect.PostgreSQL81方言

类 org.hibernate.dialect.Dialect

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

这可能是休眠版本和 Postgresql 提供的驱动程序之间的某种不兼容吗?

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

4

2 回答 2

4

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

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

我正在处理我的下一个问题!

于 2013-10-26T00:28:15.670 回答
1

这是一个已知的 Hibernate 错误,已在4.1.4.Final中修复

休眠错误报告:https ://hibernate.atlassian.net/browse/HHH-7084

于 2015-11-21T22:38:35.333 回答