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
- Configure beanRefFactory.xml in web.xml.
<context-param>
<param-name>locatorFactorySelector</param-name>
<param-value>classpath*:/properties/beanRefFactory.xml</param-value>
</context-param><context-param>
<param-name>parentContextKey</param-name>
<param-value>beanId1</param-value> // this is the bean id that gets loaded and all related
// beans.
</context-param>
- The listener goes like this in web.xml.
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
- Contents in beanRefFactory.xml
<bean id="beanId1" <!-- remember the same id we gave when configuring in web.xml -- >
class="org.springframework.context.support.ClassPathXmlApplicationContext">
<constructor-arg>
<list>
<value>/app_dataSource.xml</value>
<value>/app-dao.xml</value>
</list>
</constructor-arg>
</bean>
- Importing a spring xml into another
<import resource="app_springxml1.xml" />
<import resource="app_springxml2.xml" />
<import resource="app_springxml3.xml" />
- Configuring the PropertyPlaceholderConfigurer - A property resource configurer that resolves placeholders in bean property values of context definitions. It pulls values from a properties file into bean definitions.
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location"
value="classpath:project_sub_module.properties" />
</bean>
- Configuring HibernateTemplate
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
- Configuring sessionFactory - Properties to load datasource. etc..
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="hibernateProperties">
<props>
<prop key="hibernate.connection.datasource">jdbc/Datasource_Name</prop>
<prop key="hibernate.default_schema">DB_SCHEMA_NAME</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.dialect">
org.hibernate.dialect.OracleDialect
</prop>
</props>
</property>
</bean>
- Configuring the hbm.xml files. - 7 and 8 items will go under the same <bean> tag
<property name="mappingResources">
<list>
<value>
com/darla/properties/Test1.hbm.xml
</value>
</property>
- Last but not the least, dont forget to have an abstract class (SomeDAO.java) which extends
org.springframework.orm.hibernate3.support.HibernateDaoSupport
This is required to get support of the HibernateDaoSupport ,for example to get the instance
of HibernateTemplate class, to utilize the save() , update() and delete() methods.