Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure Hibernate 5 with Spring 4

I wanted to upgrade to Hibernate 5 but I can't get it to work for some weird reason.

My spring context configuration with Hibernate 4 looks like this:

....
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="annotatedClasses" ref="hibernateClasses" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">
                ${hibernate.dialect}
            </prop>
            <prop key="hibernate.show_sql">
                ${hibernate.show_sql}
            </prop>
            <prop key="hibernate.format_sql">true</prop>
            <prop key="hibernate.generate_statistics">
                ${hibernate.generate_statistics}
            </prop>
            <prop key="hibernate.hbm2ddl.auto">
                ${hibernate.hbm2ddl.auto}
            </prop>
        </props>
    </property>
</bean>

<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

....

I left out the datasource and the hibernateClasses beans definition.

For Hibernate 5 I changed the package name to hibernate5 like this:

....
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="annotatedClasses" ref="hibernateClasses" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">
                ${hibernate.dialect}
            </prop>
            <prop key="hibernate.show_sql">
                ${hibernate.show_sql}
            </prop>
            <prop key="hibernate.format_sql">true</prop>
            <prop key="hibernate.generate_statistics">
                ${hibernate.generate_statistics}
            </prop>
            <prop key="hibernate.hbm2ddl.auto">
                ${hibernate.hbm2ddl.auto}
            </prop>
        </props>
    </property>
</bean>

<bean id="transactionManager"
    class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

....

when I run my tests the application context fails to start up because it cannot find the LocalSessionFactoryBean class :-S. This class resides in spring-orm JAR and is actually present but for some reason it cannot be found and throws the following exception:

java.lang.NoClassDefFoundError: Could not initialize class org.springframework.orm.hibernate5.LocalSessionFactoryBuilder

My Maven dependencies are as follows:

    <!-- Servlet API -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>${servlet-api.version}</version>
        <scope>provided</scope>
    </dependency>

    <!-- Spring libraries -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <!-- Hibernate -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate.version}</version>
        <exclusions>
            <exclusion>
                <groupId>cglib</groupId>
                <artifactId>cglib</artifactId>
            </exclusion>
            <exclusion>
                <groupId>asm</groupId>
                <artifactId>asm</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>${slf4j.version}</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${slf4j.version}</version>
        <scope>provided</scope>
    </dependency>

    <!-- Test -->
    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib</artifactId>
        <version>${cglib.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>asm</groupId>
        <artifactId>asm</artifactId>
        <version>${asm.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${spring.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-mock</artifactId>
        <version>${spring-mock.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.easymock</groupId>
        <artifactId>easymock</artifactId>
        <version>${easymock.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
        <version>${hsqldb.version}</version>
        <scope>test</scope>
    </dependency>

Does anybody have an idea what I am doing wrong?

like image 843
Tranquilized Avatar asked Nov 26 '15 15:11

Tranquilized


People also ask

Does spring 4 support Hibernate 5?

3 with Spring Boot 2.2. 4. That being said only HIbernate 5 is supported by Spring 5, so no you cannot use hibernate4 with Spring Boot 2.2 or Spring 5.

Which Hibernate version is compatible with spring 4?

Note that Hibernate 4.3 is a JPA 2.1 provider and therefore only supported as of Spring Framework 4.0.

Which Hibernate version is compatible with spring 5?

Please note that Hibernate Search needs to be upgraded to 5.11. 6 for Spring Framework 5.3 JPA compatibility; see Hibernate JIRA.


2 Answers

May be you should add

<dependency>
  <groupId>javax.transaction</groupId>
  <artifactId>jta</artifactId>
  <version>1.1</version>
</dependency>

I had the same issue, and my initial logs complained about,

java.lang.NoClassDefFoundError: javax/transaction/SystemException

like image 177
sapna Avatar answered Oct 17 '22 05:10

sapna


@sapna answer is working.

But for those curious about why it breaks between hibernate 5.0.3.Final and 5.0.4.Final, here is the answer : They removed the dependency on jta, which was :

<dependency>
    <groupId>org.apache.geronimo.specs</groupId>
    <artifactId>geronimo-jta_1.1_spec</artifactId>
    <version>1.1.1</version>
    <scope>compile</scope>
</dependency>

The change is described there : HHH-10178

So if you are upgrading from 5.0.x to 5.0.4 or 5.0.5 and wish to not change anything to your classpath, you should rather import this lib than the javax.transaction:jta one.

EDIT: they reverted this change with hibernte 5.0.7 : HHH-10307, so upgrading to this version or newer should also fix the issue.

like image 8
Thierry Avatar answered Oct 17 '22 04:10

Thierry