Hibernate 4 AnnotationSessionFactoryBean

Learn about Hibernate4 AnnotationSessionFactoryBean and understand what is the correct solution.

Hibernate 4 AnnotationSessionFactoryBean

Hibernate 4 AnnotationSessionFactoryBean

How to use AnnotationSessionFactoryBean in Hibernate 4? Can it be used?


In this tutorial we are going to explain you about Hibernate4 AnnotationSessionFactoryBean which is not available in Hibernate 4. As a part of migration process of your Hibernate 3 application to Hibernate 4 you have to make necessary changes in your configuration files. This tutorial explains you how to update from Hibernate 3 to Hibernate 4.

Here is the video tutorial of: "How to use Hibernate 4 AnnotationSessionFactoryBean?"

If you use the following code in your Hibernate 4 application it will give you the error. Code used in Spring 3:

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

Application may give the following error:

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

So, you have to use the org.springframework.orm.hibernate4.LocalSessionFactoryBean instead of AnnotationSessionFactoryBean. So this change is required as a process of migration to Hibernate 4.

In the Hibernate 4 the CacheProvider-related interfaces and classes has been removed. Now the RegionFactory related cache interfaces are available for secondary level caching.

Here is the complete code of using the LocalSessionFactoryBean instead of AnnotationSessionFactoryBean in Hibernate 4 applications:

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

You can include the above code in your application configuration file.

More tutorials of Hibernate 4: