Hi,
I posted a similar question earlier, but I've had a chance to brainstorm a bit more: What I'm looking to do is a polymorphic bi-directional 1-to-n mapping. I have class A, B and C who all have 1-to-many relationships with X. A owns0 or more X's, B owns 0 or more X's, etc. I want X to link back to its parent, either A, B or C. I can do the one way mapping (A-to-X etc.) just fine, as all my primary keys are unique across the DB (i.e. there will never be a key collision between A and B). So I can simply put a mapping into A:
<set name="items">
<key column="parent_id"/>
<one-to-many class="X"/>
</set>
... and the same goes for B and c, as "parent_id" can refer to any of the three classes.
The tricky part comes in when I want to create a bi-directional association, so X can point back to its parent. Can I use <ANY> for this, and if so, what would my mapping look like?
Yes, I know the *ideal* way would be to create a superclass for A, B and C that owns X, but my model does not allow for it. My actual hierarchy is much more complex and would require multiple inheritance etc. I also want to keep query performance high by not hitting more tables than necessary.
What is the best way to accomplish this?
Thanks!
Jen
|