-->
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.  [ 6 posts ] 
Author Message
 Post subject: Mapping primary key and foreign key
PostPosted: Tue May 24, 2005 8:17 am 
Newbie

Joined: Tue May 17, 2005 6:23 am
Posts: 5
Hi All
I am very new to hibernate and little confused about primary key and foreignkey in .hbm.xml file.
Lets say I have a table called , User which has following fields
ID (Primary key)
User name
Password

I have another table called User_Details
User_Details
ID (PK+Foreign key to user table)
First Name
Last Name
Address
City
Country

Now I want to creater .hbm.xml files for both the tables how do I do that?
I dont have any tool which will generate these files automatically, so I have to really do it by myself.

Please advise how do I achieve this?


Top
 Profile  
 
 Post subject: Re: Mapping primary key and foreign key
PostPosted: Tue May 24, 2005 8:54 am 
Senior
Senior

Joined: Sun Mar 14, 2004 10:16 am
Posts: 129
Location: Ankara
reisito wrote:
Hi All
I am very new to hibernate and little confused about primary key and foreignkey in .hbm.xml file.
Lets say I have a table called , User which has following fields
ID (Primary key)
User name
Password

I have another table called User_Details
User_Details
ID (PK+Foreign key to user table)
First Name
Last Name
Address
City
Country

Now I want to creater .hbm.xml files for both the tables how do I do that?
I dont have any tool which will generate these files automatically, so I have to really do it by myself.

Please advise how do I achieve this?


First of your association logic is completely wrong, here this is a "one-to-one" association, If it is not a neccesity, do not use 2 tables for 1 object. This means extra join queries.

_________________
-developer


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 24, 2005 9:28 am 
Newbie

Joined: Tue May 17, 2005 6:23 am
Posts: 5
Thanks for your reply,
But honestly if I want to create one-to-one relationship ex: Relationship between a book and sales, a book must have one sales record and a sales record is associated with one specific book in this case how do we achieve above stated goal?


Top
 Profile  
 
 Post subject: Re: Mapping primary key and foreign key
PostPosted: Tue May 24, 2005 9:45 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
reisito wrote:
Hi All
I am very new to hibernate and little confused about primary key and foreignkey in .hbm.xml file.
Lets say I have a table called , User which has following fields
ID (Primary key)
User name
Password

I have another table called User_Details
User_Details
ID (PK+Foreign key to user table)
First Name
Last Name
Address
City
Country

Now I want to creater .hbm.xml files for both the tables how do I do that?
I dont have any tool which will generate these files automatically, so I have to really do it by myself.

Please advise how do I achieve this?


With your original example above, you might want to look at the example at http://www.hibernate.org/41.html which talks about using a lightweight class.

However with the Book/Sales example, you can specify something like this:

Code:
<hibernate-mapping">
  <class name="Book" table="BOOK">
    <id column="ID" name="id" type="java.lang.Long">
       <generator class="native"/>
    </id>
    <property column="TITLE"  name="bookTitle" type="java.lang.Long"/>
     .... additional properties....
    <one-to-one name="sale" class="Sale" cascade="...."/>       
  </class>
   
  <class name="Sale" table="SALE">
     <id column="ID" name="id" type="java.lang.Long">
        <generator class="foreign">
           <param name="property">book</param>
        </generator>
     </id>
     <one-to-one name="book" class="Book" constrained="true"/>
     <property column="SALE_DATE"  name="saleDate" type="java.util.Date"/>
     .... additional properties....
  </class> 
</hibernate-mapping>


The foriegn ID generator will save the ID of the Sale using the generated ID provided by the book.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 24, 2005 9:46 am 
Senior
Senior

Joined: Sun Mar 14, 2004 10:16 am
Posts: 129
Location: Ankara
Read part 5.1.11 one-to-one mapping, it does what exactly you want

_________________
-developer


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 24, 2005 9:50 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
borak wrote:
Read part 5.1.11 one-to-one mapping, it does what exactly you want


Section 6.1.11 in version 3.0.3 and greater


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.