我的问题是,当我删除父级时,子级未被删除但是不是删除子级,子级正在更新。父级表是Employee
而子级表是EmployeeProject
有一对多员工和项目之间存在关联船一个员工有很多项目我做了什么请检查我在哪里错误这是查询在控制台上显示
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);
transaction.commit();
flag = true;
} catch (HibernateException exception) {
if (transaction != null)
transaction.rollback();
flag = false;
}
return flag;
}
这是父表映射文件 Employee.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="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>
<set name="employeeProjects" cascade="all-delete-orphan" lazy="false"
inverse="true">
<key column="employeeNumber" />
<one-to-many class="com.nousinfo.tutorial.model.EmployeeProject" />
</set>
<property name="address1" type="string" column="ADDRESS_1"></property>
</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>
<property name="endDate" type="date" column="END_DATE"></property>
<property name="role" type="string" column="PROJECT_ROLE"></property>
<many-to-one name="employee" class="com.nousinfo.tutorial.model.Employee" ></many-to-one>
</class>
</hibernate-mapping>
答案 0 :(得分:0)
将您的级联从 all-delete-orphan 更改为删除
因为你需要的只是当父母被删除时,所有相关的孩子也应该被删除。这就是 cascade = delete 的作用。
cascade = all-delete-orphan 也用于删除子实体,它肯定服务于不同的场景。当关系被切断时,它将删除子实体。例如,如果从父实体中的集合中删除子实体,则在关闭hibernate会话时将删除子实体。当子实体逻辑上不存在父级时,此级联类型非常有用。
答案 1 :(得分:0)
在employee.hbm.xml中,删除标志inverse =“true”并将级联修改为cascade =“all”。将inverse =“true”置于此处意味着此关系的所有者是EmployeeProject,而不是Employee,这不是我们想要的。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.nousinfo.tutorial.model.Employee" table="EMPLOYEE">
<id type="int" name="employeeId" column="EMPLOYEE_ID"/>
<set name="employeeProjects" cascade="all">
<key column="EMPLOYEE_ID"/>
<one-to-many class="com.nousinfo.tutorial.model.EmployeeProject"/>
</set>
</class>
</hibernate-mapping>
我还修改了键列的名称以匹配EmployeeProject composite-id中的名称,否则您将拥有2列具有相同信息的列。 在project.hbm.xml中,按如下所示修改多对一,以获得正确的双向映射:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.nousinfo.tutorial.model.EmployeeProject" table="EMPLOYEE_PROJECT">
<composite-id>
<key-property type="int" name="employeeId" column="EMPLOYEE_ID"/>
<key-property type="string" name="projectCode" column="PROJECT_CODE"/>
</composite-id>
<many-to-one name="employee" column="EMPLOYEE_ID" insert="false" update="false"/>
</class>
</hibernate-mapping>