Tuesday, March 1, 2011

How to integrate Spring and Hibernate using HibernateDaoSupport


What are the ways to access Hibernate using Spring
There are two approaches to spring’s Hibernate integration:
1.      Inversion of Control with a HibernateTemplate and Callback
2.      Extending HibernateDaoSupport and Applying an AOP Interceptor

How to integrate Spring and Hibernate using HibernateDaoSupport case 2
Spring and Hibernate can integrate using Spring’s SessionFactory called LocalSessionFactory. The integration process is of 3 steps.
  • Configure the Hibernate SessionFactory
  • Extend your DAO Implementation from HibernateDaoSupport
  • Wire in Transaction Support with AOP
1. mapping with the table in the .hbm.xml file
2. <bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource">
              <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
              <property name="url" value="jdbc:mysql://localhost:3306/ims"/>
              <property name="username" value="root"/>
              <property name="password" value="root"/>                  
     </bean>
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
    <property name="dataSource" ref="dataSource"/>
    <property name="mappingResources">
        <list>
                               <value>/po/users.hbm.xml</value>
            <value>/po/messages.hbm.xml</value>
            <value>/po/keywords.hbm.xml</value>
            <value>/po/pastsessioninfo.hbm.xml</value>
                  </list>
             </property>
            <property name="hibernateProperties" value="hibernate.dialect=org.hibernate.dialect.MySQLDialect"/>
     </bean>
3. the dao class should implement HibernateDaoSupport 

No comments:

Post a Comment