2013-02-15 115 views
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; 
    } 

} 
+0

你嘗試過這麼遠嗎?您應該更改標籤以獲得更多合適的人員來回答您的問題。標籤的一個好主意是您正在使用的語言的名稱。 – 2013-02-15 10:36:27

+0

@ProfPickle我已經添加一個標籤感謝您的建議實際上這是我第一次問這個問題,這就是爲什麼我不知道 – user1527637 2013-02-15 10:45:01

回答

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"

這適用於註釋 @OneToMany(orphanRemoval=true),所以我敢肯定它與XML工程,以及(實際上,你甚至可以刪除 cascade="all-delete-orphan"相當安全)

編輯:我簽出更多的信息,發現這頁Hibernate Unidirectional Parent/Child relationship - delete() performs update on child table instead of 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> 
+0

孤兒刪除元素不支持在我的XML如果你有另一種解決方案,請給我和一個問題在我pojo類我沒有覆蓋equals()和hashCode()這有什麼區別? – user1527637 2013-02-15 10:28:39

+0

不,它不應該。Hibernate發出查詢'update employee_project set employeeNumber = null where employeeNumber =?',這樣就可以看到你的級聯。看看[api](http://docs.jboss.org/hibernate/orm/4.0/javadocs/org/hibernate/metamodel/binding/CascadeType.html),看起來你的例子應該像你期望的那樣工作,但試試將cascade =「all-delete-orphan」'修改爲'cascade =「delete-orphan」'。對不起,我正試圖從我的記憶中恢復,我很久沒有使用xml了! – ThanksForAllTheFish 2013-02-15 10:36:32

+0

我已經嘗試過所有的全部刪除孤兒,刪除孤兒,所有,刪除孤兒沒有人正在工作相同的問題發生父母被刪除,但孩子沒有刪除其更新 – user1527637 2013-02-15 10:41:26

0

我覺得級聯應該是全部刪除孤兒而不是全刪除,孤兒

Hibernate文檔中的摘錄:

如果子對象的生命週期由父對象的生命週期限定,通過指定 cascade =「all,delete-orphan」(@ OneToMany(cascade = CascadeType.AL)將其設爲生命週期對象L, orphanRemoval = true))。

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

+0

仍然存在同樣的問題存在孩子正在更新和它的所有,刪除孤兒你的拼寫錯誤,我認爲:-P – user1527637 2013-02-15 10:37:09

+0

你是對的拼寫 - 我爲了一致性,但現在我對無法工作的原因一無所知...... – Grim 2013-02-15 10:43:43