Jay
Jay

Reputation: 65

QueryDSL join on multiple entities with one way relationships

I have some entities similar to the following (psuedocode)

class A {
    Integer aId;
}

class B {
    Integer bId;

    @ManyToOne
    A a;
}

class C {
    Integer cId;

    @ManyToOne
    A a;
}

I'd like to use QueryDSL to get a list of B based on criteria in C. I'd like not to have to create a Set of B or a Set of C in A.

If I do

query.from(b, c).innerJoin(b.a, a).fetch().innerJoin(c.a, a).
    where(c.cId.eq(1)).list(b);

then, as expected, I get a cross join.

If I do

query.from(b).innerJoin(b.a, a).fetch().innerJoin(c.a, a).
    where(c.cId.eq(1)).list(b);

then, as expected, I get an "Undeclared path" error.

I can do

query.from(b, c).innerJoin(b.a, a).fetch().innerJoin(c.a, a).
    where(c.cId.eq(1)).where(c.a.aId.eq(a.aId).list(b);

This keeps the cross join but does limit the results based on the where clause. I wonder if there is a way to do this without the cross join.

Upvotes: 1

Views: 1426

Answers (1)

Timo Westkämper
Timo Westkämper

Reputation: 22200

Without changes to the entity types you will need to use a cross join to connect the entities in the query.

Upvotes: 1

Related Questions