Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • Best way to configure hibernate 3 with spring?

    • 0
    • 1
    • 1
    • 2
    • 0
    • 0
    • 0
    • 0
    • 997
    Comment on it

    Different way to configure hibernate 3 with spring?

    As per my knowledge There are 3 different way to configure Hibernate 3 with spring 3. Let us sort discuss on each of three configuration method and lastly conclude witch method is best.

    Method - 1

    Create your concrete DAO class and extend HibernateDaoSupport which is provided by spring 3. This base class has to be configured with a SessionFactory and gives access to Hibernate Sessions and a HibernateTemplate.

    Method - 2

    The second method is Directly injecting a HibernateTemplate which is provided by spring 3 into your DAO class. You don't have to extend any framework class. The HibernateTemplate gives access to many general purpose DAO methods. On the other side the HibernateTemplate is handy for automatic handling of the hibernate session management (e.g. binding the session to the current Transaction) but now in Hibernate 3 this is no longer needed as Hibernate 3 also provide this facility. HibernateTemplate also converts any Hibernate or SQL related Exception into a corresponding DataAccessException, which is very helpful.

    Method- 3

    Third method is directly injecting the Hibernate SessionFactory into your DAO class. Your DAO won't have dependancies on any class from the Spring Framework. Since Hibernate 3 is capable of binding the Session to a preset Context (e.g. Thread or Transaction) the HibernateTemplate is no longer needed.


    I think Method- 3 is the best way to configure hibernate 3 with spring, because in Method 2 and Method 3 our DAO or our application is tightly bounded with spring helper classes like HibernateTemplate and HibernateDaoSupport but in Method 3 our DAO or our application is depended on only hibernate 3 APIs.


    Now question arises if we use Method 3 for configuration then how data base specific exception can be converted to DataAccessExceptions which HibernateTemplate automatically convert.The answer is very simple just annotate your DAOs with @Repository and include this Spring Bean into you ApplicationContext.


    As of Hibernate 3.0.1, transactional Hibernate access code can also be coded in plain Hibernate style. Hence, for newly started projects, consider adopting the standard Hibernate3 style of coding data access objects instead, based on SessionFactory.getCurrentSession().

    NOTE :

    Spring Document says not use HibernateDaoSupport and HibernateTemplate. Please reffer following URL for more on this

    URL :

    Ex: Configuring Hibernate 3 with Spring by using method - 3

      DAO Layer
        public class CatrgoryDaoImpl implements CategoryDao
                private SessionFactory sessionFactory;
                    public Collection loadCatogery(String param)
                             Session session  =  sessionFactory.getCurrentSession();
                            return session.createQuery("<HQL query>") ).list();
        Xml Layer
          <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
            <property name="url" value="jdbc:hsqldb:hsql://localhost:9090"/>
            <property name="username" value=""/>
            <property name="password" value=""/>
          <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <property name="mappingResources">
            <property name="hibernateProperties">

 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: