0

我的问题是,当我删除父子时,没有删除父子,而不是删除子,子正在更新,这是输出

Hibernate: update employee_project set employeeNumber=null where employeeNumber=?
Hibernate: delete from employee where EMPLOYEE_NUMBER=?

当我删除父级时,这是我运行的代码片段

 public boolean deleteEmployee(Employee employee) {
        Transaction transaction = null;
        boolean flag;
        try {
            transaction = session.beginTransaction();
            session.delete(employee);
            session.flush();
            transaction.commit();
            flag = true;
        } catch (HibernateException exception) {
            if (transaction != null)
                transaction.rollback();
            flag = false;
        }
        return flag;
    }

这是我的父表 Employee.hbm.xml

<hibernate-mapping package="com.nousinfo.tutorial.model">
    <class name="Employee" table="employee">
        <meta attribute="class-description">
            This class contains the employee detail
        </meta>
        <id name="employeeNumber" type="int" column="EMPLOYEE_NUMBER">

        </id>
        <property name="firstName" type="string" column="FIRST_NAME"></property>
        <property name="lastName" type="string" column="LAST_NAME"></property>
    </property>
        <set name="employeeProjects" cascade="all-delete-orphan">
            <key column="employeeNumber" />
            <one-to-many class="com.ravindrainfo.tutorial.model.EmployeeProject" />
        </set>

    </class>

</hibernate-mapping>

这是子表 project.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
 "-//Hibernate/Hibernate Mapping DTD//EN"
 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.nousinfo.tutorial.model">
    <class name="EmployeeProject" table="employee_project">
        <meta attribute="class-description">
            This class contains the employee detail
        </meta>
        <composite-id>
            <key-property name="employeeNumber" type="int"
                column="EMPLOYEE_NUMBER"></key-property>
            <key-property name="projectCode" type="string" column="PROJECT_CODE"></key-property>
        </composite-id>
        <property name="startDate" type="date" column="START_DATE"></property>

        <many-to-one name="employee" class="com.ravindrainfo.tutorial.model.Employee"></many-to-one>
    </class>
</hibernate-mapping>

这是我的员工 Pojo

import java.io.Serializable;
import java.util.Date;

import java.util.Set;

public class Employee implements Serializable {

    private static final long serialVersionUID = 4314638638373270669L;
    private int employeeNumber;
    private String firstName;
    private String lastName;
        private Set<EmployeeProject> employeeProjects;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
        public Set<EmployeeProject> getEmployeeProjects() {
        return employeeProjects;
    }

    public void setEmployeeProjects(Set<EmployeeProject> employeeProjects) {
        this.employeeProjects = employeeProjects;
    }
        public int getEmployeeNumber() {
        return employeeNumber;
    }

    public void setEmployeeNumber(int employeeNumber) {
        this.employeeNumber = employeeNumber;
    }

}
4

2 回答 2

0

我不确定这也适用于 xml,但尝试修改

<set name="employeeProjects" cascade="all-delete-orphan">
      <key column="employeeNumber" />
      <one-to-many class="com.ravindrainfo.tutorial.model.EmployeeProject" />
</set>

<set name="employeeProjects" cascade="all-delete-orphan" orphanRemoval="true">
      <key column="employeeNumber" />
      <one-to-many class="com.ravindrainfo.tutorial.model.EmployeeProject" />
</set>

所以添加orphanRemoval="true"

这适用于 annotation @OneToMany(orphanRemoval=true),所以我很确定它也适用于 xml (实际上你甚至可以cascade="all-delete-orphan"非常安全地删除)

编辑:我查看了更多信息,发现此页面Hibernate Unidirectional Parent/Child relationship - delete() 对子表执行更新而不是删除,因此您可以尝试修改映射添加inverse="true"而不是orphanRemoval. 所以

<set name="employeeProjects" cascade="all-delete-orphan" inverse="true">
    <key column="employeeNumber" />
    <one-to-many class="com.ravindrainfo.tutorial.model.EmployeeProject" />
</set>
于 2013-02-15T10:21:09.227 回答
0

我认为级联应该是all,delete-orphan而不是all-delete-orphan

休眠文档的摘录:

如果子对象的生命周期受限于父对象的生命周期,则通过指定 cascade="all,delete-orphan"(@OneToMany(cascade=CascadeType.ALL, orphanRemoval=true)) 使其成为生命周期对象。

参考:http ://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/objectstate.html#objectstate-transitive

于 2013-02-15T10:31:24.867 回答