?xml version="1.0" encoding="UTF-8"?
persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
persistence-unit name="myp" transaction-type="RESOURCE_LOCAL"
provider org.eclipse.persistence.jpa.PersistenceProvider /provider
class>com.emp.entity.Department
class>com.emp.entity.Employee
class>com.emp.entity.Manager
class>com.emp.entity.EmployeeManager
class>com.emp.entity.EmployeeManagerPK
properties
property name="eclipselink.jdbc.driver" value="org.postgresql.Driver" property name="eclipselink.jdbc.url" value="jdbc:postgresql://127.0.0.1:5432/employee"
property name="eclipselink.jdbc.user" value="postgres"
property name="eclipselink.jdbc.password" value="allion123"
---------------------------------------------
package com.company.entity;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
@Entity
public class Department {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private String id ;
@Column(length=25 ,nullable=false)
private String name ;
@OneToMany(fetch = FetchType.LAZY ,mappedBy = "department")
@JoinColumn(name = "department", nullable = true)
private Set
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set
return managers;
}
public void setManagers(Set
this.managers = managers;
}
}
---------------------------------------------------
package com.company.entity;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
@Entity
public class Employee {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(length=10, nullable =false)
private String id;
@Column(length=20, nullable =true)
private String name;
@Column(length=9, nullable =false)
private String nic;
/*@ManyToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
@JoinTable(name="employee_manager" ,joinColumns =@JoinColumn(name ="id") ,inverseJoinColumns=@JoinColumn(name ="mid") )
private Set
@OneToMany(fetch = FetchType.LAZY ,mappedBy = "employee_id")
@JoinColumn(name = "employee_id", nullable = true)
private Set
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNic() {
return nic;
}
public void setNic(String nic) {
this.nic = nic;
}
public Set
return em;
}
public void setEm(Set
this.em = em;
}
}
---------------------------------------------
package com.company.entity;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
@Entity
public class Manager {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private String mid;
@Column(length=20 ,nullable=false)
private String name;
@ManyToOne(fetch=FetchType.LAZY)
private Department department;
@Column(length=9 ,nullable=false)
private String nic;
/* @ManyToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY , mappedBy="managers")
private Set
@OneToMany(fetch = FetchType.LAZY ,mappedBy = "manager_id")
@JoinColumn(name = "manager_id", nullable = true)
private Set
public String getNic() {
return nic;
}
public void setNic(String nic) {
this.nic = nic;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public String getMid() {
return mid;
}
public void setMid(String mid) {
this.mid = mid;
}
public Set
return em2;
}
public void setEm2(Set
this.em2 = em2;
}
}
--------------------------------------------------------------
package com.company.entity;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
@Entity
public class EmployeeManager {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private String id;
@ManyToOne(fetch=FetchType.LAZY )
private Employee employee_id ;
@ManyToOne(fetch=FetchType.LAZY)
private Manager manager_id ;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Employee getEmployee_id() {
return employee_id;
}
public void setEmployee_id(Employee employee_id) {
this.employee_id = employee_id;
}
public Manager getManager_id() {
return manager_id;
}
public void setManager_id(Manager manager_id) {
this.manager_id = manager_id;
}
}
--------------------------------------------------
package com.emp.entity;
public class GenericEntity
{
@Override
public boolean equals(Object o)
{
if(o != null && o.getClass().equals(this.getClass()))
{
return true;
}
return false;
}
@Override
public int hashCode()
{
int prime = 31;
return prime *7/2;
}
}
-------------------------------------------------------
package com.emp.entity;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class MyEntityManager
{
private javax.persistence.EntityManager em;
private static final String PERSISTENT_UNIT ="myp";
public MyEntityManager()
{
EntityManagerFactory factoty = Persistence.createEntityManagerFactory("myp");
this.em = factoty.createEntityManager();
}
public javax.persistence.EntityManager getEm() {
return em;
}
}
--------------------------------------------------
CREATE TABLE sequence
(
seq_name character varying(50) NOT NULL,
seq_count numeric(38,0),
CONSTRAINT sequence_pkey PRIMARY KEY (seq_name )
)
WITH (
OIDS=FALSE
);
ALTER TABLE sequence
OWNER TO postgres;
--"SEQ_GEN" 0
import java.util.Set;
import com.emp.entity.Department;
import com.emp.entity.EmployeeManager;
//Data transfer object for both direction(UI to entity /entity to UI)
public class EmployeeDto {
private String empId;
private String address;
private String empName;
private double salary;
private Department department;
private Set
public EmployeeDto() {
}
public EmployeeDto(String empId,String address,double salary,Department department,Set
this.empId = empId;
this.address = address;
this.empName = address;
this.salary = salary;
this.department = department;
}
public EmployeeDto(com.emp.entity.Employee employee) {
this.empId = employee.getEmpId();
this.address = employee.getAddress();
this.empName = employee.getEmpName();
this.salary = employee.getSalary();
this.department = employee.getDepartment();
this.employeeManagers = employee.getEmployeeManagers();
}
public String getEmpId() {
return empId;
}
public void setEmpId(String empId) {
this.empId = empId;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public Set
return employeeManagers;
}
public void setEmployeeManagers(Set
this.employeeManagers = employeeManagers;
}
}
No comments:
Post a Comment