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. CREATE TABLE Employee( EMPID INT NOT NULL AUTO_INCREMENT, NAME VARCHAR(20) NOT NULL, AGE INT NOT NULL, SALARY BIGINT NOT NULL, PRIMARY KEY (ID) );
package com.dineshonjava.sdnext.domain; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; /** * @author Dinesh Rajput * */ @Entity @Table(name="Employee") public class Employee { @Id @Column(name="EMPID") @GeneratedValue(strategy=GenerationType.AUTO) private int empid; @Column(name="NAME") private String name; @Column(name="AGE") private int age; @Column(name="SALARY") private long salary; /** * @return the empid */ public int getEmpid() { return empid; } /** * @param empid the empid to set */ public void setEmpid(int empid) { this.empid = empid; } /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } /** * @return the age */ public int getAge() { return age; } /** * @param age the age to set */ public void setAge(int age) { this.age = age; } /** * @return the salary */ public long getSalary() { return salary; } /** * @param salary the salary to set */ public void setSalary(long salary) { this.salary = salary; } public String toString(){ return "EMPLOYEE{empid- "+this.empid+" name- "+this.name+ " age- "+this.age+" salary- "+this.salary+"}"; } }
<beans xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:security="http://www.springframework.org/schema/security" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.4.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <context:annotation-config></context:annotation-config> <context:component-scan base-package="com.dineshonjava.sdnext.dao.impl"> </context:component-scan> <!-- Creating data source --> <bean class="org.apache.commons.dbcp.BasicDataSource" id="dataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/DAVDB"></property> <property name="username" value="root"></property> <property name="password" value="root"></property> <property name="initialSize" value="2"></property> <property name="maxActive" value="5"></property> </bean> <!-- Creating session factory --> <bean class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" id="sessionFactory"> <property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> <prop key="hibernate.show_sql">true</prop> </props> </property> <property name="annotatedClasses"> <list> <value> com.dineshonjava.sdnext.domain.Employee </value> </list> </property> </bean> <bean class="com.dineshonjava.sdnext.dao.impl.EmployeeDaoImpl" id="employeeDaoImpl"> <property name="sessionFactory" ref="sessionFactory"> </property></bean> </beans>
package com.dineshonjava.sdnext.dao; import java.util.List; import com.dineshonjava.sdnext.domain.Employee; /** * @author Dinesh Rajput * */ public interface EmployeeDao { /** * This is the method to be used to create * a record in the Employee table. */ void createEmployee(Employee employee); /** * This is the method to be used to list down * a record from the Employee table corresponding * to a passed Employee id. */ Employee getEmployee(Integer empid); /** * This is the method to be used to list down * all the records from the Employee table. */ List listEmployees(); /** * This is the method to be used to delete * a record from the Employee table corresponding * to a passed Employee id. */ void delete(Employee employee); /** * This is the method to be used to update * a record into the Employee table. */ void update(Employee employee); }
package com.dineshonjava.sdnext.dao.util; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; /** * @author Dinesh Rajput * */ public abstract class SuperHibernateDaoSupport extends HibernateDaoSupport { }
package com.dineshonjava.sdnext.dao.impl; import java.util.List; import org.springframework.stereotype.Repository; import com.dineshonjava.sdnext.dao.EmployeeDao; import com.dineshonjava.sdnext.dao.util.SuperHibernateDaoSupport; import com.dineshonjava.sdnext.domain.Employee; /** * @author Dinesh Rajput * */ @Repository public class EmployeeDaoImpl extends SuperHibernateDaoSupport implements EmployeeDao { @Override public void createEmployee(Employee employee) { getHibernateTemplate().saveOrUpdate(employee); } @Override public Employee getEmployee(Integer empid) { return (Employee) getHibernateTemplate().get(Employee.class, empid); } @Override public ListStep 5: Creating Application Class for using thislistEmployees() { return getHibernateTemplate().find("FROM EMPLOYEE"); } @Override public void delete(Employee employee) { getHibernateTemplate().delete(employee); } @Override public void update(Employee employee) { getHibernateTemplate().saveOrUpdate(employee); } }
package com.dineshonjava.sdnext.main; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.dineshonjava.sdnext.dao.EmployeeDao; import com.dineshonjava.sdnext.domain.Employee; /** * @author Dinesh Rajput * */ public class EmpMainApp { /** * @param args */ public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); EmployeeDao empDao = (EmployeeDao) context.getBean("employeeDaoImpl"); Employee employee = new Employee(); employee.setName("Dinesh"); employee.setAge(25); employee.setSalary(50000l); System.out.println("------Records Creation--------" ); empDao.createEmployee(employee); System.out.println("------Listing Multiple Records--------" ); List employees = empDao.listEmployees(); for (Employee emp: employees) { System.out.print(emp); } System.out.println("------find one Records--------" ); Employee employee = empDao.getEmployee(3); System.out.print(employee); System.out.println("------Delete one Records--------" ); empDao.delete(employee); } }
Labels: Spring3.0