Been a while since I posted on the forums, Hibernate's been working great for us, and we've been able to address any issues ourselves. However, once in a blue moon one of our developers will come to me with an interesting behaviour they are seeing. Specifically, some of the objects in a collection attribute loaded using a 'join fetch' throw LazyInitialization exception when accessed on the web tier (Session long since closed). The strange thing, is that sometimes the same query will work and at other times it will not.
Today I encountered the same behaviour myself, luckily with only two HQL having been run! The 2nd HQL being the one that returns the results we want to use.
The tables structure is of the general form:
A <--> B (one to many)
B <--> C (one to many)
Relationships are setup in a bi-directional fashion and everything is proxy-able.
The first HQL queries objects C, _without_ fetching B. This results with C having a non-initialized proxy to B.
The second HQL queries objects A, _with_ fetching B. This results with A having an initialized collection of B objects.
What I notice then, is that some of the B in the collection from the 2nd query are actually non-initialized proxy objects. It seems to me the first query is loading a non-initialized proxy into the Session, that is then being placed into the collection fetched by the second query.
Note: If I do not run the first query, all objects in the second query are initialized.
My ultimate question then is: Does a fetch join only guarentee the Collection is initialized, but not that the objects in the collection are initialized? (even though the fetch loads the data needed initialized the objects)
I've searched the forums, scanned the documentation and my HiA, and I've not hit upon the magic answer yet. Best I reference I found was something by Max at the end of this post, but I was unable to find the comment in the javadoc he says he added.
http://forum.hibernate.org/viewtopic.php?t=935226&highlight=fetch+join+initialization+proxy+lazy
Humble apologies if this is answered elsewhere. I've not found it in any form that I recognized. It's not for lack of trying. I'm posting as a last resort.
Later,
gus