Hibernate如何删除父记录并将子设置为null

时间:2015-04-23 13:04:04

标签: hibernate java-ee

我在尝试删除员工记录引用它的员工记录时收到错误。我想在删除经理记录时将下级MANAGER列设置为null。

这是错误:

Caused by: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [project.to.Employee#26]
    at org.hibernate.engine.internal.StatefulPersistenceContext.checkUniqueness(StatefulPersistenceContext.java:697)
    at org.hibernate.event.internal.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:119)
    at org.hibernate.event.internal.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:74)
    at org.hibernate.internal.SessionImpl.fireDelete(SessionImpl.java:957)
    at org.hibernate.internal.SessionImpl.delete(SessionImpl.java:936)
    at project.dao.EmployeeDAOImpl.delete(EmployeeDAOImpl.java:73)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155)

这是我的员工班。

Employee.java
@Entity
@Table(name = "EMPLOYEE")
public class Employee implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID")
    private String ID;

    @Column(name = "NAME")
    private String name;

    @Column(name = "EMAIL")
    private String email;

    @ManyToOne
    @JoinColumn(name = "MANAGER")
    private Employee manager

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "manager")
    @Cascade(value = { org.hibernate.annotations.CascadeType.DETACH })
    private List<Employee> subordinates = new ArrayList<Employee>();

    ...
}

这是我的删除方法:

public void delete(Employee[] ems) {
        if (ems!= null && ems.length > 0) {
            for (int i = 0; i < ems.length; i++) {
                Employee em= ems[i];
                // Get the subordinates
                List<Employee> subList = em.getSubordinates();
                em.setSubordinates(null);
                // Remove this employee
                getEmployeeDAO().delete(em);
                if(subList.size() > 0){
                    for (int j = 0; j < subList.size(); j++) {
                        // Update the subordinate MANAGER field to null
                        Employee sub = subList.get(j);
                        sub.setManager(null);
                        getEmployeeDAO().update(sub);
                    }
                }               
            }
        }
    }

这是我的EmployeeDAO删除和更新

public class EmployeeDAOImpl extends IframeHibernatePaginationDaoSupport implements EmployeeDAO {
@Override
public Employee update(Employee em) {
    Employee updateEM = (Employee) getCurrentSession().get(
            Employee.class, em.getID());
    BeanUtils.copyProperties(em, updateEM);
    getCurrentSession().update(updateEM);
    return updateEM;
}

@Override
public void delete(Employee em) {
    getCurrentSession().delete(em);
}

0 个答案:

没有答案