I have four tables:
RootNode // Will return multiple root nodes
SubNode // Will return one sub node per root node
SubNodeChildren1 // Will return multiple for each sub node
SubNodeChildren2 // Will return multiple for each sub node
and a similar entity structure:
RootNode -> SubNode -> SubNodeChildren1
-> SubNodeChildren2
I need one query that will return all the RootNodes in the table with its SubNode and SubNode children initialized. The SubNode is eagerly fetched, but the SubNode children is lazy fetched.
I know how to write a query that will LEFT OUTER JOIN FETCH the immediate children of a table and initialize them accordingly. However, I have no idea of how to grab the children of a table that is eagerly fetched from the top-level table.
I have tried something like:
SELECT rn FROM RootNode AS rn LEFT OUTER JOIN FETCH rn.SubNode.SubNodeChildren1
but, this always gives me an error that the owner is not part of the SELECT.
Any help is greatly appreciated.
Here goes Hibernate reference
Which implies your query should be re-written as
You can either
disable default subNode fetch=FetchType.EAGER and just retrieve what you really want by using HQL query – It (HQL query) effectively overrides the outer join and lazy declarations of the mapping file for associations and collections (Hibernate reference documentation). This approach is supported by POJO in Action book.
or enable the collection of SubNodeChildren as fetch=FetchType.EAGER
The following has been extracted from the Hibernate FAQ (The link has been disabled (As far as i know) but i have saved before disappearing)
In an MVC application, how can we ensure that all proxies and lazy collections will be initialized when the view tries to access them ?
…