4

I'm trying to efficiently fetch a two-level nested association using OpenJPA 2.2. Basically, I have one A that has one B that has many C:

@Entity
public class A {
    ...

    @ManyToOne(fetch=FetchType.EAGER)
    private B b;
}

@Entity
public class B {
    ...

    @OneToMany(mappedBy="b", fetch=FetchType.EAGER)
    private List<C> cs;
}

@Entity
public class C {
    ...
}

My JPQL query is as follows:

SELECT a
FROM A a
WHERE a.id = some condition

I set the EagerFetchMode to "PARALLEL" in order to have a second query that retrieves the Cs of all the Bs:

<property name="openjpa.jdbc.EagerFetchMode" value="parallel"/>
<property name="org.apache.openjpa.persistence.jdbc.EagerFetchMode" value="PARALLEL"/>

The problem is that openjpa is generating n+1 queries instead of 2: one for the As and Bs and one for each C:

SELECT *
FROM a
LEFT OUTER JOIN b 
    ON a.b_id = b.id 
WHERE some condition

and n times

SELECT *
FROM c 
WHERE c.b_id = x

How can I make openjpa issue two queries instead of n+1?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.