-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: one-to-one use within joined-subclass - should it work?
PostPosted: Mon May 02, 2005 8:57 am 
Senior
Senior

Joined: Mon Apr 04, 2005 8:04 am
Posts: 128
Location: Manchester, NH USA
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.