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.