Spring ORM
Spring ORM
—-----------------
The main intention of the Spring ORM module is to integrate ORM
implementations to the Spring applications.
EX:
Configuration configuration = new Configuration();
configuration.configure();
SessionFactory sessionfactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Employee emp = new Employee();
emp.setEno(111);
emp.setEname(“Durga”);
emp.setEsal(50000.0d);
emp.setEaddr(“Hyd”);
session.save(emp);
tx.commit();
session.close();
sessionFactory.close();
1. Create a Maven Project with all the Spring and Hibernate dependencies.
2. Create a Bean / POJO class.
3. Create a Hibernate mapping File.
4. Create a DAO interface.
5. Create DAO implementation class
6. Create Spring Configuration File.
7. Create Test application
1. Create a Maven Project with all the Spring and Hibernate dependencies:
—--------------------------------------------------------------------------
To prepare Spring ORM applications we have to use the dependencies.
1. Hibernate3-core
2. MySQL Driver
3. Javassist
4. Spring-orm
5. Spring-Context
}
@Transactional
Public Employee search(Employe emp){
Employee emp = (Employee)hibernateTemplate.get(emp);
}
@Transactional
public String update(Employee emp){
hibernateTemplate.update(emp);
}
@Transactional
public String delete(Employee emp){
hibernateTemplate.delete(emp);
}
}
<beans>
<tx:annotation-driven/>
<bean name=”dataSource” class=”....DriverManagerDataSource”>
<property name=”driverClassName” value=”--”/>
<property name=”url” value=”---”/>
<property name=”userName” value=”---”/>
<property name=”password” value=”--”/>
</bean>
<bean name=”sessionFactory” class=”....LocalSessionFactoryBean”>
<property name=”dataSource” ref=”dataSource”/>
<property name=”mappingResources”>
<list>
<value>Employee.hbm.xml</value>
</list>
</property>
<property name=”hibernateProperties”>
<props>
<prop key=”hibernate.dialect”>..MySQLDialect</prop>
<prop key=”hibernate.show_sql”>true</prop>
</props>
</property>
</bean>
<bean name=”transactionManager” class=”...HibernateTransactionManager”>
<property name=”sessionFactory” ref=”sessionFactory”/>
</bean>
<bean name=”hibernateTemplate” class=”....HibernateTemplate”>
<property name=”sessionFactory” ref=”sessionFactory”/>
</bean>
<bean name=”employeeDao” class=”EmployeeDao”>
<property name=”hibernateTemplate” ref=”hibernateTemplate”/>
</bean>
</beans>
EX:
—--
Employee.java
package com.durgasoft.beans;
EmployeeService.java
package com.durgasoft.service;
import com.durgasoft.beans.Employee;
EmployeeServiceImpl.java
package com.durgasoft.service;
import com.durgasoft.beans.Employee;
import com.durgasoft.dao.EmployeeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("employeeService")
public class EmployeeServiceImpl implements EmployeeService{
@Autowired
private EmployeeDao employeeDao;
@Override
public String addEmployee(Employee employee) {
String status = employeeDao.add(employee);
return status;
}
@Override
public Employee searchEmployee(int eno) {
Employee employee = employeeDao.search(eno);
return employee;
}
@Override
public String updateEmployee(Employee employee) {
String status = employeeDao.update(employee);
return status;
}
@Override
public String deleteEmployee(Employee employee) {
String status = employeeDao.delete(employee);
return status;
}
}
EmployeeDao.java
package com.durgasoft.dao;
import com.durgasoft.beans.Employee;
EmployeeDaoImpl.java
package com.durgasoft.dao;
import com.durgasoft.beans.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
@Repository
public class EmployeeDaoImpl implements EmployeeDao{
@Autowired
private HibernateTemplate hibernateTemplate;
@Transactional
@Override
public String add(Employee employee) {
String status = "";
try{
int pkVal = (int) hibernateTemplate.save(employee);
if(pkVal == employee.getEno()){
status = "SUCCESS";
}else{
status = "FAILURE";
}
}catch (Exception exception){
exception.printStackTrace();
}
return status;
}
@Override
public Employee search(int eno) {
Employee employee = hibernateTemplate.get(Employee.class,
eno);
return employee;
}
@Transactional
@Override
public String update(Employee employee) {
String status = "";
try {
hibernateTemplate.update(employee);
status = "SUCCESS";
}catch (Exception exception){
status = "FAILURE";
exception.printStackTrace();
}
return status;
}
@Transactional
@Override
public String delete(Employee employee) {
String status="";
try{
hibernateTemplate.delete(employee);
status = "SUCCESS";
}catch(Exception exception){
status = "FAILURE";
exception.printStackTrace();
}
return status;
}
}
Main.java
package com.durgasoft;
import com.durgasoft.beans.Employee;
import com.durgasoft.dao.EmployeeDao;
import com.durgasoft.service.EmployeeService;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.BufferedReader;
import java.io.InputStreamReader;
System.out.println("--------------------------------------------");
BufferedReader bufferedReader = new BufferedReader(new
InputStreamReader(System.in));
String status = "";
int eno = 0;
String ename = "";
float esal = 0.0f;
String eaddr = "";
Employee employee = null;
while (true) {
System.out.println();
System.out.println("1. ADD Employee");
System.out.println("2. SEARCH Employee");
System.out.println("3. UPDATE Employee");
System.out.println("4. DELETE Employee");
System.out.println("5. EXIT");
System.out.print("Your Option : ");
int userOption =
Integer.parseInt(bufferedReader.readLine());
switch (userOption){
case 1:
System.out.println("Employee ADD Module");
System.out.println("--------------------------");
System.out.print("Employee Number : ");
eno =
Integer.parseInt(bufferedReader.readLine());
employee =
employeeService.searchEmployee(eno);
if(employee == null) {
System.out.print("Employee Name : ");
ename = bufferedReader.readLine();
System.out.print("Employee Salary :
");
esal =
Float.parseFloat(bufferedReader.readLine());
System.out.print("Employee Address :
");
eaddr = bufferedReader.readLine();
employee = new Employee();
employee.setEno(eno);
employee.setEname(ename);
employee.setEsal(esal);
employee.setEaddr(eaddr);
status =
employeeService.addEmployee(employee);
System.out.println("Status : " +
status);
}else{
System.out.println("Status : Employee
Existed Already");
}
break;
case 2:
System.out.println("Employee SEARCH Module");
System.out.println("----------------------------");
System.out.print("Employee Number : ");
eno =
Integer.parseInt(bufferedReader.readLine());
employee =
employeeService.searchEmployee(eno);
if(employee == null){
System.out.println("Status : Employee
Does Not Exist");
}else{
System.out.println("Employee Details");
System.out.println("------------------------");
System.out.println("Employee Number :
"+employee.getEno());
System.out.println("Employee Name :
"+employee.getEname());
System.out.println("Employee Salary :
"+employee.getEsal());
System.out.println("Employee Address :
"+employee.getEaddr());
}
break;
case 3:
System.out.println("Employee UPDATE Module");
System.out.println("-----------------------------");
System.out.print("Employee Number : ");
eno =
Integer.parseInt(bufferedReader.readLine());
employee =
employeeService.searchEmployee(eno);
if(employee == null){
System.out.println("Status : Employee
Does Not Exist");
}else{
System.out.print("Employee Name : Old :
"+employee.getEname()+" New : ");
ename = bufferedReader.readLine();
String newEname =
ename==null||ename.equals("")? employee.getEname() : ename;
status =
employeeService.updateEmployee(employee1);
System.out.println("Status :
"+status);
}
break;
case 4:
System.out.println("Employee DELETE Module");
System.out.println("---------------------------");
System.out.print("Employee Number : ");
eno =
Integer.parseInt(bufferedReader.readLine());
employee =
employeeService.searchEmployee(eno);
if(employee == null){
System.out.println("Status : Employee
Does Not Exist");
}else{
status =
employeeService.deleteEmployee(employee);
System.out.println("Status : "+status);
}
break;
case 5:
System.out.println("*******Thank You for Using
Employee Management System Application*******");
System.exit(0);
break;
default:
System.out.println("Wrong Entry, please enter
the number from 1,2,3,4 and 5");
break;
}
}
} catch (Exception exception) {
exception.printStackTrace();
}
}
}
SpringConfig.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd ">
<tx:annotation-driven/>
<context:component-scan base-package="com.durgasoft.*"/>
<bean name="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="com.mysql.cj.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost:3300/durgadb"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean name="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>Employee.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop
key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
</props>
</property>
</bean>
<bean name="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager
">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean name="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean name="employeeDao"
class="com.durgasoft.dao.EmployeeDaoImpl"/>
</beans>
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.durgasoft</groupId>
<artifactId>springormapp01</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.10.Final</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.1.GA</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.31</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.30.RELEASE</version>
</dependency>
<!--
https://mvnrepository.com/artifact/org.springframework/spring-orm -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.3.30.RELEASE</version>
</dependency>
</dependencies>
</project>