Thread:
 Mapping a "one-to-zero-or-one" relationship 
 Regis Pires Magalhae...   18 Oct 2005, 16:35 

Comment
Prev. thread 
 Next thread
 
Prev. posting 
 Next posting
From: Regis Pires Magalhaes (18 Oct 2005, 16:35) Replies: 0, Views: 38624
Subject: Mapping a "one-to-zero-or-one" relationship
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

<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.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

<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.
Prev. thread 
 Next thread
 
Prev. posting 
 Next posting
© Copyright 2006, Red Hat Middleware, LLC. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc. [Privacy Policy]