-->
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.  [ 2 posts ] 
Author Message
 Post subject: Best practice for mapping a one-to-zero-or-one relationship
PostPosted: Wed Oct 20, 2004 2:11 am 
Newbie

Joined: Mon Aug 30, 2004 1:37 am
Posts: 16
Hibernate version: 2.1.6

I understand that it is not possible to lazily load the optional end of a one-to-one (-or zero) association:
http://www.hibernate.org/162.html
especially the last paragraph:
Quote:
f your B->C mapping is mandatory (constrainted=true), Hibernate will use proxy for C resulting in lazy initialization. But if you allow B without C, Hibernate just HAS TO check presence of C at the moment it loads B. But a SELECT to check presence is just inefficient because the same SELECT may not just check presence, but load entire object. So lazy loading goes away.

Given this - is there a best practice for mapping this kind of relationship so that the optional child is not loaded when the parent is loaded? This seems to be a pretty common scenario (a small frequently accessed table with an optional primary key relationship to a much larger rarely accessed table) so I wonder how people are addressing it.

Thanks.
Grainne.


Top
 Profile  
 
 Post subject: Mapping a "one-to-zero-or-one" relationship
PostPosted: Tue Oct 18, 2005 4:38 pm 
Newbie

Joined: Wed Oct 06, 2004 10:34 am
Posts: 16
Location: Teresina - PI - Brasil
I've been searching a way to do a lazy a "one-to-zero-or-one" relationship in Hibernate and I finally found a way to do it using many-to-one with unique="true". See example below:

Association: Person [1 <--> 0..1] Note

Code:
<class name="Person" table="person">
   <id name="id" column="id" type="int" unsaved-value="-1">
       <generator class="sequence">
           <param name="sequence"father_id_seq</param>
       </generator>
   </id>
   <property name="name" type="string"/>
   <many-to-one name="note" class="Note" column="id"
                unique="true" insert="false" update="false"
                cascade="all"/>
</class>

<class name="Note" table="note">
   <id name="id" column="id" type="int" unsaved-value="-1" >
       <generator class="foreign">
           <param name="property">owner</param>
       </generator>
   </id>
   <property name="note" type="string"/>
   <one-to-one name="owner" class="Person" constrained="true"/>
</class>



Observe that column "id" is used twice in Person mapping.

_________________
Regis Pires


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

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.