Spring Framework : A Framework from SpringSource :: Spring ORM (Object Relational Mapping)
Spring and Hibernate |
- let us understand the need for such integration before we actually get into the integration between these two technologies.
- It is well known that Hibernate is a powerful ORM tool that lies between Application and Database.
- It enables Application to access data from any database in a platform-independent manner.
- There is no need for the Application to depend on the low-level JDBC details like managing connection, dealing with statements and result sets.
- All the necessary details for accessing a particular data source is easily configurable in Xml files.
- Another good thing is that Hibernate can be coupled well with both J2SE and J2EE Applications.
One of the problem with using Hibernate is that the client Application that accesses the database usingHibernate Framework has to depend on the Hibernate APIs like Configuration, SessionFactory and Session.
- These objects will continue to get scattered across the code throughout the Application.
- Moreover, the Application code has to manually maintain and manage these objects.
- In the case of Spring, the business objects can be highly configurable with the help of IOC Container.
- In simple words, the state of an object can be externalized from the Application code.
- It means that now it is possible to use the Hibernate objects as Spring Beans and they can enjoy all the facilities that Spring provides.
Problem
When using an ORM framework on its own, you have to configure its resource factory with its API. For Hibernate and JPA, you have to build a session factory and an entitymanager factory
from the native Hibernate API and JPA. In this way, you can only manage the session factory and entitymanager factory by yourself. Besides, your application cannot utilize the data
access facilities provided by Spring.
Solution
Spring provides several factory beans for you to create a Hibernate session factory or a JPA entitymanager factory as a singleton bean in the IoC container. These factories can be shared between multiple beans via dependency injection. Moreover, this allows the session factory and the entitymanager factory to integrate with other Spring data access facilities, such asdata sources and transaction managers.
Benefits using Hibernate with Spring |
Some of the benefits of using the Spring Framework to create your ORM DAOs include:
- Ease of testing.
- Common data access exceptions.
- General resource management.
- Integrated transaction management.
Understanding Hibernate |
We will start with a coverage of Hibernate 3.x in a Spring environment, using it to demonstrate the approach that Spring takes towards integrating O/R mappers.
Hibernate 3 classes and configuration.
All of this can (pretty much) be applied to Hibernate 2.x as-is, using the analogous Hibernate 2.x support package:
org.springframework.orm.hibernate, mirroring org.springframework.orm.hibernate3 with analogous support classes for Hibernate 2.x.
Note :- As of Spring 3.0, Spring requires Hibernate 3.2 or later.
SessionFactory |
SessionFactory setup in a Spring container
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="iobj" class="com.javaskool.Item">
<property name="itemId" value="1001"/>
<property name="itemName" value="Logitech Mouse"/>
<property name="qty" value="500"/>
</bean>
<em><!-- If you are having hibfernate.cfg.xml , below two line is enough to get sessionFactory-->
<!--
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>
-->
</em>
<!-- OR -->
<em><!-- You can use below code to configure data source here itself --></em>
<bean id="<em>dataSource</em>" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/javaskoolDB" />
<property name="username" value="root" />
<property name="password" value="admin" />
</bean>
<!--
Now, suppose you have a data source defined in the Spring IoC container.
If you want to use this data source for your session factory, you can inject it into the
dataSource property of LocalSessionFactoryBean.
-->
<bean id="<em>sessionFactory</em>" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref=",<em>dataSource</em>" />
<property name="mappingResources">
<list>
<value>item.hbm.xml</value> <!-- <value>com/javaskool/item.hbm.xml</value> -->
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
</beans>
<!--
The mappingResources propertys type is String[], so you can specify a set of mapping
files in the classpath. LocalSessionFactoryBean also allows you take advantage of Springs
resource-loading support to load mapping files from various types of locations. You can specify
the resource paths of the mapping files in the mappingLocations property, whose type is
Resource[].
-->
JNDI-located DataSource (usually managed by an application server) is just a matter of configuration:
<beans>
<bean id="myDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/mydsrc"/>
</bean>
</beans>
You can also access a JNDI-located SessionFactory, using Spring’s JndiObjectFactoryBean to retrieve and expose it.
HibernateTemplate |
The HibernateTemplate
public static void main(String[] args)
{
BeanFactory beans=new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
Item p=(Item)beans.getBean("iobj"); //getting Object from context
SessionFactory sf=(SessionFactory)beans.getBean("sessionFactory");
HibernateTemplate ht=new HibernateTemplate(sf);
//ht.save(p); //for insert
p.setItemId(2);
p.setQty(99999);
ht.update(p); //for update
//p.setItemId(1);
//ht.delete(p); //for delete
}
Click Below to download the Examples
Recent Comments