How to left join unrelated entities?

2020-08-23 04:13发布

问题:

When i try to run a query like this:

QA A = QA.a;
QB B = QB.b;
...
from(A)
.leftJoin(B).with(B.name.eq(A.nameSomething));

(A and B entities are not related) I'm always getting this error:

Caused by: org.hibernate.hql.ast.QuerySyntaxException: Path expected for join!

I would like to be able to left join unrelated entities using querydsl. Is it possible at all? Or the only way is to write a native Oracle query?

回答1:

Joining unrelated entities is not possible with Querydsl JPA. Querydsl JPA uses JPQL internally and inherit's this restriction.



回答2:

Assuming you're referring to JPA Criteria. innerJoin, leftOuterJoin are for joining relationships, since that is the only logical thing you would join on in an O-O system.

If instead you want to compare equality of entity.field with entity2.field (where no FK exists in the database and no relation exists in the classes) then do that in the WHERE clause, just like any filter.

Obviously QueryDSL has its own syntax/rules so if referring to that then this is not JPA



标签: jpa hql querydsl