'Querydsl JPA creates multiple exists queries for collection joins
I have an entity Bookstore , which has one to many relation with Book:
@OneToMany(fetch = FetchType.LAZY, mappedBy = "bookStore", cascade = {CascadeType.PERSIST, CascadeType.MERGE})
private List<Book> books;
entity Book has multiple attributes name, author, code... I would like to search all bookstores, which has book with name like '%Love%' or category like '%Love%'. So QueryDSL predicate looks like:
BooleanExpression conditionHelper = Expressions.asNumber(1).eq(2);
conditionHelper = conditionHelper.or(qBookstore.book.name.contains(search)).or(qBookstore.qBook.qCategory.contains(search));
where search is 'Love'.
QueryDSL generates 2 exists queries, instead of 1. Is it possible to generate 1 query?
or exists (
select
1
from
bookstore bookstore_
cross join book book_
where
book_.bookstore_uuid = bookstore_.uuid
and
lower(book_.name) like '%Love%' escape '!' )
or exists(
select 1 from bookstore bookstore_ cross join book book_ where book_.bookstore_uuid = bookstore_.uuid and lower(book_.category) like '%Love%' escape '!' )
instead of exists ( select 1 from bookstore bookstore_ cross join book book_ where book_.bookstore_uuid=bookstore_.uuid and (lower(book_.name) like '%Love%' escape '!' or lower(book_category) like '%Love%' escape '!'))
)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
