2013-02-28 105 views
1

父表 刪除父母與子女在一對多映射在Hibernate中

<property name="buyerGroupName" type="string"  column="BUYER_GROUP_NAME" /> 
    <property name="description"  type="string"  column="DESCRIPTION" /> 
    <property name="approvalPathId" type="int"   column="APPROVAL_PATH_ID" /> 
    <property name="active"   type="int"   column="ACTIVE" /> 
    <property name="createdOn"   type="timestamp"> 
      <column name="CREATED_ON" length="20" not-null="true" /> 
    </property> 

    <set name="buyers" table="buyers" cascade="all" > 
     <key> 
      <column name="BG_ID" not-null="true" /> 
     </key> 
     <one-to-many class="com.sg.beans.Buyers" /> 
    </set> 


</class> 
    </hibernate-mapping> 

子表映射

<hibernate-mapping> 
<class name="com.sg.beans.Buyers" table="buyers" > 
    <id name="id" type="int" column="ID"> 
     <generator class="increment" /> 
    </id> 

    <property name="loginId" type="int" column="LOGIN_ID" /> 

    <many-to-one name="buyerGroup" class="com.sg.beans.BuyerGroup" fetch="select" cascade="all" > 
     <column name="BG_ID" not-null="true" /> 
    </many-to-one> 

</class> 
    </hibernate-mapping> 

刪除方法..傳遞父對象刪除

public Boolean deleteBuyerGroup(BuyerGroup bg){ 

    try { 
     session.delete(bg); 
     session.getTransaction().commit(); 
     return true; 
    } catch (Exception e) { 
     e.printStackTrace(); 
     return false; 
    } 
} 

下面是錯誤

buyerGroup.getBuyerGroupId()1 

    Hibernate: update buyers set BG_ID=null where BG_ID=? 
    org.hibernate.exception.ConstraintViolationException: Column 'BG_ID' cannot be null 

我做了調試,並確信BG_ID設置的值,它期待...請幫助!

回答

1

我想你將不得不在cascade屬性上使用父實體映射的all,delete-orphan值。 您還必須通過在父實體映射上設置inverse="true"來設置雙向關係的責任。

+0

它仍然給我同樣的錯誤 – ashlesha 2013-02-28 22:10:07

+0

你放:'cascade ='all,delete-orphan''? – benzonico 2013-02-28 22:12:45

+0

是的,我爲買家標記在parent.hbm文件中做了所有級聯 – ashlesha 2013-02-28 22:18:31

相關問題