Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 607
NimChimpsky Avatar asked Dec 19 '11 17:12

NimChimpsky


People also ask

Which version of hibernate 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.

Can we use hibernate 4 with spring 5?

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.

What is SessionFactory in hibernate?

The SessionFactory is a thread safe object and used by all the threads of an application. The SessionFactory is a heavyweight object; it is usually created during application start up and kept for later use. You would need one SessionFactory object per database using a separate configuration file.

How SessionFactory is created in hibernate?

Most importantly, the SessionFactory in Hibernate is responsible for the creation of Session objects. The Hibernate Session provides methods such as save, delete and update, all of which are used to perform CRUD-based operations on the database to which the SessionFactory connects.


2 Answers

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.

like image 54
qnox Avatar answered Oct 07 '22 20:10

qnox


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:

  • Using JBoss Cache as a Hibernate Second Level Cache - Chapter 5. Architecture
  • Ehcache Hibernate Second-Level Cache
  • Hibernate 4 - The Second Level Cache

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.

like image 23
Dan Cruz Avatar answered Oct 07 '22 22:10

Dan Cruz