HibernateTemplate is throwing java.lang.NoSuchMethodError: org.hibernate.Session.getFlushMode() -- Spring4.2.6, Hibernate5.2
HibernateTemplate is throwing java.lang.NoSuchMethodError: org.hibernate.Session.getFlushMode() -- Spring4.2.6, Hibernate5.2
我查看了有关 SessionFactory 和缺少 Main() 的类似帖子。我的问题与他们无关。不确定是否有任何 jar 不匹配。我正在使用 JDK8 尝试一个带有 Hibernate5.2 代码的示例 Spring4.2.6 并得到这个异常:
Exception in thread "main" java.lang.NoSuchMethodError: org.hibernate.Session.getFlushMode()Lorg/hibernate/FlushMode;
at org.springframework.orm.hibernate5.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1125)
at org.springframework.orm.hibernate5.HibernateTemplate.doInHibernate(HibernateTemplate.java:619)
at org.springframework.orm.hibernate5.HibernateTemplate.doInHibernate(HibernateTemplate.java:616)
at org.springframework.orm.hibernate5.HibernateTemplate.doExecute(HibernateTemplate.java:341)
at org.springframework.orm.hibernate5.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:309)
at org.springframework.orm.hibernate5.HibernateTemplate.save(HibernateTemplate.java:616)
at com.nougain.springtraining.hibernate.CustomerDAOImpl.save(CustomerDAOImpl.java:37)
at com.nougain.springtraining.hibernate.Client_SpringHibernate.testSpringHibernate(Client_SpringHibernate.java:29)
at com.nougain.springtraining.hibernate.Client_SpringHibernate.main(Client_SpringHibernate.java:12)
以下是我正在使用的 jar 文件:
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="C:/Java Stuff/spring-framework-4.2.6.RELEASE/libs/spring-beans-4.2.6.RELEASE.jar"/>
<classpathentry kind="lib" path="C:/Java Stuff/spring-framework-4.2.6.RELEASE/libs/spring-core-4.2.6.RELEASE.jar"/>
<classpathentry kind="lib" path="C:/Java Stuff/spring-framework-4.2.6.RELEASE/libs/spring-context-4.2.6.RELEASE.jar"/>
<classpathentry kind="lib" path="C:/Java Stuff/Apache Commons/commons-logging-1.2/commons-logging-1.2.jar"/>
<classpathentry kind="lib" path="C:/Java Stuff/spring-framework-4.2.6.RELEASE/libs/spring-expression-4.2.6.RELEASE.jar"/>
<classpathentry kind="lib" path="C:/Java Stuff/jars/javax.inject.jar"/>
<classpathentry kind="lib" path="C:/Java Stuff/spring-framework-4.2.6.RELEASE/libs/spring-test-4.2.6.RELEASE.jar"/>
<classpathentry kind="lib" path="C:/Java Stuff/spring-framework-4.2.6.RELEASE/libs/spring-jdbc-4.2.6.RELEASE.jar"/>
<classpathentry kind="lib" path="C:/Java Stuff/spring-framework-4.2.6.RELEASE/libs/spring-orm-4.2.6.RELEASE.jar"/>
<classpathentry exported="true" kind="lib" path="C:/Java Stuff/hibernate-release-5.2.0.Final/lib/required/jboss-logging-3.3.0.Final.jar"/>
<classpathentry kind="lib" path="C:/Java Stuff/Oracle JDBC Driver/ojdbc7.jar"/>
<classpathentry kind="lib" path="C:/Java Stuff/junit-4.12/junit-4.12.jar"/>
<classpathentry kind="lib" path="C:/Java Stuff/junit-4.12/hamcrest-core-1.3.jar"/>
<classpathentry kind="lib" path="C:/Java Stuff/spring-framework-4.2.6.RELEASE/libs/spring-tx-4.2.6.RELEASE.jar"/>
<classpathentry kind="lib" path="C:/Java Stuff/jars/aopalliance-1.0.jar"/>
<classpathentry kind="lib" path="C:/Java Stuff/jars/aspectjweaver-1.8.9.jar"/>
<classpathentry kind="lib" path="C:/Java Stuff/spring-framework-4.2.6.RELEASE/libs/spring-aop-4.2.6.RELEASE.jar"/>
<classpathentry kind="lib" path="C:/Java Stuff/Apache Commons/commons-dbcp2-2.1.1/commons-dbcp2-2.1.1.jar"/>
<classpathentry kind="lib" path="C:/Java Stuff/Apache Commons/commons-pool2-2.4.2/commons-pool2-2.4.2.jar"/>
<classpathentry kind="lib" path="C:/Java Stuff/hibernate-release-5.2.0.Final/lib/required/hibernate-core-5.2.0.Final.jar"/>
<classpathentry kind="lib" path="C:/Java Stuff/hibernate-release-5.2.0.Final/lib/required/hibernate-jpa-2.1-api-1.0.0.Final.jar"/>
<classpathentry kind="lib" path="C:/Java Stuff/hibernate-release-5.2.0.Final/lib/required/geronimo-jta_1.1_spec-1.1.1.jar"/>
<classpathentry kind="lib" path="C:/Java Stuff/hibernate-release-5.2.0.Final/lib/required/dom4j-1.6.1.jar"/>
<classpathentry kind="lib" path="C:/Java Stuff/hibernate-release-5.2.0.Final/lib/required/hibernate-commons-annotations-5.0.1.Final.jar"/>
<classpathentry kind="lib" path="C:/Java Stuff/hibernate-release-5.2.0.Final/lib/required/classmate-1.3.0.jar"/>
DAOImpl 代码片段:
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
@Component("customerDAO")
public class CustomerDAOImpl implements CustomerDAO {
@Autowired
private HibernateTemplate hibernateTemplate;
public CustomerDAOImpl() {
System.out.println("___Created CustomerDAOImpl() object");
}
@Override
public void save(Customer c) {
hibernateTemplate.save(c);
}
...
}
我的配置文件如下所示:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<array>
<value>bin/connection.properties</value>
</array>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="${driver_class_name}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
<property name="initialSize" value="${initial_size}"/>
<property name="maxTotal" value="${max_size}"/>
</bean>
<bean class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.use_sql_comments">true</prop>
<prop key="hibernate.transaction.factory_class">
org.hibernate.transaction.JDBCTransactionFactory
</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
<property name="mappingResources">
<array>
<value>Customer.hbm.xml</value>
</array>
</property>
</bean>
感谢您的帮助。
我仍然无法使 Spring 4.2.6 与 Hibernate 5.2.0(几天前刚刚发布)一起工作,尽管我得到的错误是不同的。那时候我还在用Hibernate 5.1.0
请注意,在 Hibernate 5.1.0 上,您需要两个 jar:hibernate-core 和 hibernate-entitymanager。
[编辑]Spring 4.2.6 将 Hibernate 5.0.9 列为可选依赖项,因此您可能应该坚持使用该版本,尽管 5.1.0 对我来说工作正常。
Spring 4.2.6 可能与 Hibernate 5.2.0 不兼容,因为这个版本的 hibernate 带来了一些重大变化,合并了 hibernate-core 与 hibernate-entitymanager 和 hibernate-java8。
我查看了有关 SessionFactory 和缺少 Main() 的类似帖子。我的问题与他们无关。不确定是否有任何 jar 不匹配。我正在使用 JDK8 尝试一个带有 Hibernate5.2 代码的示例 Spring4.2.6 并得到这个异常:
Exception in thread "main" java.lang.NoSuchMethodError: org.hibernate.Session.getFlushMode()Lorg/hibernate/FlushMode;
at org.springframework.orm.hibernate5.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1125)
at org.springframework.orm.hibernate5.HibernateTemplate.doInHibernate(HibernateTemplate.java:619)
at org.springframework.orm.hibernate5.HibernateTemplate.doInHibernate(HibernateTemplate.java:616)
at org.springframework.orm.hibernate5.HibernateTemplate.doExecute(HibernateTemplate.java:341)
at org.springframework.orm.hibernate5.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:309)
at org.springframework.orm.hibernate5.HibernateTemplate.save(HibernateTemplate.java:616)
at com.nougain.springtraining.hibernate.CustomerDAOImpl.save(CustomerDAOImpl.java:37)
at com.nougain.springtraining.hibernate.Client_SpringHibernate.testSpringHibernate(Client_SpringHibernate.java:29)
at com.nougain.springtraining.hibernate.Client_SpringHibernate.main(Client_SpringHibernate.java:12)
以下是我正在使用的 jar 文件:
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="C:/Java Stuff/spring-framework-4.2.6.RELEASE/libs/spring-beans-4.2.6.RELEASE.jar"/>
<classpathentry kind="lib" path="C:/Java Stuff/spring-framework-4.2.6.RELEASE/libs/spring-core-4.2.6.RELEASE.jar"/>
<classpathentry kind="lib" path="C:/Java Stuff/spring-framework-4.2.6.RELEASE/libs/spring-context-4.2.6.RELEASE.jar"/>
<classpathentry kind="lib" path="C:/Java Stuff/Apache Commons/commons-logging-1.2/commons-logging-1.2.jar"/>
<classpathentry kind="lib" path="C:/Java Stuff/spring-framework-4.2.6.RELEASE/libs/spring-expression-4.2.6.RELEASE.jar"/>
<classpathentry kind="lib" path="C:/Java Stuff/jars/javax.inject.jar"/>
<classpathentry kind="lib" path="C:/Java Stuff/spring-framework-4.2.6.RELEASE/libs/spring-test-4.2.6.RELEASE.jar"/>
<classpathentry kind="lib" path="C:/Java Stuff/spring-framework-4.2.6.RELEASE/libs/spring-jdbc-4.2.6.RELEASE.jar"/>
<classpathentry kind="lib" path="C:/Java Stuff/spring-framework-4.2.6.RELEASE/libs/spring-orm-4.2.6.RELEASE.jar"/>
<classpathentry exported="true" kind="lib" path="C:/Java Stuff/hibernate-release-5.2.0.Final/lib/required/jboss-logging-3.3.0.Final.jar"/>
<classpathentry kind="lib" path="C:/Java Stuff/Oracle JDBC Driver/ojdbc7.jar"/>
<classpathentry kind="lib" path="C:/Java Stuff/junit-4.12/junit-4.12.jar"/>
<classpathentry kind="lib" path="C:/Java Stuff/junit-4.12/hamcrest-core-1.3.jar"/>
<classpathentry kind="lib" path="C:/Java Stuff/spring-framework-4.2.6.RELEASE/libs/spring-tx-4.2.6.RELEASE.jar"/>
<classpathentry kind="lib" path="C:/Java Stuff/jars/aopalliance-1.0.jar"/>
<classpathentry kind="lib" path="C:/Java Stuff/jars/aspectjweaver-1.8.9.jar"/>
<classpathentry kind="lib" path="C:/Java Stuff/spring-framework-4.2.6.RELEASE/libs/spring-aop-4.2.6.RELEASE.jar"/>
<classpathentry kind="lib" path="C:/Java Stuff/Apache Commons/commons-dbcp2-2.1.1/commons-dbcp2-2.1.1.jar"/>
<classpathentry kind="lib" path="C:/Java Stuff/Apache Commons/commons-pool2-2.4.2/commons-pool2-2.4.2.jar"/>
<classpathentry kind="lib" path="C:/Java Stuff/hibernate-release-5.2.0.Final/lib/required/hibernate-core-5.2.0.Final.jar"/>
<classpathentry kind="lib" path="C:/Java Stuff/hibernate-release-5.2.0.Final/lib/required/hibernate-jpa-2.1-api-1.0.0.Final.jar"/>
<classpathentry kind="lib" path="C:/Java Stuff/hibernate-release-5.2.0.Final/lib/required/geronimo-jta_1.1_spec-1.1.1.jar"/>
<classpathentry kind="lib" path="C:/Java Stuff/hibernate-release-5.2.0.Final/lib/required/dom4j-1.6.1.jar"/>
<classpathentry kind="lib" path="C:/Java Stuff/hibernate-release-5.2.0.Final/lib/required/hibernate-commons-annotations-5.0.1.Final.jar"/>
<classpathentry kind="lib" path="C:/Java Stuff/hibernate-release-5.2.0.Final/lib/required/classmate-1.3.0.jar"/>
DAOImpl 代码片段:
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
@Component("customerDAO")
public class CustomerDAOImpl implements CustomerDAO {
@Autowired
private HibernateTemplate hibernateTemplate;
public CustomerDAOImpl() {
System.out.println("___Created CustomerDAOImpl() object");
}
@Override
public void save(Customer c) {
hibernateTemplate.save(c);
}
...
}
我的配置文件如下所示:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<array>
<value>bin/connection.properties</value>
</array>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="${driver_class_name}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
<property name="initialSize" value="${initial_size}"/>
<property name="maxTotal" value="${max_size}"/>
</bean>
<bean class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.use_sql_comments">true</prop>
<prop key="hibernate.transaction.factory_class">
org.hibernate.transaction.JDBCTransactionFactory
</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
<property name="mappingResources">
<array>
<value>Customer.hbm.xml</value>
</array>
</property>
</bean>
感谢您的帮助。
我仍然无法使 Spring 4.2.6 与 Hibernate 5.2.0(几天前刚刚发布)一起工作,尽管我得到的错误是不同的。那时候我还在用Hibernate 5.1.0
请注意,在 Hibernate 5.1.0 上,您需要两个 jar:hibernate-core 和 hibernate-entitymanager。
[编辑]Spring 4.2.6 将 Hibernate 5.0.9 列为可选依赖项,因此您可能应该坚持使用该版本,尽管 5.1.0 对我来说工作正常。
Spring 4.2.6 可能与 Hibernate 5.2.0 不兼容,因为这个版本的 hibernate 带来了一些重大变化,合并了 hibernate-core 与 hibernate-entitymanager 和 hibernate-java8。