0% found this document useful (0 votes)
33 views

Spring ORM Using Hibernate Template

The document describes how to create a Spring ORM project using Hibernate. It includes code for entities, DAOs, configuration files, and a main class to demonstrate basic CRUD operations. Folder structure and dependencies are defined, along with annotations and XML configuration for the Hibernate session factory.

Uploaded by

Raghu Gowda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Spring ORM Using Hibernate Template

The document describes how to create a Spring ORM project using Hibernate. It includes code for entities, DAOs, configuration files, and a main class to demonstrate basic CRUD operations. Folder structure and dependencies are defined, along with annotations and XML configuration for the Hibernate session factory.

Uploaded by

Raghu Gowda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Spring-ORM(Hibernate) Example(Using Hibernate template):

Folder Structure:

config.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:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd
">

<context:annotation-config/>
<context:component-scan base-package="com.app"/>
<bean class="com.app.Address" name="addr123,addr124">

</bean>

<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource"
name="dataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="root"/>

</bean>

<bean
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
name="sessionFactory">
<property name="dataSource" ref="dataSource"/>
<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>
<property name="annotatedClasses">
<list>
<value>com.app.Employee</value>
</list>
</property>
</bean>

<bean class="org.springframework.orm.hibernate3.HibernateTemplate"
name="template">
<property name="sessionFactory" ref="sessionFactory"></property>

</bean>

<bean class="com.app.EmployeeDaoHibernateImpl" name="empDao">


<property name="template">
<ref bean="template"/>

</property>

</bean>
</beans>

Employee.java

package com.app;

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

@Entity
@Table(name="emp")
public class Employee {

@Id
@Column(name="eid")
private Integer empId;
@Column(name="ename")
private String empName;
@Column(name="esal")
private Double empSal;

public Employee() {
super();
}

public Employee(Integer empId, String empName, Double empSal) {


super();
this.empId = empId;
this.empName = empName;
this.empSal = empSal;
}

public Integer getEmpId() {


return empId;
}
public void setEmpId(Integer empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public Double getEmpSal() {
return empSal;
}
public void setEmpSal(Double empSal) {
this.empSal = empSal;
}
@Override
public String toString() {
return "Employee [empId=" + empId + ", empName=" + empName
+ ", empSal=" + empSal + "]";
}

}
package com.app;

import java.util.List;

public interface IEmployeeDao {


public Integer createEmployee(Employee emp);
public Integer updateEmployee(Employee emp);
public Integer deleteEmployee(Integer empId);
public Employee loadEmployee(Integer empId);
public List<Employee> getAllEmployees();

package com.app;

import java.util.List;

import org.springframework.orm.hibernate3.HibernateTemplate;

public class EmployeeDaoHibernateImpl implements IEmployeeDao {

private HibernateTemplate template;

public void setTemplate(HibernateTemplate template) {


this.template = template;
}
@Override
public Integer createEmployee(Employee emp) {
Integer id=(Integer)template.save(emp);
return id;
}

@Override
public Integer updateEmployee(Employee emp) {
template.update(emp);
return emp.getEmpId();
}

@Override
public Integer deleteEmployee(Integer empId) {
Employee emp=template.get(Employee.class, empId);
if (emp!=null) {
template.delete(emp);
}
return emp.getEmpId();
}

@Override
public Employee loadEmployee(Integer empId) {

return template.get(Employee.class, empId);


}

@Override
public List<Employee> getAllEmployees() {

return template.loadAll(Employee.class);
}

}
Main.java

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.app.Employee;
import com.app.IEmployeeDao;

public class Main {

/**
* @param args
* @author Raghu
*/
public static void main(String[] args) {

ApplicationContext context=new
ClassPathXmlApplicationContext("config.xml");
IEmployeeDao empDao=(IEmployeeDao)context.getBean("empDao");
Employee emp=new Employee(103,"ka", 16.5);
System.out.println(empDao.getAllEmployees());
}

Hibernate Download Link:

http://sourceforge.net/projects/hibernate/files/hibernate3/3.6.5.Final/hibernate-distribution-3.6.5.Final-
dist.zip/download

You might also like