-->
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.  [ 5 posts ] 
Author Message
 Post subject: Need help reg. one-to-one association
PostPosted: Sat Jul 03, 2010 11:45 am 
Newbie

Joined: Sat Jul 03, 2010 11:29 am
Posts: 2
Hi,
I have DB structure as below:

Table Person:
ID : Primary Key
Age

Table Name:
ID : Primary Key : Foreign Key Person(ID)
First_Name
Last_Name


I have my POJOs as below:

class Env
{
private int id;
private int age;
private Name name;
}

class Name
{
private int id;
private String firstName;
private String lastName;
}


I'm new to Hibernate. I have an urgent requirement to implement a similar thing as above. I read many articles on net, but could not get a solution to above. Some of the places i found NAME_ID as a primary key to Name table and a reference is stored in Person table. But my requirement is as above.

Please help me with getting correct mapping.


Top
 Profile  
 
 Post subject: Re: Need help reg. one-to-one association
PostPosted: Sat Jul 03, 2010 2:02 pm 
Newbie

Joined: Mon Jun 21, 2010 1:36 pm
Posts: 14
As I understand, you would like to use the same value for primary key in both tables. This is a Shared Primary key scenario. It can be implemented with the following:

1. Add an attribute of type Env to Name class, this will establish a bi-directional association.
2. Add <one-to-one> tag with the appropriate attributes in the .hbm.xml files for both classes.
3. In the .hbm.xml file for Name class, you also need to add the following xml elements
<id name="id" column="ID">
<generator class="foreign">
<param name="property">env</param>
</generator>
</id>

env in step 3 refers to the reference of Env class in Name that you need to add.


Top
 Profile  
 
 Post subject: Re: Need help reg. one-to-one association
PostPosted: Sun Jul 04, 2010 3:16 am 
Newbie

Joined: Sat Jul 03, 2010 11:29 am
Posts: 2
Hi Jagan,
Thanks for the quick response...

I still have few queries -
First thing is i want my association to be uni-directional, from Person to Name. So is it still needed to have a reference to Person in Name class? (class "Env" was a typo...please consider it as "Person")

Also i have mapped the entities as below:

<class name="Person" table="PERSON" >
<id column="ID" name="id" />
<property column="AGE" name="age" />
<one-to-one name="name" class="Name"/>
</class>

<class name="Name" table="NAME" >
<id name="id" column="ID">
<generator class="foreign">
<param name="property">person</param>
</generator>
</id>
<property column="FIRST_NAME" name="firstName" />
<property column="LAST_NAME" name="lastName" />
<one-to-one name="person" class="Person" constrained="true" />
</class>

Please correct me if i have done anything wrong above.

Also, how would i need to query to get a Person Object having a reference to a Name object based on ID?

Thanks in advance :)
-Pranit


Top
 Profile  
 
 Post subject: Re: Need help reg. one-to-one association
PostPosted: Mon Jul 05, 2010 2:35 pm 
Newbie

Joined: Mon Jun 21, 2010 1:36 pm
Posts: 14
Pranit,

I believe you need to have Bi-directional association here to have the shared primary key. In Hibernate, we cant seem to do this with a Uni-directional association.

Using JPA with annotations, this seems to be possible with Uni-directional assoc. But again, we need to do a bit of jugglary.

Regards.


Top
 Profile  
 
 Post subject: Re: Need help reg. one-to-one association
PostPosted: Tue Jul 06, 2010 1:51 pm 
Newbie

Joined: Tue Jul 06, 2010 11:37 am
Posts: 6
see pranit...if it is bi-directional then at one side you need to write a one-to-one and otherside many-to-one...vice versa....


if it is unidirectional then write a many-to-one tag on oneside..............thats enough..............on the otherside u need not write any relation..or reference belong to opposite mapping class.....

in hibernate we can use many to one tag for one to one relation--unidirectional...........


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