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: one-to-one question
PostPosted: Thu Aug 24, 2006 1:01 pm 
Beginner
Beginner

Joined: Mon Jun 19, 2006 4:10 pm
Posts: 27
In general, for a one-to-one mapping to work such that when a child entity is added to a parent, and the parent is persisted, the child gets saved, and then when a parent is returned the child is hydrated, does the one-to-one declaration have to go in both the parent and child mappings? Or just the parent?

I could not get the child hydration working with just the one-to-one in the parent mapping....

thanx


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 24, 2006 2:26 pm 
Regular
Regular

Joined: Fri Aug 18, 2006 2:40 pm
Posts: 51
Location: Metz, France
Hibernate version: ?

In Hibernate 3.x, default behaviour is now to instanciate proxy for one-to-one relationship. You can make hibernate not use proxy using lazy=false at the class or association levels.

When you do parent.getChild().getAPropertyValue() where getAPropertyValue is one of the existing getter, Hibernate will load the related instance.
Quote:
The optional proxy attribute enables lazy initialization of persistent instances of the class. Hibernate will initially return CGLIB proxies which implement the named interface. The actual persistent object will be loaded when a method of the proxy is invoked. See "Proxies for Lazy Initialization" below.

Quote:
By default, Hibernate3 uses lazy select fetching for collections and lazy proxy fetching for single-valued associations. These defaults make sense for almost all associations in almost all applications.
...
Alternatively, we could use a non-lazy collection or association, by specifying lazy="false" for the association mapping. However, it is intended that lazy initialization be used for almost all collections and associations. If you define too many non-lazy associations in your object model, Hibernate will end up needing to fetch the entire database into memory in every transaction!


http://www.hibernate.org/hib_docs/v3/re ... ching-lazy

_________________
Denis
Don't forget to rate ... thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 24, 2006 2:36 pm 
Beginner
Beginner

Joined: Mon Jun 19, 2006 4:10 pm
Posts: 27
thanx for the response.

version was 3.x.

I definitely made lazy=false, but with the one-to-one in only the parent, the child was not being hydrated/set on the parent on a load.

parent.getChild() returned null....


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 24, 2006 2:38 pm 
Regular
Regular

Joined: Fri Aug 18, 2006 2:40 pm
Posts: 51
Location: Metz, France
I guess your mapping is wrong
http://www.hibernate.org/hib_docs/v3/re ... n-onetoone

You must say if you use
primary key associations
OR
unique foreign key associations

_________________
Denis
Don't forget to rate ... thanks.


Last edited by denis.etienne on Thu Aug 24, 2006 2:43 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 24, 2006 2:42 pm 
Beginner
Beginner

Joined: Mon Jun 19, 2006 4:10 pm
Posts: 27
in the parent mapping, in the one-to-one element for the child, lazy = false.

I fixed it by adding a foreign id to the child, and adding one-to-one to the child. I don't know if it was the first or second that did it.

Whats wierd is before i added anything to the child mapping, I looked thru the hibernate output and saw it running the query to fetch the child, but it hydrated 0 objects.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 24, 2006 2:45 pm 
Regular
Regular

Joined: Fri Aug 18, 2006 2:40 pm
Posts: 51
Location: Metz, France
Quote:
Alternatively, a foreign key with a unique constraint, from Employee to Person, may be expressed as:

<many-to-one name="person" class="Person" column="PERSON_ID" unique="true"/>
And this association may be made bidirectional by adding the following to the Person mapping:

<one-to-one name"employee" class="Employee" property-ref="person"/>
[/quote]

_________________
Denis
Don't forget to rate ... thanks.


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.