|
Hello board...
I'm resolving to this board, because i have stumbled into this situation quite a couple of times now, and never really seemed to find a good explanation to my wonders and a clean solution.
The issue is, that i have a many-to-many mapping.. Example: blog has many keywords, and blog is the defining side of the relationship.. However. There's not full participation in the relationship and keywords are not aggregates.. Keyword objects are a portion of both selectable (already existing) values and keywords which are added while managing the blog (possibly becoming selectable to others).. The issue here is, that i also would like to delete these keywords, and this is where the pain begins... If keywords are attached to a blog, i'm not able to delete them without first resolving to whom they are connected and removing the references... Trying to delete attached keywords, results in following exceptions:
Caused by: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
java.sql.BatchUpdateException: Batch entry 0 delete from Keyword where id=17 was aborted. Call getNextException to see the cause.
What i want is the ability to delete keywords and automatically loosen references it might have.. Hibernate actually does try to delete the object, but violates the DB constraints, because it does not remove the reference in the join table before it tries to delete the keyword object itself. It works perfectly well doing it the other way around from the "managing" side...
Is this per spec behaviour? Trying to resolve the references first and removing them from that point seems very tediuos in many cases and involves unreasonable database action, especially considering, that from a relational aspect it's just a matter of cascade the delete to the join table.. Another approach could be to manually hack the DB constraint and define it as cascading, but i'm not very confident about "cheating" on Hibernate. Third would be to just flag keywords as deleted (and maybe let them be cleaned up at a later point by some process)...
However.. Just annoys me that this is not possible, and i would like a confirmation of this issue, or it's just me doing something wrong.. And if this is normal behavious, i would like to know how managing loose many to many relations can be achieved, simply disconnecting them from whatever side they are deleted...
What even more surprised me is, that it's actually possible to cascade a delete, providing mappedBy="keywords", cascade=CascadeType.DELETE on the non-managing side.. Why is this not possible then?
Hopefully someone can enlighten me.. Thanks in advance, and a lot of thanks to the Hibernate team :)
|