Saturday, March 24, 2018

Spring ORM(Object Relational Mapping)


Spring ORM framework allows you to integrate with Hibernate, Java Persistence API (JPA), Java Data Objects (JDO) and iBATIS for resource management and data access object (DAO) implementations and other transaction strategies.

Benefits of using Spring Framework to create your ORM DAOs include:

• Easier testing: Spring IOC approach allows you to swap easily the implementations and configuration locations of Hibernates Session Factory instances etc., so that you can point your configurations to various environments without modifying the source code.
• Common data access exception: Spring instead of exposing ORM specific checked exceptions to the top level tier’s of the application, it will wraps the technology specific exceptions to a common runtime DataAccessException hierarchy.
• Integrated Transaction management: Instead of dealing with ORM technology related transactional code, it allows you to declaratively manage the transactionality using AOP Declarative transaction management tags or annotation driven @Transactional annotation.
Integrating with Hibernate

In order to use spring ORM with hibernate, you need to declare your Hibernate Language objects (HLO) using declarative mapping.hbm files are annotation your classes with hibernate annotations. In order to perform the operations using these classes, you need to declare an HibernateSessionFactory and then injects it into HibernateTemplate, recall the concept of Template JDBCTemplate which allows you to perform the JDBC operations using Template approach.
Sample code is shown below.

EmployeeHLO.java

import javax.persistance.Column;
import javax.persistance.Entity;
import javax.persistance.Id;
import javax.persistance.Table;

@Entity
@Table(name = TBLEMP)
public class EmployeeHLO {
       private long id;
       private String name;
       private double salary;
     
       @Id
       @Column(name=EMP_ID)
       public long getId() {
            return id;
       }

        public void setId(long id) {
            this.id = id;
       }

       @Column(name=EMP_NM)
       public String getName() {
            return name;
       }

        public void setName(String name) {
             this.name = name;
       }

       @Column(name=SALARY)
       public double getSalary() {
            return salary;
       }

        public void setSalary(double salary) {
                this.salary = salary;
       }
}

EmployeeDao.java

import org.springframework.orm.hibernate3.HibernateTemplate;

import com.ew.hlo.EmployeeHLO;

public class EmployeeDao {

     private HibernateTemplate hibernateTemplate;

     public EmployeeDao(HibernateTemplate hibernateTemplate) {
           this.hibernateTemplate = hibernateTemplate;
     }

     public void insert(EmployeeHLO employeeHLO) {
           hibernateTemplate.save(employeeHLO);
     }
}

EmployeeService.java

import com.ew.command.Employee;
import com.ew.dao.EmployeeDao;
import com.ew.hlo.EmployeeHLO;

public class EmployeeService {
       private EmployeeDao employeeDao;

       public void setEmployeeDao(EmployeeDao employeeDao) {
              this.employeeDao = employeeDao;
       }

       public void insert(Employee employee) {
              EmployeeHLO employeeHLO = new EmployeeHLO();
              employeeHLO.setId(employee.getId());
              employeeHLO.setName(employee.getName());
              employeeHLO.setSalary(employee.getSalary());
              employeeDao.insert(employeeHLO);
       }
}

persistence-beans.xml

<bean id=”dataSource”
      class=”org.springframework.jdbc.datasource.DriverManagerDataSouce”>
      <property name=”driverClassName” value=”com.microsoft.sqlserver.jdbc.SQLServerDriver”/>
      <property name=”url” value=”jdbc:sqlserver://localhost:1433;databaseName=spdb”/>
      <property name=”username” value=”sa”/>
      <property name=”password” value=”welcome”/>
</bean>

<bean id=”SessionFactory”
      class=”org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean”>
      <property name=”dataSource” ref=”dataSource”/>
      <property name=”annotatedClasses”>
          <list>
               <value>com.ew.hlo.EmployeeHLO</value>
          </list>
      </property>
      <property name=”hibernateProperties”>
           <props>
              <prop key=”hibernate.dialect”>org.hibernate.dialect.SQLServerDialect</prop>
              <prop key=”show_sql”>true</prop>
           </props>
      </property>
</bean>

<bean id=”hibernateTemplate”
      class=”org.springframework.orm.hibernate3.HibernateTemplate”>
      <property name=”sessionFactory” ref=”sessionFactory”/>
</bean>

<bean id=”transactionManager”
      class=”org.springframework.orm.hibernate3.HibernateTransactionManager”>
      <property name=”sessionFactory” ref=”sessionFactory”/>
</bean>

<bean id=”employeeDao” class=”com.ew.dao.EmployeeDao”>
      <constructor-arg ref=”hibernateTemplate”/>
</bean>


aop-beans.xml

<aop:config>
     <aop:pointcut expression=”execution(* com.ew.service.EmployeeService.*(..))”
     <aop:advisor advice-ref=”empTxAdvice” pointcut-ref=”empServicePC”/>
</aop:config>
<tx:advice id=”empTxAdvice” transaction-manager=”transactionManager”>
      <tx:attributes>
            <tx:method name=”insert*” read-only=”false” propagation=”REQUIRED”/>
      </tx:attributes>
</tx:advice>

FloydsTriangle Java Programe
Java Assertions
SpringJdbc Insert
Mapping SQL Operations as Sub classes In Spring
NamedParameterJDBCTemplate In Spring
Spring JDBCTemplate
Data Source Set Up In Spring

Thanks for reading. If you like this post please follow us for more updates about technology related updates.

No comments: