我的问题是,当我删除父级时,子级没有被删除,而是没有删除子级,子级正在更新。父表是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>