-->
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.  [ 7 posts ] 
Author Message
 Post subject: OneToOne and LazyToOne
PostPosted: Tue Jun 24, 2008 10:58 am 
Beginner
Beginner

Joined: Thu Feb 21, 2008 3:31 pm
Posts: 34
Hi all,

I have a mapping in a User domain class that has a OneToOne with another class. I want to only load this mapping when the getter is called.

I came across this mapping that should do that; however, when I query for the Object, it runs another query to populate the OneToOne mapping:

Code:
@OneToOne(optional = true, fetch = FetchType.LAZY)
@LazyToOne(value = LazyToOneOption.NO_PROXY)
@LazyToOne(LazyToOneOption.NO_PROXY)


I tried several variations of LazyToOne, but none of those worked. Is there another method I could use to only load this object when the getter is called?


Thanks,
Walter


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 24, 2008 11:06 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Here's a little tutorial on mapping one-to-one associations using JPA with Hibernate as the persistence framework.

http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=17mappingonetooneassociations

Image

I think what you want is a lazy attribute, as opposed to a full annotation:

Code:
@OneToOne(cascade=CascadeType.ALL,
                         fetch=FetchType.LAZY)
@JoinColumn(name="detail_id")
public ExamDetail getDetail() {
  return detail;
}


The FetchType.LAZY will stop the eager loading of associated objects. With onetoone relationships, the default is eager.



Quote:
For performance reasons, it's always preferential to minimize the load placed on a system's memory. As such, most associations are set to a FetchType of LAZY. This is especially true for one to many and many to many relationships, where the impact of loading a large number of associated properties can be significant.

For one-to-many and many-to-many relationships, the default FetchType is LAZY. However, for one-to-one mappings, the default is EAGER, meaning that a class, and all of its one-to-one associations, are loaded into memory when it is invoked by the client. If this is not the behavior you want, you can set the FetchType to LAZY in your @OneToOne annotations.


http://www.thebookonhibernate.com/Javac ... sociations

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 04, 2009 7:25 am 
Regular
Regular

Joined: Tue Jun 03, 2008 1:12 pm
Posts: 84
Location: germany
this

Code:
fetch=FetchType.LAZY


does not work for xToOne-Relationships.

The referenced one-to-one entity is always eager fetched, even FetchType.LAZY is set. So we have to use something like LazyToOne or
@Fetch(SELECT)!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 04, 2009 7:38 am 
Regular
Regular

Joined: Tue Jun 03, 2008 1:12 pm
Posts: 84
Location: germany
Even this does not work:

Code:
@Fetch(FetchMode.SELECT)
@OneToOne(fetch = FetchType.LAZY, optional = true, targetEntity=EClass.class)
@JoinColumns({..
private EClass eclass;
//getter/setter


the entity Eclass is always eager fetched.


Last edited by nimo23 on Wed Mar 04, 2009 8:01 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 04, 2009 7:48 am 
Regular
Regular

Joined: Tue Jun 03, 2008 1:12 pm
Posts: 84
Location: germany
Even this does not work, collection is ALWAYS eager fetched.

Code:
@LazyToOne(LazyToOneOption.PROXY)
@Fetch(FetchMode.SELECT)
@OneToOne(fetch = FetchType.LAZY, optional = true, targetEntity=Postage.class)
@JoinColumns({...


any Ideas, why the annotations does not work?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 04, 2009 7:49 am 
Regular
Regular

Joined: Tue Jun 03, 2008 1:12 pm
Posts: 84
Location: germany
Even this does not work, collection is ALWAYS eager fetched.

Code:
@LazyToOne(LazyToOneOption.PROXY)
@Fetch(FetchMode.SELECT)
@OneToOne(fetch = FetchType.LAZY, optional = true, targetEntity=EClass.class)
@JoinColumns({...


any Ideas, why the annotations does not work?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 18, 2009 2:32 pm 
Newbie

Joined: Wed Mar 18, 2009 2:26 pm
Posts: 1
nimo23 wrote:
Even this does not work, collection is ALWAYS eager fetched.

Code:
@LazyToOne(LazyToOneOption.PROXY)
@Fetch(FetchMode.SELECT)
@OneToOne(fetch = FetchType.LAZY, optional = true, targetEntity=EClass.class)
@JoinColumns({...


any Ideas, why the annotations does not work?



write @OneToOne(fetch = FetchType.LAZY, optional=false) and all be OK.

The feature of xxxToOne is the "optional=false" must be declared


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