-->
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.  [ 8 posts ] 
Author Message
 Post subject: Question about FetchType.LAZY
PostPosted: Mon Nov 08, 2010 3:22 pm 
Newbie

Joined: Mon Nov 08, 2010 3:09 pm
Posts: 4
EntityOne has a reference to EntityTwo, and EntityTwo has a reference to EntityThree. All three relationships are defined as follows:
Code:
   @ManyToOne(cascade = {}, fetch = FetchType.LAZY)
   @JoinColumn(name = "XX", unique = false, nullable = false, insertable = true, updatable = true)

In my code, I am trying to get to EntityThree this way:
Code:
entityOne.getEntityTwo().getEntityThree()

If I use FetchType.EAGER, this works perfectly. If I use LAZY, entityOne.getEntityTwo() returns a proxy, and getEntityThree() then fails:
Code:
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
    at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:57)
    at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
    at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:150)
    at com.ibi.entity.price.OperatingSystem$$EnhancerByCGLIB$$cee9d0f8.toString(<generated>)

Originally I had FetchType.EAGER, but I thought I'd switch to LAZY to improve performance (as EntityOne has a number of @ManyToOne relationships, and I usually don't need them all at any one time.) Clearly I am missing something elemental in how LAZY should work, and/or my coding technique is incorrect. Can someone point me in the right direction?


Top
 Profile  
 
 Post subject: Re: Question about FetchType.LAZY
PostPosted: Mon Nov 08, 2010 3:35 pm 
Newbie

Joined: Fri Nov 05, 2010 1:36 pm
Posts: 11
ibstmt wrote:
EntityOne has a reference to EntityTwo, and EntityTwo has a reference to EntityThree. All three relationships are defined as follows:
Code:
   @ManyToOne(cascade = {}, fetch = FetchType.LAZY)
   @JoinColumn(name = "XX", unique = false, nullable = false, insertable = true, updatable = true)

In my code, I am trying to get to EntityThree this way:
Code:
entityOne.getEntityTwo().getEntityThree()

If I use FetchType.EAGER, this works perfectly. If I use LAZY, entityOne.getEntityTwo() returns a proxy, and getEntityThree() then fails:
Code:
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
    at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:57)
    at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
    at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:150)
    at com.ibi.entity.price.OperatingSystem$$EnhancerByCGLIB$$cee9d0f8.toString(<generated>)

Originally I had FetchType.EAGER, but I thought I'd switch to LAZY to improve performance (as EntityOne has a number of @ManyToOne relationships, and I usually don't need them all at any one time.) Clearly I am missing something elemental in how LAZY should work, and/or my coding technique is incorrect. Can someone point me in the right direction?



I'm not sure but can you just force the entityTwo to become a real object by referencing a method or something and then do the getEntityThree()?


Top
 Profile  
 
 Post subject: Re: Question about FetchType.LAZY
PostPosted: Mon Nov 08, 2010 3:41 pm 
Newbie

Joined: Mon Nov 08, 2010 3:09 pm
Posts: 4
Nope, that doesn't do anything.


Top
 Profile  
 
 Post subject: Re: Question about FetchType.LAZY
PostPosted: Tue Nov 09, 2010 7:39 am 
Beginner
Beginner

Joined: Wed Jan 07, 2009 7:07 pm
Posts: 26
Hi,
When you are trying to get entityOne.getEntityTwo() , as you said hibernate will return proxy. The proxy holds a reference of session object which was jused to fetch entity two. Now if that session is closed before you call getEntityThree(), you will get LazyInitializationException.

In case of Eager , real object and not proxy is returned and hence things will work fine.

Just suremake you keep the session open and you won't get this error.

Thanks
-Vishal


Top
 Profile  
 
 Post subject: Re: Question about FetchType.LAZY
PostPosted: Tue Nov 09, 2010 8:59 am 
Newbie

Joined: Mon Nov 08, 2010 3:09 pm
Posts: 4
How do I keep the session open when I'm executing objectOne.getObjectTwo().getObjectThree()?


Top
 Profile  
 
 Post subject: Re: Question about FetchType.LAZY
PostPosted: Tue Nov 09, 2010 10:00 am 
Beginner
Beginner

Joined: Wed Jan 07, 2009 7:07 pm
Posts: 26
You must have opened the session while fetching the objectOne. Don't close that session, when you are calling one.getTwo.getThree.

Thanks
-Vishal


Top
 Profile  
 
 Post subject: Re: Question about FetchType.LAZY
PostPosted: Tue Nov 09, 2010 10:04 am 
Newbie

Joined: Mon Nov 08, 2010 3:09 pm
Posts: 4
gaur wrote:
You must have opened the session while fetching the objectOne. Don't close that session, when you are calling one.getTwo.getThree.

Thanks
-Vishal

Sorry, but that makes no sense to me. We are using Hibernate and Seam. I don't open or close sessions.

Well, let me amend that: I inject EntityManager into my EJB. That is standard.


Top
 Profile  
 
 Post subject: Re: Question about FetchType.LAZY
PostPosted: Tue Nov 09, 2010 10:07 am 
Newbie

Joined: Fri Nov 05, 2010 1:36 pm
Posts: 11
comment removed.


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