Wednesday 23 May 2012

Spring ORM Module


JDBC,Spring DAO based persistence logic is data base software dependent.

Spring ORM Module does not provide its own ORM S/W but, it provides abstract layer on the multiple ORM S/W’s like Hibernate, iBatis, JDO (Java Data Object), JPA(Java Persistent API) , Top Link

ORM Technology         Template Class
Hibernate  ------------->org.springframework.orm.hibernate.HibernateTemplate
iBatis        ------------->org.springframework.orm.ibatis.sqlMapClientTemplate
JDO(java Data Object)-> org.springframework.orm.jdbo.JdoTemplate
JPA(Java Persistant API)->rg.springframework.orm.jpa.JpaTemplate
TopLink   ------------->  org.springframework.orm.toplink.TopLinkTemplate


There are 2 approaches to develop spring with application by using spring ORM Module.

Approach 1:
By Injecting Hibernate Session Factory object to spring bean through dependency injection(but we need to write HB persistence logic by using HB api




Sample Application to Describe Approach-1(For this Application add Hibernate jar files and Spring jar files)
Table In oracle
create table emp1(empno number(3),ename varchar2(10),sal number(3));

POJO class for Hibernate(emp1.java)

public class emp1  {
private int empno,sal;
private String ename;
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public int getSal() {
return sal;
}
public void setSal(int sal) {
this.sal = sal;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
}

Hibernate Mapping File(emp1.hbm.xml)

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="emp1" table="emp1">
<id name="empno" column="empno"></id>
<property name="ename" column="ename"></property>
<property name="sal" column="sal"></property>
</class>
</hibernate-mapping>


Interface(demo.java)

import java.util.Iterator;
public interface  demo {
public Iterator getData();
public void saveobj(emp1 obj);
}


Implementations class(demoimpl.java)


import java.util.Iterator;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
public class demoimpl implements demo {
SessionFactory sf;
public SessionFactory getSf() {
return sf;
}
public void setSf(SessionFactory sf) {
this.sf = sf;
    }
public Iterator getData() {
// TODO Auto-generated method stub
Session ses=sf.openSession();
Query q=ses.createQuery("from emp1");
Iterator i1=q.iterate();
return i1;
}
public void saveobj(emp1 obj) {
// TODO Auto-generated method stub
Session ses=sf.openSession();
ses.save(obj);
org.hibernate.Transaction t=ses.beginTransaction();
t.commit();
}
}

SpringConfiguration file(Springhb.xml)

<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.xsd">
<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value></property>
<property name="url"><value>jdbc:oracle:thin:@localhost:1521:xe</value></property>
<property name="username"><value>system</value></property>
<property name="password"><value>system123</value></property>
</bean>
<bean id="mys" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource"><ref bean="ds"></ref>
</property>
<property name="mappingResources">
<list>
<value>emp1.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
</props>
</property>
</bean>
<bean id="d1" class="demoimpl">
<property name="sf">
<ref local="mys"></ref></property>
</bean>
</beans>



Client Application(ClientSelect.java)

import java.util.Iterator;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class ClientSelect {
public static void main(String[] args) {
// TODO Auto-generated method stub
XmlBeanFactory fact=new XmlBeanFactory(new ClassPathResource("springhb.xml"));
demoimpl obj=(demoimpl)fact.getBean("d1");
Iterator ie=obj.getData();
while(ie.hasNext())
{
emp1 d=(emp1)ie.next();
System.out.println("empno is "+d.getEmpno()+" ename is "+d.getEname());
}
}
}

Client Application(ClientInsert.java)(This is to store the data)

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class ClientInsert {
public static void main(String arg[])
{
XmlBeanFactory fact=new XmlBeanFactory(new ClassPathResource("springhb.xml"));
demoimpl obj=(demoimpl)fact.getBean("d1");
emp1 e=new emp1();
e.setEmpno(1);
e.setEname("abc");
e.setSal(15821);
obj.saveobj(e);
System.out.println("saved..............");
}
}




Sample Application to Describe Approach-2(For this Application add Hibernate jar files and Spring jar files)(Here We won't work with Hibernate Api)


By injecting Hibernate Template class object to Spring bean template dependency injection

Note:In approach-2  even though  we are not working with api of hibernate we need to add jar files of hibernate because HibernateTemplate class internally uses Hibernate software. For that purpose we need to add jar files..


Steps:
1. Pojo Class is same as above
2. Hibernate Mapping file is same as above
3. demo.java is same as above

4. demoimpl.java
import java.util.Iterator;

import java.util.List;
import org.springframework.orm.hibernate3.HibernateTemplate;
public class demoimpl implements demo {
 private HibernateTemplate ht;
@Override
public Iterator getData() {
// TODO Auto-generated method stub
List l=ht.find(" from emp1");
Iterator i1=l.iterator();
return i1;
}
public void setHt(HibernateTemplate ht) {
this.ht = ht;
}
public HibernateTemplate getHt() {
return ht;
}
}


5.SpringConfiguration file


<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.xsd">
<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value></property>
<property name="url"><value>jdbc:oracle:thin:@localhost:1521:xe</value></property>
<property name="username"><value>system</value></property>
<property name="password"><value>cgvak123</value></property>
</bean>
<bean id="mys" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource"><ref bean="ds"></ref>
</property>
<property name="mappingResources">
<list>
<value>emp1.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
</props>
</property>
</bean>
<bean id="t" class="org.springframework.orm.hibernate3.HibernateTemplate"><property name="sessionFactory"><ref bean="mys"></ref></property></bean>
<bean id="d1" class="demoimpl">
<property name="ht">
<ref bean="t"></ref></property>
</bean>
</beans>


6.Client.java

import java.util.Iterator;

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class ClientSelect {
public static void main(String[] args) {
// TODO Auto-generated method stub
XmlBeanFactory fact=new XmlBeanFactory(new ClassPathResource("springhb.xml"));
demoimpl obj=(demoimpl)fact.getBean("d1");
Iterator ie=obj.getData();
while(ie.hasNext())
{
emp1 d=(emp1)ie.next();
System.out.println("empno is "+d.getEmpno()+" ename is "+d.getEname());
}
}
}





No comments:

Post a Comment