Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to join when building Predicate(s), but without having a JPAQuery instance

Tags:

java

querydsl

When building a Predicate for the Entity Book, I would like to be able to left join on Category (ManyToMany) to add a AND Predicate on Category. I could simply achieve that if I have the JPAQuery instance :

if (catId != null) {
    jpaQuery.leftJoin(book.categories, category);
    jpaQuery.where(category.id.eq(catId).or(category.parentCategory.id.eq(catId)));
}

But when building Predicates I don't have yet the JPAQuery. So for the Predicate itself I could do :

booleanBuilder.and(category.id.eq(this.categoryId).or(category.parentCategory.id.eq(this.categoryId)));

But for the leftjoin how to proceed without the jpaQuery instance ?

like image 329
Gauthier Peel Avatar asked Nov 23 '12 18:11

Gauthier Peel


1 Answers

You need the Query to declare the left join in Querydsl. If this is Spring Data related, they might come up with an API level solution.

book.categories.any() can be used instead of category, but it is serialized differently to JPQL, with a subquery instead of a join.

like image 119
Timo Westkämper Avatar answered Nov 11 '22 09:11

Timo Westkämper