postgresql 为什么 org.hibernate.dialect.PostgreSQLDialect 不能转换为 org.hibernate.dialect.Dialect?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19601089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
why org.hibernate.dialect.PostgreSQLDialect cannot be cast to org.hibernate.dialect.Dialect?
提问by Marc
In hibernate.xml, I have :
在 hibernate.xml 中,我有:
<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.
引起:java.lang.ClassCastException: org.hibernate.dialect.PostgreSQLDialect 不能转换为 org.hibernate.service.jdbc.dialect.internal 的 org.hibernate.dialect.Dialect。
Here is the relevant bit of my pom.xml:
这是我的 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:
我已经将问题追溯到 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 );
}
}
Attempting to instantiate a Dialog here results in the above ClassCastException.
尝试在此处实例化 Dialog 会导致上述 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
类 org.hibernate.dialect.PostgreSQLDialect
class org.hibernate.dialect.PostgreSQL82Dialect
类 org.hibernate.dialect.PostgreSQL82Dialect
class org.hibernate.dialect.PostgreSQL81Dialect
类 org.hibernate.dialect.PostgreSQL81Dialect
class org.hibernate.dialect.Dialect
类 org.hibernate.dialect.Dialect
yet... sure enough, org.hibernate.dialect.Dialect.class.isAssignableFrom(classLoaderService.classForName( dialectName ) ) returns false.
然而……果然, org.hibernate.dialect.Dialect.class.isAssignableFrom(classLoaderService.classForName( dialectName ) ) 返回 false。
Could this be some sort of incompatibility between versions of hibernate and the drivers provided by Postgresql?
这可能是 hibernate 版本和 Postgresql 提供的驱动程序之间的某种不兼容吗?
Been tearing my hair out. Any help much appreciated!
一直在撕我的头发。非常感谢任何帮助!
回答by Marc
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 :
是的。问题似乎是我试图混合各种休眠依赖项的不兼容版本。我刚刚用以下内容替换了上面引用的 pom.xml 部分:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search</artifactId>
<version>4.3.0.Final</version>
</dependency>
And I'm on to my next issue!
我要开始我的下一个问题了!
回答by Daniel Hári
That was a Hibernate known bug that has been fixed in 4.1.4.Final
这是一个 Hibernate 已知错误,已在4.1.4.Final中修复
Hibernate bug report: https://hibernate.atlassian.net/browse/HHH-7084