openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jay McHugh <>
Subject Re: Multiple join fetches
Date Tue, 07 Apr 2009 05:11:24 GMT
Hey Daryl,

If you will always want the query to return all levels, you can also
try annotating the relations as 'fetch=FetchType.EAGER'.  Then simply
doing your select as:

select gp from GrandParent as gp

should also grab the parents and children without having to explicitly
fetch join them.

If that doesn't work - or you (cannot/do not want to) eagerly fetch
the relationship all of the time - a quick stab at what using fetch
groups might look like this:


// assuming you already have an entity manager named 'entityManager'
OpenJPAEntityManager em = OpenJPAPersistence.cast(entityManager);

FetchPlan plan = em.getFetchPlan();
plan.addField(GrandParent.class, "parents");
plan.addField(Parent.class, "children");

Query query = em.createQuery("select gp from GrandParent as gp");


Also take a look at the references in this thread:

Hope that helps,


On Mon, Apr 6, 2009 at 11:23 PM, Craig L Russell <> wrote:
> Hi Daryl,
> On Apr 6, 2009, at 8:03 AM, Daryl Stultz wrote:
>> Hey all,
>> I'm new to JPA. I'm trying to learn to write queries. Suppose I have
>> GrandParents, Parents and Children entities with the obvious relationship.
>> How do I write a query that fetches all three levels? I want something
>> like
>> this:
>> select gp from GrandParent as gp
>> from GrandParent
>> join fetch gp.parents
>> join fetch gp.parents.children
>> The first fetch works (if I comment out the second) but the second
>> doesn't.
>> I've tried a number of variations but can't seem to get the second level
>> to
>> fetch.
>> (Posted the above to general JPA forum.)
>> I am told JPA 1.0 supports only one level fetch. Is there some OpenJPA
>> extension for fetching more? I don't really need this right now, I'm just
>> trying to understand it all.
> Fetch groups are intended to support this use case. Not JPA standard, and
> also not guaranteed to fetch all instances in one database round trip. But
> it might work for you.
> Craig
>> Thanks.
>> --
>> Daryl Stultz
>> _____________________________________
>> 6 Degrees Software and Consulting, Inc.
> Craig L Russell
> Architect, Sun Java Enterprise System
> 408 276-5638
> P.S. A good JDO? O, Gasp!

View raw message