Hibernate Template
What is hibernate template ?
1 ) The Spring framework provides HibernateTemplate class, so you don't need to follow so many steps like create Configuration, BuildSessionFactory, Session, beginning and committing transaction etc. Hibernate Template class and Method make shorten the lengthy code steps in Hibernate. Makes easy the basic CURD orations and others configuring context file.
Mapping Step HibernateTemplate in the Spring Context file
1) Create data source
2) Configure LocalSessionFactoryBeans
a) Configure data source
b) Configure Mapping resources(hbm.xml file) like employee.hbm.xml
c) Configure HibernateProperties like dilect ,update etc
d) Configure HibernateTemplate
Here is an example of applicationContext.xml which describe the how we
configure and use hibernate in spring. It’s also explains use of Template
applicationContext.xml
------------------------------------------
- <?xml version="1.0" encoding="UTF-8"?>
- <beans
- xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
-
-
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
- <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
- <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"></property>
- <property name="username" value="system"></property>
- <property name="password" value="oracle"></property>
- </bean>
-
- <bean id="mysessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource" ref="dataSource"></property>
-
- <property name="mappingResources">
- <list>
- <value>employee.hbm.xml</value>
- </list>
- </property>
-
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
- <prop key="hibernate.hbm2ddl.auto">update</prop>
- <prop key="hibernate.show_sql">true</prop>
-
- </props>
- </property>
- </bean>
-
- <bean id="template" class="org.springframework.orm.hibernate3.HibernateTemplate">
- <property name="sessionFactory" ref="mysessionFactory"></property>
- </bean>
-
- <bean id="d" class="com.blsoft.EmployeeDao">
- <property name="template" ref="template"></property>
- </bean>
-
-
- </beans>
In Java class we can use EmployeeDao for easy CURD opration using HibernateTemplate. Here assuming EmployeeDao and hbm files are defined.
Hibernate Template
Reviewed by Mukesh Jha
on
2:49 AM
Rating:
No comments:
Add your comment