You are on page 1of 3

package com.eclipselink.

service;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import com.eclipselink.entity.Department;
import com.eclipselink.entity.Employee;
public class OneToMany {
public static void main(String args[]){
EntityManagerFactory emf=Persistence.createEntityManagerFactory("JPA_Ecl
ipselink_OTM");
EntityManager em=emf.createEntityManager();
em.getTransaction().begin();
Employee E1=new Employee("73910_FS","Harshita",45000,"Con");
Employee E2=new Employee("73911_FS","Vinita",40000,"AsCon");
Employee E3=new Employee("73912_FS","Deepiaka",50000,"SeniorCon");
Employee E4=new Employee("73913_FS","Shruti",60000,"LeadCon");
Employee E5=new Employee("73914_FS","Shweta",80000,"Manager");
em.persist(E1);
em.persist(E2);
em.persist(E3);
em.persist(E4);
em.persist(E5);
List<Employee> emplist = new ArrayList<Employee>();
emplist.add(E1);
emplist.add(E2);
emplist.add(E3);
emplist.add(E4);
emplist.add(E5);
Department department = new Department();
department.setName("Insurance");
department.setEmployeelist(emplist);
em.persist(department);
em.getTransaction().commit();
em.close();
emf.close();

}
}
######################################################################
package com.eclipselink.entity;
import java.util.List;
import
import
import
import
import

javax.persistence.Entity;
javax.persistence.GeneratedValue;
javax.persistence.GenerationType;
javax.persistence.Id;
javax.persistence.OneToMany;

@Entity
public class Department {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String name;
@OneToMany(targetEntity=Employee.class)
private List<Employee> employeelist;
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 List<Employee> getEmployeelist() {
return employeelist;
}
public void setEmployeelist(List<Employee> employeelist) {
this.employeelist = employeelist;
}
}
########################################################
package com.eclipselink.entity;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Employee {
@Id
private String kin_id;
private String name;
private double salary;
private String designation;
public Employee(String kin_id, String name, double salary, String design
ation) {
super();
this.kin_id = kin_id;
this.name = name;
this.salary = salary;
this.designation = designation;
}
public Employee() {
super();
// TODO Auto-generated constructor stub
}
public String getKin_id() {
return kin_id;
}
public void setKin_id(String kin_id) {
this.kin_id = kin_id;
}

public String getName() {


return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
}

You might also like