JPA级连删除
雇员(Employee)表
部门表(Department)表
雇员对部门为1对多的关系。
要求,删除雇员不影响部门,删除部门则同步删除且部门下的所有雇员
import java.util.Date;
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.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
/**
* 员工实体类
* @author Administrator
*
*/
@Entity
@Table(name="employee")
public class Employee{
@Id
@Column(name="emp_id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer empId; //员工id
@Column(name="emp_name",nullable = false)
private String empName; //员工名称
@Column(name="emp_officePhone",nullable = false)
private String officePhone; //联系电话
@Column(name="emp_mobilePhone",nullable = false)
private String mobilePhone; //办公电话
@Temporal(TemporalType.DATE)
@Column(name = "emp_entryDate",nullable = false)
private Date entryDate; //入职日期
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "addr_id")
private Address homeAddress; //员工地址
@ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER, optional = false)
@JoinColumn(name="dept_id")
private Department department; //员工所在部门
public Integer getEmpId() {
return empId;
}
public void setEmpId(Integer empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getOfficePhone() {
return officePhone;
}
public void setOfficePhone(String officePhone) {
this.officePhone = officePhone;
}
public String getMobilePhone() {
return mobilePhone;
}
public void setMobilePhone(String mobilePhone) {
this.mobilePhone = mobilePhone;
}
public Date getEntryDate() {
return entryDate;
}
public void setEntryDate(Date entryDate) {
this.entryDate = entryDate;
}
public Address getHomeAddress() {
return homeAddress;
}
public void setHomeAddress(Address homeAddress) {
this.homeAddress = homeAddress;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
}
import java.util.HashSet;
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.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import org.codehaus.jackson.annotate.JsonIgnore;
/**
* 部门实体类
* @author Administrator
*
*/
@Entity
@Table(name="depart")
public class Department{
@Id
@Column(name="dept_id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@Column(name="dept_name",nullable = false)
private String deptName; //部门名称
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "loca_id")
private Location deptLocation; //部门所在的地址
@JsonIgnore
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "department")
private Set<Employee> employees = new HashSet<Employee>();
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public Location getDeptLocation() {
return deptLocation;
}
public void setDeptLocation(Location deptLocation) {
this.deptLocation = deptLocation;
}
public Set<Employee> getEmployees() {
return employees;
}
public void setEmployees(Set<Employee> employees) {
this.employees = employees;
}
public void addEmployee(Employee employee){
employee.setDepartment(this);
this.employees.add(employee);
}
public void deleteEmployee(Employee employee){
this.employees.remove(employee);
employee.setDepartment(null);
}
public void clearEmployeeSet(){
employees.clear();
}
}
@Repository("employeeDAO")
public class EmployeeDAO extends BaseDao<Employee>{
@Override
public void delete(Employee obj) {
Employee ori = findByObject(obj);
if(null!=ori){
ori.getDepartment().deleteEmployee(ori);
getEntityManager().remove(ori);
return;
}else{
throw new DaoException("要删除的对象不存在..");
}
}
}