Home  >  Spring Core

HibernateTemplate in Spring

By Arvind Rai, February 09, 2013
HibernateTemplate is the class of org.springframework.orm.hibernate3. HibernateTemplate provides the integration of hibernate and spring. Spring manages database connection DML, DDL etc commands by itself. HibernateTemplate has the methods like save, bulkUpdate, delete, evict etc. Letís understand how to configure HibernateTemplate in our spring application.
Add xml configuration in application.xml of spring application.
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
	<property name="sessionFactory">
	  <ref bean="sessionFactory" />
	</property>
</bean>
 
hibernateTemplate will be used in the dao classes. Initialize hibernateTemplate. Provide a setter method to set hibernateTemplate by spring.
    private HibernateTemplate hibernateTemplate;

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	public void persist(){
        User u1= new User(1,"Ankita");
        hibernateTemplate.save(u1);
        
        User u2= new User(2,"Renu");
        hibernateTemplate.save(u2);
        
	}
 
Use hibernateTemplate for the data fetching or saving.

Download Source Code

spring_hibernate_integration_example.zip
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS


©2019 concretepage.com | Privacy Policy | Contact Us