-->
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.  [ 3 posts ] 
Author Message
 Post subject: One to One relationship association always null
PostPosted: Thu Jun 12, 2008 8:58 pm 
Newbie

Joined: Thu Jun 12, 2008 8:01 pm
Posts: 3
Hi all,

I am fairly new to Hibernate and have started working on a small app using hibernate on the backend.

I have the following two tables

User <---> Registration

There is a one to one relationship with unique foreign key. This is how I have defined it in the hbm.xml files

User.hbm.xml

Code:
<one-to-one name="registration" class="Registration" property-ref="user" />



Registration.hbm.xml

Code:
<many-to-one name="user" class="User" column="USER_ID" unique="true" not-null="true"/>



When I try to run the following HSQL query to load both the user and the registration objects.

Code:
from User as u
inner join fetch u.registration as reg
where u.id = <id value>


I get a User object but the reference to registration via user.getRegistration() always returns null.

I have even tried to set the attributes lazy='false' and fetch='join' but still getting same results.

On the other hand when I execute the following HSQL

Code:
from Registration as reg
inner join fetch reg.user as u
where u.id = <id value>


I get a Registration object returned and on executing reg.getUser() I do get User object back.

I know I can always work around by changing the HSQL query but I want to know why is Hibernate is behaving in this manner.

I user Hibernate3 and Postgres

Regards


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 12, 2008 11:04 pm 
Newbie

Joined: Thu Jun 12, 2008 8:01 pm
Posts: 3
I have been debugging Hibernate logs to see if anything obvious comes out

Code:
=[DEBUG] 12:57:32 SQL - select user0_.ID as ID8_0_, registrati1_.ID as ID6_1_, user0_.OBJ_VERSION as OBJ2_8_0_, user0_.FIRSTNAME as FIRSTNAME8_0_, user0_.LASTNAME as LASTNAME8_0_, user0_.USERNAME as USERNAME8_0_, user0_.PASSWORD as PASSWORD8_0_, user0_.ENABLED as ENABLED8_0_, user0_.EMAIL as EMAIL8_0_, user0_.CREATED as CREATED8_0_, registrati1_.OBJ_VERSION as OBJ2_6_1_, registrati1_.URL as URL6_1_, registrati1_.CREATED as CREATED6_1_, registrati1_.USER_ID as USER5_6_1_, registrati1_.TYPE as TYPE6_1_ from USERS user0_ inner join REGISTRATION registrati1_ on user0_.ID=registrati1_.USER_ID where user0_.ID=?
Hibernate: select user0_.ID as ID8_0_, registrati1_.ID as ID6_1_, user0_.OBJ_VERSION as OBJ2_8_0_, user0_.FIRSTNAME as FIRSTNAME8_0_, user0_.LASTNAME as LASTNAME8_0_, user0_.USERNAME as USERNAME8_0_, user0_.PASSWORD as PASSWORD8_0_, user0_.ENABLED as ENABLED8_0_, user0_.EMAIL as EMAIL8_0_, user0_.CREATED as CREATED8_0_, registrati1_.OBJ_VERSION as OBJ2_6_1_, registrati1_.URL as URL6_1_, registrati1_.CREATED as CREATED6_1_, registrati1_.USER_ID as USER5_6_1_, registrati1_.TYPE as TYPE6_1_ from USERS user0_ inner join REGISTRATION registrati1_ on user0_.ID=registrati1_.USER_ID where user0_.ID=?
=[DEBUG] 12:57:32 AbstractBatcher - about to open ResultSet (open ResultSets: 0, globally: 0)
=[DEBUG] 12:57:32 Loader - result row: EntityKey[com.model.User#1], EntityKey[com.model.Registration#1]
=[DEBUG] 12:57:32 AbstractBatcher - about to close ResultSet (open ResultSets: 1, globally: 1)
=[DEBUG] 12:57:32 AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
=[DEBUG] 12:57:32 StatefulPersistenceContext - initializing non-lazy collections


As you can see it is issuing the right SQL and loading all the properties of the user and registration class in one go. But then it is not populating the registration object. I just cannot understand why?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 13, 2008 3:50 am 
Newbie

Joined: Thu Jun 12, 2008 8:01 pm
Posts: 3
Ok I have fixed my problem :)

Basically, when saving the user and registration objects I was only setting one side of the association.

Code:
registration.setUser(user)


Setting both sides of the association fixed the problem.
like

Code:
registration.setUser(user)
user.setRegistration(registration)


Oh well...it's all part of learning...


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