-->
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.  [ 4 posts ] 
Author Message
 Post subject: LazyInitilizationException with JOIN FETCH
PostPosted: Thu Feb 12, 2009 4:44 pm 
Newbie

Joined: Fri Jun 02, 2006 2:49 pm
Posts: 13
I am afraid this has been asked to many times, sadly I seem to get unique problems.

I load an object graph with HQL (or is it JPQL now) and walk down the object graph, when I try and walk back up the object graph I get the infamous LazyInitilizationException. "Failed to lazy initialize a collection of role: com.foo.bar.GrandChld.children no session or session is was closed".

the object graph looks something like

Code:
parent
   |- Child[]
         |-grandChild



Upside down it would be

Code:
grandChild
   |- child[]
         |-parent


The code looks like this

Code:
grandChild = parent.getChildren().getGrandChild()
grandChild.getChildren().size();  //exception here


These are all sets

Parent is a one-to-many relationship to Child
Child is a many-to-one relationship to Parent

Child is a many-to-one relationship to Grandchild
Grandchild is a one-to-many relationship to Child.

I use an JPQL fetch to get the object graph

Code:
SELECT parent FROM Parent AS parent
LEFT JOIN FETCH parent.children AS child
LEFT JOIN FETCH child.grandChild AS grandChild
WHERE parent.id = '154123123'


So my confusion comes into why I can't walk back up the graph I just walked down.

I was able to get it to work using this JPQL but it really looks wrong to be
fetching the same stuff I just got.

Code:
SELECT parent FROM Parent AS parent
LEFT JOIN FETCH parent.children AS child
LEFT JOIN FETCH child.grandChild AS grandChild
LEFT JOIN FETCH grandChild.children


I have this feeling I am doing something wrong. I would like to figure out why the first JPQL does not work.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 12, 2009 5:32 pm 
Senior
Senior

Joined: Thu Jan 08, 2009 3:48 pm
Posts: 168
Shouldn't child to grandchildren be one-to-many
and
grandchildren to child many-to-one?

Otherwise it would mean that the several children could have the same grandchild.

The point with your first query is, you only retrieve one(1) grandchild which can have an (yet unknown) number of associated children.

So your solution is actually right for your problem though i still would suggest reconsiderung your mapping.

Rating is welcome.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 12, 2009 6:24 pm 
Newbie

Joined: Fri Jun 02, 2006 2:49 pm
Posts: 13
Sadly the relationship is right, maybe some fancy mappings could be used. Ideally it would be done with a join table but the child object contains data I need other then the FK to parent and grandChild.

The data looks like this

Parent A -> child A -> grandChild A
child B -> grandChild B

Parent B -> child C -> grandChild B
child D -> grandChild C

parent C -> child E -> grandChild C
child F -> grandChild D


so when walking up the tree after I have walked down it, why is the child association not know, I just used it.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 13, 2009 1:39 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
jamesconf wrote:
grandChild.getChildren().size(); //exception here

So my confusion comes into why I can't walk back up the graph I just walked down.

If I understand your data model, here you are not walking back up, but you are trying to walk further down the tree and thats why you are getting LazyIntialization exception.

But in the second HQL you are retriving the grangChild's children also and so you are able to get the size of the collection.

If you feel that I have mis-understanding of your data model, then plz post the mappings as well.

_________________
Regards,
Litty Preeth


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