在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 );
}
}试图在这里实例化对话框将导致上面的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提供的驱动程序之间的某种不兼容性吗?
一直在扯我的头发。任何帮助都非常感谢!
发布于 2013-10-26 00:28:15
是啊。问题似乎是,我试图混合各种hibernate依赖项的不兼容版本。我刚刚将上面引用的pom.xml部分替换为:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search</artifactId>
<version>4.3.0.Final</version>
</dependency>我开始我的下一期了!
发布于 2015-11-21 22:38:35
这是一个已在4.1.4中修复的Hibernate已知的bug。
Hibernate bug报告:https://hibernate.atlassian.net/browse/HHH-7084
https://stackoverflow.com/questions/19601089
复制相似问题