• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Use Join Fetch but still obtain LAZY exception

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
author
Posts: 580
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Derek Bright
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 580
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Derek,

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

Cheers,
Reza
 
Derek Bright
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
are there any more comments for the code?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://pronicles.blogspot.com/2009/09/orghibernatelazyinitializationexception.html
reply
    Bookmark Topic Watch Topic
  • New Topic