Java

Saturday, 2 February 2013

spring data jpa onetomany example

spring data jpa onetomany example:
-----------------------------------



     JAVA-SERVLETS     JAVA-JDBC     JAVA-JSP       HIBERNATE-SHCEMABASED 

    HIBERNATE-ANNOTATIONS     SPRING-IOC       SPRING –AOP-SCHEMABASED   

    SPRING-AOP-ANNOTATIONS      SPRING -DAO     SPRIN-MVC     SPRING-SECUTITY

     SPRING-DATA-JPA     REST-WEB-SERVICE     STRUTS2HIBERNATE    GWT.... 





NOTE:   1)  maven dependeccy pom.xml  , we can get from previous example.
               2) presestance.xml , beans.xml file also we can get from my previous example program.



Dept .java
----------------



package repository;

import java.util.HashSet;


import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.springframework.data.jpa.domain.AbstractPersistable;

@Entity
@Table(name="dept")
public class Dept {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long deptno;
private String dname;
private String loc;
@OneToMany(mappedBy = "dept")
private Set<Employee> employees=new HashSet<Employee>(0);
public Long getDeptno() {
return deptno;
}

public Set<Employee> getEmployees() {
return employees;
}

public void setEmployees(Set<Employee> employees) {
this.employees = employees;
}

public void setDeptno(Long deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}

}


Employee .java
---------------




package repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
public interface DeptRepository extends JpaRepository<Dept, Long>,JpaSpecificationExecutor<Dept>{

}



Employee .java
----------------


package repository;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.springframework.data.jpa.domain.AbstractPersistable;
@Entity
@Table(name="employee2")
public class Employee extends AbstractPersistable<Long>{

private String ename;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "deptno")
private Dept dept;
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
public String getEname() {
      return ename;
}
public void setEname(String ename) {
      this.ename = ename;
}
}




EmployeeRepository .java
--------------------------


package repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

public interface EmployeeRepository extends JpaRepository<Employee, Long>,
JpaSpecificationExecutor<Employee>{

}


TestCase .java
---------------



package madhav;
import repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class TestCase {

@Autowired
    private DeptRepository DeptRepository;
@Autowired
private EmployeeRepository EmployeeRepository;
 
    @Transactional
public void createEmployee() {
       
         try
         {
       
        Dept dept = new Dept();
             dept.setDname("hugo");
             dept.setLoc("hyd");

                DeptRepository.save(dept);
     
       Employee emp1 = new Employee();
       emp1.setEname("madhav");
       emp1.setDept(dept);

       Employee emp2 = new Employee();
       emp2.setEname("narendar");
       emp2.setDept(dept);
       EmployeeRepository.save(emp1);
       EmployeeRepository.save(emp2);

         }
         catch(Exception ex)
         {
       
         }
}

public DeptRepository getDeptRepository() {
return DeptRepository;
}

public void setDeptRepository(DeptRepository deptRepository) {
DeptRepository = deptRepository;
}

public EmployeeRepository getEmployeeRepository() {
return EmployeeRepository;
}

public void setEmployeeRepository(EmployeeRepository employeeRepository) {
EmployeeRepository = employeeRepository;
}

}




Test.java
---------------




package madhav;

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

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");

Object tc=(Object)ctx.getBean("testCase");
TestCase g=(TestCase)tc;
g.createEmployee();
}

}





output from console:
----------------------




output from sql prompt:
------------------------


TUTIORALS FROM MADHAV:

     JAVA-SERVLETS     JAVA-JDBC     JAVA-JSP       HIBERNATE-SHCEMABASED 

    HIBERNATE-ANNOTATIONS     SPRING-IOC       SPRING –AOP-SCHEMABASED   

    SPRING-AOP-ANNOTATIONS      SPRING -DAO     SPRIN-MVC     SPRING-SECUTITY

     SPRING-DATA-JPA     REST-WEB-SERVICE     STRUTS2HIBERNATE    GWT....  




































5 comments:

  1. NICE BLOG!!! Good writing is something I can appreciate. You have made your points in a smart way. I am impressed with how interesting you have been able to present this content. Thanks for sharing a valuable information.
    iim calcutta distance learning

    ReplyDelete
  2. Thanks for sharing this article.
    Computer Classes NY

    ReplyDelete
  3. Shree Ram Techno Solutions Provides CCTV Camera, Security Camera, Wireless Security, Attendance System, Access Control System, DVR, NVR, Spy Camera, Fire Alarm, Security Alarm, PCI, IP Network Camera, Dome Camera, IR Camera, CCTV, Camera Price, HIKVISION, SCATI, Time Machine

    CCTV CAmera in jaipur at Rajasthan
    Home security system in jaipur
    Wireless Home Security System in jaipur
    Realtime attendance machine in jaipur
    cctv camera dealer in jaipur
    Hikvision DVR in jaipur at Rajasthan
    security system solutions in jaipur

    ReplyDelete