This is more of a "should Hibernate allow this" question.
I have been struggling conceptually with why you can't use a self-referential one-to-one mapping from one joined-subclass to another sibling subclass, as asked most recently
here.
In my read of it, Hibernate treats a "one-to-one" relationship as object equivalence:
Quote:
Primary key associations don't need an extra table column; if two rows are related by the association then the two table rows share the same primary key value. So if you want two objects to be related by a primary key association, you must make sure that they are assigned the same identifier value!
With normal, unrelated classes, setting up a one-to-one relationship will add referential integrity between their primary keys.
However, with self-referential joined-subclasses, e.g.
Code:
<class name="A">
<id ... />
<joined-subclass name="B">
...
<one-to-one name="relatedObj" class="A"/>
</joined-subclass>
<joined-subclass name="C">
...
</joined-subclass>
</class>
Let's assume we have code such as this:
Code:
C cObj = new C();
B bObj = new B();
b.setRelatedObj(cObj);
... {code to save/commit} ...
When I then, later load the objects, ...
Code:
... {code to load objects} ...
A aObj = b.getRelatedObj();
...
aObj will have a value of
bObj instead of
cObj.
I believe this is because with a one-to-one relationship, Hibernate assumes the primary keys are the same, regardless of object inheritance structure. Thus Hibernate associates bObj with
itself, instead of cObj.
Question 1. Does this make sense?
Question 2. Is this a bug? Is this something Hibernate should allow?
Thanks for any input (BTW I have already worked around this by modeling as a <many-to-one> relationship, I just was curious about community feedback.
(the following doesn't apply, but I list anyway)
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:
2.1.x
Mapping documents:
[code]
Code between sessionFactory.openSession() and session.close():
N/A
Full stack trace of any exception that occurs:
N/A
Name and version of the database you are using:
Any
The generated SQL (show_sql=true):
N/A
Debug level Hibernate log excerpt:
N/A