我的問題是,當我刪除父子並沒有被刪除,而不是刪除了孩子,孩子正在更新 這是輸出雖然刪除了父母,孩子被更新,而不是刪除
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;
}
}
你嘗試過這麼遠嗎?您應該更改標籤以獲得更多合適的人員來回答您的問題。標籤的一個好主意是您正在使用的語言的名稱。 – 2013-02-15 10:36:27
@ProfPickle我已經添加一個標籤感謝您的建議實際上這是我第一次問這個問題,這就是爲什麼我不知道 – user1527637 2013-02-15 10:45:01