GeeCON Prague 2014*
The moose likes EJB and other Java EE Technologies and the fly likes Use Join Fetch but still obtain LAZY exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Use Join Fetch but still obtain LAZY exception" Watch "Use Join Fetch but still obtain LAZY exception" New topic
Author

Use Join Fetch but still obtain LAZY exception

Derek Bright
Greenhorn

Joined: Aug 28, 2008
Posts: 28
Hi, i am having some problems with retrieving my entity. The error i got is:

Exception Description: An attempt was made to traverse a relationship using indirection that had a null Session. This often occurs when an entity with an uninstantiated LAZY relationship is serialized and that lazy relationship is traversed after serialization. To avoid this issue, instantiate the LAZY relationship prior to serialization.

Part of the session bean code:

Query q = em.createQuery("SELECT DISTINCT subProgT FROM SubTrainingProgTemplate subProgT " +
"LEFT JOIN FETCH subProgT.trainingActivityTemplates " +
"WHERE subProgT.id =:subProgTempId");

Well, i try to retrieve the entity in the servlet where i do this:

System.out.println("Size: " + xxx.getSubTrainingProgTemplate(new Long(xx)).getTrainingActivityTemplates().size());

Even with the use of Left Join Fetch, i still encounter the error above, which doesn't really make sense to me.

However, by adding 1 more line of code in the session bean:
xx.getTrainingActivityTemplates().size();

**The error somehow vanish, and i was able to retrieve the size value in servlet. I don't know what's happening, can anyone help me here? Thanks!
Reza Rahman
author
Ranch Hand

Joined: Feb 01, 2005
Posts: 580
    
    5
Derek,

Your code is fine. Make sure you have redeployed the EJB JAR correctly. Do a server restart if necessary. If that doesn't work, it looks like you have a persistence provider bug. In such a case, it is best to pursue this with the vendor. A lot of major JPA vendors still seem to have trouble implementing the simple concept of lazy/eager loading...

Some of them have definitely gotten it right. If you can forgive me I'd rather not discuss who they are...in any case, switching JPA providers should be a simple matter of dropping a JAR and changing a few configuration parameters around.

Regards,
Reza


Independent Consultant — Author, EJB 3 in Action — Expert Group Member, Java EE 6 and EJB 3.1
Derek Bright
Greenhorn

Joined: Aug 28, 2008
Posts: 28
Hi Reza,

Thanks for your reply, actually, i have another code exactly the same as the above and works perfectly fine, but somehow, for this entity, this problem simply persist even when i redeploy, restart computer, everything i try, but to no avail apart from adding the .getsize code which did nothing but somehow break away from the error.
Reza Rahman
author
Ranch Hand

Joined: Feb 01, 2005
Posts: 580
    
    5
Derek,

Can you post the code for getTrainingActivityTemplates? Are you accidentally loading another relationship that is still lazily loaded?

Cheers,
Reza
Derek Bright
Greenhorn

Joined: Aug 28, 2008
Posts: 28
here goes:

this is the full code

public SubTrainingProgTemplateEntity getSubTrainingProgTemplate(Long id)
throws ExistException {

try {
Query q = em.createQuery("SELECT DISTINCT subProgT FROM SubTrainingProgTemplate subProgT " +
"LEFT JOIN FETCH subProgT.trainingActivityTemplates " +
"WHERE subProgT.id =:subProgTempId");

q.setParameter("subProgTempId", id);

SubTrainingProgTemplateEntity subProgTemp =
(SubTrainingProgTemplateEntity) q.getSingleResult();

//This code may seems redundant, but somehow
//without this line, the fetch in the sql doesn't seems to work
//Therefore, do not remove the system.out.println
//System.out.println("activity size: " + subProgTemp.getTrainingActivityTemplates().size());

return subProgTemp;

} catch (NoResultException nre) {
throw new ExistException("This template does not exist!");
}
}

** This is the code at the servlet.

remote.getSubTrainingProgTemplate(new Long(2)).getTrainingActivityTemplates().size());
Derek Bright
Greenhorn

Joined: Aug 28, 2008
Posts: 28
are there any more comments for the code?
Zik Zikit
Greenhorn

Joined: Dec 05, 2009
Posts: 1
http://pronicles.blogspot.com/2009/09/orghibernatelazyinitializationexception.html
 
GeeCON Prague 2014
 
subject: Use Join Fetch but still obtain LAZY exception