我有3个班级A,B和C.而且关系是:
我可以添加新记录并编辑地址集的现有记录,但我无法删除任何现有记录。
更新对象的代码
currentObj = getSession().merge(currentObj);
映射:
Address.hbm.xml
<joined-subclass name="test.domain.ParticipantAddress"
table="PARTICIPANT_ADDRESS"
dynamic-update="false"
dynamic-insert="false"
lazy="true"
>
<key
column="IDENTIFIER"
/>
<many-to-one name="participant" class="test.domain.Participant"
column="PARTICIPANT_ID" not-null="false" cascade="none" unique="true"/>
<property
name="sortOrder"
type="long"
update="true"
insert="true"
column="sortOrder"
length="255"
/>
</joined-subclass>
Participant.hbm.xml
<list name="addressCollection" table="PARTICIPANT_ADDRESS"
lazy="false"
inverse="true"
cascade="all">
<cache usage="read-write"/>
<key column="PARTICIPANT_ID"/>
<list-index column="sortOrder"/>
<one-to-many class="test.domain.ParticipantAddress"/>
</list>
我也尝试过使用cascade =“all,delete-orphan”,但它也会抛出错误“A collection with cascade =“all-delete-orphan”不再被拥有实体实例“
引用答案 0 :(得分:0)
检查表B中的外键约束,如果它不允许在B之前删除A,那么您将看到此行为。请记住,无论你在hibernate上做什么都将被翻译成SQL DML / DDL