[英]How to delete just the entity and its join record in a many-to-many Hibernate association
我與多對多關系有一個嚴重的問題。 我只將關系映射到組的一側,如下所示
<class mutable="true" name="entities.Group" table="StudGroup">
...
<set lazy="false" cascade="save-update" name="studentSet" table="stud_group_join">
<key column="groupId"/>
<many-to-many class="entities.Student" column="studId"/>
</set>
...
</class>
通過刪除嘗試,我會遇到以下問題:
通過刪除組,該組和與之關聯的所有學生都將被刪除。
通過嘗試刪除學生,我收到異常:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`students`.`stud_group_join`, CONSTRAINT `FK30E5217826CEA4E5` FOREIGN KEY (`studId`) REFERENCES `student` (`id`))
我希望程序只刪除傳遞給它的實體和綁定到它的聯接記錄。
您的任何幫助,我將不勝感激。
如果要刪除Student
,則必須這樣做:
student.getGroups().remove(student);
entityManager.remove(student);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.