How to join while creating Predicate (s), but without a JPAQuery instance

When creating a predicate for an entity book, I would like to be able to left join the category (ManyToMany) to add an I-Predicate to the category. I could just achieve this if I have a JPAQuery instance:

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

But when creating Predicates, I don't have JPAQuery yet. Therefore, for the Predicate itself, I could do:

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

But for leftjoin, how to act without jpaQuery instance?

+4
source share
1 answer

You need a query to declare a left join in Querydsl. If it's Spring Data related, they might find an API level solution.

book.categories.any () can be used instead of a category, but it serializes differently with JPQL, with a subquery, and not with a join.

+1
source

Source: https://habr.com/ru/post/1448012/


All Articles