NimChimpsky
NimChimpsky

Reputation: 47300

Spring 3.1, Hibernate 4, SessionFactory

This was working:

<bean id="sessionFactory"  
      class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
...

but upgrading to the aforementioned versions breaks it. What is the correct method to create a SessionFactory bean with Spring 3.1.Release and Hibernate 4.0.0.FINAL?

The error on deploy is:

nested exception is java.lang.NoClassDefFoundError: Lorg/hibernate/cache/CacheProvider;


EDIT
Have added my own answer, which fixed it for me.

Upvotes: 42

Views: 78556

Answers (6)

Yuci
Yuci

Reputation: 30199

  • In the case of Hibernate 4.0 or higher, as of Spring 4.0, you should use
org.springframework.orm.hibernate4.LocalSessionFactoryBean

For example:

<bean id="sessionFactory" 
      class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    ...
</bean>

See http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/orm/hibernate4/LocalSessionFactoryBean.html

  • In the case of Hibernate 5.0/5.1/5.2, as of Spring 4.3, you should better instead use
org.springframework.orm.hibernate5.LocalSessionFactoryBean

(See http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/orm/hibernate5/LocalSessionFactoryBean.html)

Upvotes: 0

qnox
qnox

Reputation: 1481

I think you should use org.springframework.orm.hibernate4.LocalSessionFactoryBean instead of org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean

From LocalSessionFactoryBean javadoc:

NOTE: This variant of LocalSessionFactoryBean requires Hibernate 4.0 or higher. It is similar in role to the same-named class in the orm.hibernate3 package. However, in practice, it is closer to AnnotationSessionFactoryBean since its core purpose is to bootstrap a SessionFactory from annotation scanning.

Upvotes: 92

Defrag
Defrag

Reputation: 146

Use this configuration

hibernate configuration file:

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
</bean>

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

POM:

    <!-- CGLIB -->
    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib</artifactId>
        <version>${cglib-version}</version>
        <scope>runtime</scope>
    </dependency>

    <!-- Hibernate -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${org.hibernate-version}</version>
        <!-- will come with Hibernate core -->
    </dependency>

    <!-- Spring -->
   <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>

i forgot to include the versions, I am using hibernate version: 4.1.2.Final and spring version: 3.1.1.RELEASE, there is an update of hibernate 4.1.3.Final, not tested but I believe it will work fine.

Upvotes: 8

Augustine
Augustine

Reputation: 1

Spring 3.1 and Hibernate 4 are not compatible in so many ways. Please refer the following Spring JIRA https://jira.springsource.org/browse/SPR-9365

Upvotes: -3

NimChimpsky
NimChimpsky

Reputation: 47300

I had to change a couple of things, here we go :

In my transaction manager set up changed 3 -> 4 :

org.springframework.orm.hibernate4.HibernateTransactionManager;

And my sessionFactory to this (thanks @toxin) :

<bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"

Upvotes: 5

Go Dan
Go Dan

Reputation: 15512

Hibernate 4 has removed the deprecated CacheProvider-related interfaces and classes in favor of the previously released RegionFactory-related cache interface. You can find the version 4 cache package summary here, the version 3.2 cache package summary here (just before the RegionFactory interface was added) and the version 3.3 cache package summary here (when RegionFactory was first released).

Other than the JavaDoc, you might find the following documentation useful:

However, based on the Spring 3.1 dependencies Spring 3.1 does not require Hibernate 4 (under the Full Dependencies section, JBoss Hibernate Object-Relational Mapper is at version 3.3.2.GA). If you want to upgrade to Hibernate 4, you'll need to update your cache settings. Otherwise, try using Hibernate 3.3.2 or higher 3.X version instead.

UPDATE: Keep in mind, Hibernate 4 documentation in Spring 3.1 is currently sparse. The Spring Framework Reference Documentation only has the following for Support for Hibernate 4.x:

See Javadoc for classes within the new org.springframework.orm.hibernate4 package

Spring 3.1 introduces the LocalSessionFactoryBuilder, which extends Hibernate's Configuration.

It would seem you should keep an eye out for some other changes if you want to use Hibernate 4.

UPDATE 2: Just noticed this question is a close duplicate of Exception NoClassDefFoundError for CacheProvider.

Upvotes: 15

Related Questions