spring data jpa onetomany example:
-----------------------------------
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:
------------------------
-----------------------------------
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.
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:
------------------------
Related topics:
6. spring data jpa onetoone
6. spring data jpa onetoone
TUTIORALS FROM MADHAV:
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.
ReplyDeleteiim calcutta distance learning
Thanks for sharing this article.
ReplyDeleteComputer Classes NY
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
ReplyDeleteCCTV 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
Learn makeup in Mumbai. Visit
ReplyDeleteBasic Makeup Classes in Mumbai
Classboat provides you Best Aviation College in Pune
ReplyDelete