I have a many-to-many relation on a single object with itself:
<set name="relatedPersons" lazy="false" table="PersonRelatedPerson"
cascade="none" >
<key column="personId" foreign-key="fk_related_person" not-
null="false"/>
<many-to-many column="relatedPersonId" class="Person" not-found="ignore"/>
</set>
as you can see I have set cascade to "none" because, when i delete one person, ofcourse i just would like to delete the relations from the table PersonRelatedPerson, and not the related Persons them selves. When i try to do delete with this mapping, i get the ConstraintViolationException: Could not execute JDBC batch update....
I am guessing that it is somehow possible to delete this by hibernate by properly setting some mapping flags (or do i have to do some weird workarounds eg. first delete second person from set of first person, delete first person from set of second, and then delete first.... i really dont wanna do this :D)
Appreciate all the help!!!
You'll just have to remove the relationships yourself. To do this, before removing the person A, you'll have to find all the persons who have A in their set of related persons, and remove A from these sets. If you don't, then of course you can't delete A because other persons still reference A.
Either do this with a HQL query and remove A from the sets of the found persons in Java, or use a dedicated SQL delete query to remove all the rows from the join table. But be aware that this SQL query will bypass the caches (first-level and second-level, if any), and your session might thus hold an incorrect view of the database.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.