To continue this thread I thought I would present my exact problem since I'm not sure I can do it with the above solution.
Here's the situation, I need to store 'Vote' objects that contain a few small strings. These vote objects MUST be tied somehow to a parent object, and that parent object is one-to-one'd with a user object.
1) If I have a vote.parent property and a many-to-one mapping in the vote mapping file I can add the vote easily by setting the parent. However the problem comes when I need to delete, since there is no top down connection if I delete the user object it will cascade and delete the parent object however that cascade will not continue to the vote object. So I'm stuck loading all the votes (could be 1000's or even 100's of thousands), and then sending them to session.delete 1 by 1, VERY not performant.
2) If I chose to map parent.votes as a bag, the cascaded deletes would work, however I Just noticed this in the docs section 14.1.1:
Quote:
Bags are the worst case. Since a bag permits duplicate element values and has no index column, no primary key may be defined. Hibernate has no way of distinguishing between duplicate rows. Hibernate resolves this problem by completely removing (in a single DELETE) and recreating the collection whenever it changes. This might be very inefficient.
I can't imagine re-inserting 1000's+ of records everytime I add to the bag to be performant.
3) I thought about using a many-to-many relationship and setting the vote end as inverse, since it would only ever be setting one parent thats not a big deal (weird model but it might work). However querying with that situation seemed very difficult, ie would this even work??):
Code:
selct user.id from Vote vote left join fetch vote.parents p left join fetch p.user where vote.property <> :value and p.property='1'
Any help greatly appreciated,
David