在Hibernate中删除一个多映射中的父项和子项
父表在Hibernate中删除一个多映射中的父项和子项,hibernate,orm,hibernate-mapping,Hibernate,Orm,Hibernate Mapping,父表 <property name="buyerGroupName" type="string" column="BUYER_GROUP_NAME" /> <property name="description" type="string" column="DESCRIPTION" /> <property name="approvalPathId" type="int"
<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>
下面是错误
<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>
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设置为它期望的值。。。请帮忙我认为您必须在父实体映射的
cascade
属性上使用all,delete orphan
值。
<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>
您还必须通过在父实体映射上设置
inverse=“true”
来设置双向关系的责任。我认为您必须使用父实体映射上的cascade
属性上的all,delete orphan
值。
<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>
您还必须通过在父实体映射上设置
inverse=“true”
来设置双向关系的责任。您是否放置了:cascade='all,delete orphan'
?是的,我在parent.hbm文件中为买家标记进行了所有的级联,我编辑了我的答案,因为我看到没有inverse=“true”
在您的映射上。我确实添加了它,现在它不会引发任何错误。。我在控制台中看到它正在运行删除查询。。但是当我检查数据库(MYSQL)时,列仍然在父表和子表中。我想现在您可能有一个事务问题,检查您是否放了:cascade='all,delete orphan'
?是的,我在parent.hbm文件中为买家标记了所有的级联。我编辑了我的答案,因为我看到没有inverse=“true”
在您的映射上。我确实添加了它,现在它不会引发任何错误。。我在控制台中看到它正在运行删除查询。。但是当我检查数据库(MYSQL)时,列仍然在父表和子表中。我认为现在您可能有事务问题,请检查
<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>