I am new in Hibernate currently want to implement the Hibernate Template classes , any one please tell me about the Hibernate Template classes.
xml file
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost/test" /> <property name="username" value="root" /> <property name="password" value="mnrpass" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mappingResources"> <list> <value>employee.hbm.xml</value> </list> </property> <property name="configLocation" value="classpath:hibernate.cfg.xml" /> </bean> <bean id="springHibernateExample" class="com.javarticles.spring.hibernate.SpringHibernateExample"> <property name="sessionFactory" ref="sessionFactory" /> </bean> </beans>
2 Answers
Answers 1
Copied from Hibernate Interview Questions:
Hibernate Template
When Spring and Hibernate integration started, Spring ORM provided two helper classes –
HibernateDaoSupport
andHibernateTemplate
. The reason to use them was to get theSession
from Hibernate and get the benefit of Spring transaction management. However from Hibernate 3.0.1, we can useSessionFactory getCurrentSession()
method to get the current session and use it to get the spring transaction management benefits. If you go through above examples, you will see how easy it is and that’s why we should not use these classes anymore.One other benefit of HibernateTemplate was exception translation but that can be achieved easily by using
@Repository
annotation with service classes, shown in above spring mvc example. This is a trick question to judge your knowledge and whether you are aware of recent developments or not.
Answers 2
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,update, delete etc. Try to 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>
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); }
0 comments:
Post a Comment