Followers

springdao : Spring and Hibernate Integration





Employee.java
=========

package mypack;

public class Employee {
   
private int id; 
private String name; 
private int salary;

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public int getSalary() {
        return salary;
    }

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

EmployeeDAO.java
============

package mypack; 
import org.springframework.orm.hibernate4.HibernateTemplate; 
import java.util.*; 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
public class EmployeeDao { 
private HibernateTemplate template;
@Autowired
public void setTemplate(HibernateTemplate template) { 
    this.template = template; 
}  
//method to save employee
@Transactional(readOnly=false)
public void saveEmployee(Employee e){ 
    template.save(e); 
//method to update employee 
@Transactional(readOnly=false)
public void updateEmployee(Employee e){
    template.update(e);
//method to delete employee 
@Transactional(readOnly=false)
public void deleteEmployee(Employee e){ 
    template.delete(e); 
//method to return one employee of given id 
public Employee getById(int id){ 
    Employee e=(Employee)template.get(Employee.class,id); 
    return e; 
//method to return all employees 
public List<Employee> getEmployees(){ 
    List<Employee> list=new ArrayList<Employee>(); 
    list=template.loadAll(Employee.class); 
    return list; 

applicationContext.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:p="http://www.springframework.org/schema/p"
    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-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> 
  <context:annotation-config />
  <context:component-scan base-package="mypack" />
    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" >
        <property name="driverClassName"
                    value="org.apache.derby.jdbc.ClientDriver"/>
        <property name="url" value="jdbc:derby://localhost:1527/MYEMP" ></property>
        <property name="username" value="app"></property>
        <property name="password" value="app"></property>
    </bean>
     
    <bean id="sessionFactory"  class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
        <property name="dataSource" ref="dataSource"></property> 
         
        <property name="mappingResources"> 
        <list> 
        <value>employee.hbm.xml</value> 
        </list> 
        </property> 
         
        <property name="hibernateProperties"> 
          <props>
              <prop key="hibernate.dialect">org.hibernate.dialect.DerbyDialect</prop>
              <prop key="hibernate.hbm2ddl.auto">update</prop>
              <prop key="cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
              <prop key="hibernate.show_sql">true</prop>
          </props>
        </property> 
    </bean>
     <tx:annotation-driven transaction-manager="txManager" />
     <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory" />
     </bean>
     
    <bean id="template" class="org.springframework.orm.hibernate4.HibernateTemplate"> 
    <property name="sessionFactory" ref="sessionFactory"></property> 
    </bean> 
     
    <bean id="d" class="mypack.EmployeeDao"> 
    <property name="template" ref="template"></property>
    </bean> 
           
    </beans> 
         
employee.hbm.xml
============
         
          <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="mypack.Employee" table="MYEMP">
       
          <id name="id"> 
          <generator class="assigned"></generator> 
          </id> 
           
          <property name="name"></property> 
          <property name="salary"></property> 
       
    </class>
</hibernate-mapping>


Test.java
======

package mypack;

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

public class Test {
    public static  void main(String[] args){
    ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    EmployeeDao dao = (EmployeeDao)ctx.getBean("d");
   // Employee emp = dao.getById(111);
    //    System.out.println(emp.getId()+" "+emp.getName()+" "+emp.getSalary());
    /*
        List<Employee> employees = dao.getEmployees();
        Iterator<Employee> itr = employees.iterator();
        while(itr.hasNext()){
            Employee e= itr.next();
            System.out.println(e.getId()+" "+e.getName()+" "+e.getSalary());
        }
*/
   // emp.setName("Bengaluru");
   // dao.updateEmployee(emp);
   // dao.deleteEmployee(emp);
   
    Employee emp = new Employee();
    emp.setId(10);emp.setName("Sreenu");emp.setSalary(20000);
    //dao.saveEmployee(emp);
    dao.deleteEmployee(emp);
    }
}




No comments:

Post a Comment