-->
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: Serialize a class into two tables - one-to-one relationship
PostPosted: Mon Jun 12, 2006 4:12 pm 
Newbie

Joined: Mon Jun 12, 2006 2:22 pm
Posts: 1
Hi all this is my first attempt to use hibernate.

I have a class Contact that contains a class History. Those 2 classes must be serialized into two different tables CONTACTS and HISTORIES. The relationship between contact and history is one-to-one. In HISTORIES table there is a foreign key that point to Contact where the history record belongs to. My problem is how to define all this relationship in Contact.hbm.xml.

I’ll appreciate any help or hint.


Java classes:
----------------
Code:
public class Contact {

   private int id;
   
   private String fname;

   private String lname;

   private History history;


}


Code:
public class History {
   
   private int id;

   private String familyHistory;

   private String personalHistory;
   
   private Contact contact;


}


Hibernate mapping files
----------------------------
Code:
<hibernate-mapping package="gr.osmosis.paediatric.model">
   <class name="Contact" table="CONTACTS">
      <id name="id" column="ID">
            <generator class="native"/>
        </id>
        <property name="fname" />
        <property name="lname" />

   <!-- here must add something to
               define one-to-one relation to history -->       
    </class>

</ hibernate-mapping >



Code:
<hibernate-mapping  package="gr.osmosis.paediatric.model">

   <class name="History" table="HISTORIES">
      <id name="id" column="ID">
         <generator class="native" />
      </id>
      <property name="familyHistory" />
      <property name="personalHistory" />
      
      <many-to-one
       name = "contact"
       column = "CONTACT_ID"
       unique="true"
       not-null = "true" />
      
   </class>

</hibernate-mapping>


Thanks in advanced
/stavros


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 13, 2006 2:47 am 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Oak Creek, WI
Hi,

Read the below link,
http://www.hibernate.org/hib_docs/v3/re ... n-onetoone

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


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.