42

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.

informatik01
  • 16,038
  • 10
  • 74
  • 104
NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
  • Actually it's was deprecated. So removing is pretty suspectable http://docs.jboss.org/hibernate/core/3.3/api/org/hibernate/cache/CacheProvider.html – Stan Kurilin Dec 19 '11 at 17:46

6 Answers6

92

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.

Parth Soni
  • 11,158
  • 4
  • 32
  • 54
qnox
  • 1,471
  • 10
  • 8
  • 1
    I can't see the orm.hibernate4 only the .hibernate3 could you give any insight here ? I have imported hibernate 4... Solved: Make sure you use Spring 3.2.4 as well. 3.0.5 is too old. – Anders Metnik Oct 31 '13 at 09:36
  • 1
    I've just come across to this example, I hope this might help to anybody http://www.baeldung.com/hibernate-4-spring – Raul Luna Dec 01 '14 at 15:15
15

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.

Community
  • 1
  • 1
Go Dan
  • 15,194
  • 6
  • 41
  • 65
8

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.

Defrag
  • 146
  • 1
  • 5
5

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"
NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
  • I did the same sir and got org.springframework.beans.NotWritablePropertyException: Invalid property 'exposeTransactionAwareSessionFactory' of bean class... – masT Sep 06 '13 at 11:15
0
  • 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)

Yuci
  • 27,235
  • 10
  • 114
  • 113
-3

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

Augustine
  • 1
  • 1
  • 2
    The report is about, Spring 3.x and Hibernate 4 are not compatible. Unable to use HibernateTemplate. This is because HibernateTemplate is no longer supported by Hibernate 4. – Hemeroc May 15 '12 at 10:18