You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by mjdenham <mj...@gmail.com> on 2009/02/05 11:48:07 UTC

left join fetch on Parents, Children, and GrandChildren

Hi, is there any way to do a left join fetch on Parents, Children, and
GrandChildren collections

The following works and fetches Parents and Children
   SELECT p FROM Parent p LEFT JOIN FETCH p.childCollection
but I would like to extend the left join fetch to the GrandChildren i.e.
children of Children.

Thanks in advance
Martin
-- 
View this message in context: http://n2.nabble.com/left-join-fetch-on-Parents%2C-Children%2C-and-GrandChildren-tp2274260p2274260.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Re: left join fetch on Parents, Children, and GrandChildren

Posted by catalina wei <ca...@gmail.com>.
Hi,
The  syntax for Fetch Join does not allow range variable - as defined in JPA
Spec:

Left Join BNF::
    *LEFT [OUTER] JOIN join_association_path_expression* *[AS]* *
identification_variable*

Fetch Join BNF:
    *fetch_join ::= [ LEFT [OUTER] | INNER ] JOIN FETCH *  *
join_association_path_expression*

Due to this syntax, the fetch join can only be done at one level.
 If you have 2 relations in EntityA, say x and y,   you can join fetch both
x and y as:
    FROM EntityA a LFET JOIN FETCH a.x LEFT JOIN FETCH a.y

This limitation is set in the JPQL syntax.

You could set the grandChildren relation fetchType EAGER so that when
children is fetched the EAGER relation gets loaded as well.

Catalina
On Thu, Feb 5, 2009 at 2:42 PM, mjdenham <mj...@gmail.com> wrote:

>
> This is how I would do it in Hibernate but I am new to JPQL and can't work
> out the equivalent syntax:
>   from Parent as parent
>       left join fetch parent.children child
>       left join fetch child.grandChildren
>
> Does anybody know how?  Thanks in advance.
> Martin
> --
> View this message in context:
> http://n2.nabble.com/left-join-fetch-on-Parents%2C-Children%2C-and-GrandChildren-tp2274260p2277843.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>
>

Re: left join fetch on Parents, Children, and GrandChildren

Posted by mjdenham <mj...@gmail.com>.
This is how I would do it in Hibernate but I am new to JPQL and can't work
out the equivalent syntax:
   from Parent as parent
       left join fetch parent.children child
       left join fetch child.grandChildren

Does anybody know how?  Thanks in advance.
Martin
-- 
View this message in context: http://n2.nabble.com/left-join-fetch-on-Parents%2C-Children%2C-and-GrandChildren-tp2274260p2277843.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Re: left join fetch on Parents, Children, and GrandChildren

Posted by Pinaki Poddar <pp...@apache.org>.
Hi,
> this resulted in hundreds of queries being performed.

Are you using build-time enhancement? If not, please do.
If that does not solve the problem, please post an example with the domain
classes, a test case and persistence.xml for us to look at.

[1]
http://openjpa.apache.org/builds/latest/docs/manual/manual.html#ref_guide_pc_enhance_build


-- 
View this message in context: http://n2.nabble.com/left-join-fetch-on-Parents%2C-Children%2C-and-GrandChildren-tp2274260p2311620.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Re: left join fetch on Parents, Children, and GrandChildren

Posted by mjdenham <mj...@gmail.com>.
Hi Pinaki,
I have tried using a fetch group to load the grandchildren but unfortunately
this resulted in hundreds of queries being performed.  The original query
was good in that it performed the left join fetch of parent and child
collections in a single query.

Is it possible to fetch parent, child, and grandchild collections in a
single query?

The first query I used:
   SELECT p FROM Parent p LEFT JOIN FETCH p.childCollection
is executed in a single query but does not fetch grand children

If I add 
    @FetchGroup(name="fetchGrandchildren", attributes={
        @FetchAttribute(name="grandchildCollection")
    })
to the above it executes hundreds of queries instead of one.



-- 
View this message in context: http://n2.nabble.com/left-join-fetch-on-Parents%2C-Children%2C-and-GrandChildren-tp2274260p2277556.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Re: left join fetch on Parents, Children, and GrandChildren

Posted by Pinaki Poddar <pp...@apache.org>.
Hi,
  Please take a look at OpenJPA's FetchPlan/FetchGroup [1] features. It has
powerful features to fetch customized sub-graph from a selected 'root'
entity.

[1]
http://openjpa.apache.org/builds/latest/docs/manual/manual.html#ref_guide_fetch


Pinaki
-- 
View this message in context: http://n2.nabble.com/left-join-fetch-on-Parents%2C-Children%2C-and-GrandChildren-tp2274260p2275410.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.