• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Use Join Fetch but still obtain LAZY exception

 
Derek Bright
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • 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!
 
Reza Rahman
author
Ranch Hand
Posts: 580
5
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Ranch Hand
Posts: 580
5
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
are there any more comments for the code?
 
Zik Zikit
Greenhorn
Posts: 1
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic