Hi,
I'm using hibernate 3.2 with mysql 5.
I have a question concerning a mapping strategy for Associations. Say I have class A, B and C. A and B each have an unidirectional association to class C. Each A and B is associated only with one Object of type C, but each C Object can be associated by any number of A and B objects.
A or B objects can be initialized with persistent or detached C Objects.
I wonder which mapping best fits my needs here.
Option 1) which came first in my mind was to use a <many-to-one> association. This works fine so far - using cascade="save-update" also the creation of the C entries in the C table is done by hibernate. When a transient A object holding a reference to a already persistent C Object, the C Object is not saved twice, but the now persistent A object has a oreign key to the already existing C entry in teh db. This is fine so far - only problem is with removing - I need something like a garbage collector that delets all C objects that are not associated to any A or B object. Is there a way to accomplish that with hibernate?? If not what is the best way of implementing that? database Triggers are probaly better than writing that in Java I guess - any other ideas?
Then I thought about Opion 2), using a <one-to-one> mapping. The problem here is again with saving a A or B object with a reference to a persistent C object. With cascade="all", the C object is not saved again and hence, the newly created A or B object has no associated C Object when retrieved from the database.
Can anyone contribute something to this issue? Help would be greatly appreciated.
Thanks,
Hans Peter
|