-->
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: one-to-one self-relationship and lazy loading problem
PostPosted: Tue Oct 16, 2007 8:27 am 
Newbie

Joined: Tue Oct 16, 2007 8:09 am
Posts: 1
I have class Country that has bidirectional one-to-one ("0..1 to 0..1" to be specific) association to itself. One end of the association has name 'nextCountry', another is called 'previousCountry'.

To persist the class I use the following mapping:
<hibernate-mapping default-cascade="none">
<class name="model.CountryImpl" table="COUNTRY" dynamic-insert="false" dynamic-update="false">
<id name="id" type="java.lang.Integer" unsaved-value="null">
<column name="ID" sql-type="INTEGER"/>
<generator class="native">
</generator>
</id>
<property name="name" type="java.lang.String">
<column name="NAME" not-null="true" unique="true" sql-type="VARCHAR(255) BINARY"/>
</property>
<property name="active" type="boolean">
<column name="ACTIVE" not-null="true" unique="false" sql-type="TINYINT"/>
</property>
<many-to-one name="nextCountry" class="model.CountryImpl" foreign-key="COUNTRY_NEXT_COUNTRY_FKC" cascade="none" lazy="proxy" fetch="select">
<column name="NEXT_COUNTRY_FK" not-null="false" sql-type="INTEGER" unique="true"/>
</many-to-one>
<one-to-one name="previousCountry" class="model.CountryImpl" property-ref="nextCountry" cascade="none" lazy="proxy" fetch="select"/>
</class>
</hibernate-mapping>

Everything works great - I can persist the class. But there is one problem. Let's say that there is the following state in the database:

USA -> France -> Holland -> Poland (four Country objects are persisted in the database: USA, France, Holland, Poland, and USA's 'nextCountry' field points to France, France's 'nextCountry' field points to Holland, Holland's 'nextCountry' field points to Poland).

Unfortunately when I load USA object, hibernate loads USA and all the objects that are referenced from USA (in this case it means that all the objects are loaded). Because I have specified lazy="proxy" for 'nextCountry' field in the above mapping, I would expect that when I request USA i will get only USA object with its 'nextCountry' field initialized by proxy of France, whithout a database hit to fetch France object.

Could you tell me what's going on and how to get the behavior that I expect? I use hibernate 3.1.3

Thank you for your help,

Lukasz Indyk.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 16, 2007 4:43 pm 
Beginner
Beginner

Joined: Fri Jun 25, 2004 11:47 am
Posts: 34
Hi,

quickly checked your mapping. Are you sure you are not missing something in it?
The table field is not declared....

Code:
<one-to-one name="previousCountry" class="model.CountryImpl" property-ref="nextCountry" cascade="none" lazy="proxy" fetch="select"/>


rgds


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.