-->
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.  [ 4 posts ] 
Author Message
 Post subject: Lazy loading of OneToOne associations isn't lazy enough
PostPosted: Tue Feb 07, 2006 1:11 am 
Newbie

Joined: Fri Feb 03, 2006 3:42 pm
Posts: 4
Location: Manassas, VA USA
I have a top level class where all OneToOne annotations have (fetch=FetchType.LAZY). This sort of works as advertised. Each association is populated by a separate query. Unfortunately the query is run for each top-level object loaded regardless of whether the associated property is accessed.

The following annotation in the Person class:

@OneToOne(
cascade = CascadeType.REMOVE,
fetch = FetchType.LAZY
)
@PrimaryKeyJoinColumn
private ContactInfo contactInfo;

Yields the following hibernate queries for Person and ContactInfo, even though ContactInfo hasn't been accessed.

Doing the same thing with ManyToOne (fetch=FetchType.LAZY) on other properties and it works as expected. The query is run only when the associated property is accessed.

Using Hibernate 3.1.2, EntityManager 3.1beta6, Annotations 3.1beta8.

_________________
Bill Squires


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 08, 2006 2:26 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
http://www.hibernate.org/162.html

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 08, 2006 3:02 pm 
Newbie

Joined: Fri Feb 03, 2006 3:42 pm
Posts: 4
Location: Manassas, VA USA
Thank you. This makes perfect sense.

So in the terminology of annotations, adding "optional=false" to the OneToOne annotation will result in the true LAZY behavior, assuming the db relationship supports it.

I noticed that in the EJB3 spec, the default value of "optional" is true for both OneToOne and ManyToOne. I guess this means that even though the ManyToOne behavior is currently lazy without specifying optional=false, the optional=false should still be explicitly stated to ensure the same behavior in the future.

_________________
Bill Squires


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 09, 2006 9:34 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
@OneToOne(fetch=LAZY, optional=true) @PKJC can be lazyfied depending on the actual implementation choice.
It's a tradeoff between field level interception and proxy based lazy loading.

_________________
Emmanuel


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