Creating HibernateTemplate by org.springframework.orm.hibernate4.LocalSessionFactoryBean

I am working with Spring 3.1 + Hibernate 4.

I created the following sessionFactory


<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource" />
    <property name="hibernateProperties">
            <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="">validate</prop>
    <property name="packagesToScan" value=""/>


As you can see the above is sessionFactory

for Hibernate 4.

I'm trying to create a fileHibernateTemplate

( org.springframework.orm.hibernate3.HibernateTemplate

) via Java, by the same waysessionFactory

, but I'm not sure how.

I've tried the following code:

public void setSessionFactory(LocalSessionFactoryBean sessionFactory) {
    this.hibernateTemplate = new HibernateTemplate(sessionFactory.getObject());


But I am getting the following error:

org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'sessionFactory' must be of type [org.springframework.orm.hibernate4.LocalSessionFactoryBean], but was actually of type [org.hibernate.internal.SessionFactoryImpl]


Could you show me how to do this?


source to share

2 answers

As from this post :

HibernateTemplate is no longer recommended for use (since hibernate 3.0.1 release) there is no added value anymore so it is not going to be there anymore for hibernation 4. Just use plain SessionFactory and use getCurrentSession method to get the current transactional session (don't use openSession! !!!) and you're good to go ...

From the javadoc of the org.springframework.orm.hibernate4 package :

Contains the SPI implementation of the Spring transaction for local Hibernate operations. This package is intentionally pretty minimal, with no template classes or the like to follow the Hibernation Guideline as closely as possible.



You don't need to extend any hibernate dao support class in the latest spring. You can inject hibernate sessionfactory directly from xml / java based config. And it also needs to be entered into the transaction box. See this link for a better understanding:



All Articles