I have to come right out and admit something: I like my many-to-many mappings. I know they're not encouraged, but something about being able to hide the complexity of managing an association table from my application code appeals to me. Sings to me, like a siren to my caffeine-addled brain.
Now I have a problem: when you remove items from the owning side of a bidirectional many-to-many, Hibernate updates the association table. When you do the same from the owned side, nothing happens. My solution?
Declare neither side as the inverse.
Yeah. I know.
It solves the removal problem neatly enough, allowing you to delete associations from either end. But when you go to insert new associations, this falls flat on its face. Now you have to remember to set only one side of the association, instead of both like you've been taught. Instead of using your nice addXXX and removeXXX helper methods, you end up calling entity.getCollection().add(xxx) and annotating your code with apologies to the reader for your horrible, horrible crimes.
Well, at least I have the good sense to be ashamed.
|