HibernateTemplate is throwing java.lang.NoSuchMethodError: org.hibernate.Session.getFlushMode() -- Spring4.2.6, Hibernate5.2

ludwig17 picture ludwig17 · Jun 4, 2016 · Viewed 9k times · Source

I have looked around similar postings with SessionFactory and missing Main(). My problem is not related to them. Not sure if any jar is a mismatch. I am trying a sample Spring4.2.6 with Hibernate5.2 code using JDK8 and getting this exception:

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$12.doInHibernate(HibernateTemplate.java:619)
at org.springframework.orm.hibernate5.HibernateTemplate$12.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)

Below is the jar files I am using:

<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 code snippet:

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

My Configuration file looks like:

<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>

Thanks for your help.

Answer

Andr&#233; picture André · Jun 5, 2016

I still couldn't make Spring 4.2.6 to work with Hibernate 5.2.0 (which was just released some days ago), although the error I got is different. I'm still using Hibernate 5.1.0 then.

Note that on Hibernate 5.1.0 you need two jars: hibernate-core and hibernate-entitymanager.

[EDIT]Spring 4.2.6 lists Hibernate 5.0.9 as optional dependency so you probably should stick with that version, although 5.1.0 is working fine for me.

Spring 4.2.6 probably isn't compatible with Hibernate 5.2.0 as this version of hibernate brought some big changes, merging hibernate-core with hibernate-entitymanager and hibernate-java8.