openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jay McHugh <jaydmch...@gmail.com>
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:

<example>

// 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");

</example>

Also take a look at the references in this thread:
http://n2.nabble.com/%27Masking%27-fields-in-an-Entity-tp2545263p2546695.html

Hope that helps,

Jay

On Mon, Apr 6, 2009 at 11:23 PM, Craig L Russell <Craig.Russell@sun.com> 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.
>> http://www.6degrees.com
>> mailto:daryl@6degrees.com
>
> Craig L Russell
> Architect, Sun Java Enterprise System http://db.apache.org/jdo
> 408 276-5638 mailto:Craig.Russell@sun.com
> P.S. A good JDO? O, Gasp!
>
>

Mime
View raw message