我的问题是,当我删除父子时,没有删除父子,而不是删除子,子正在更新,这是输出
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;
}
}