This is a tool that helps you create queries with JPA using JPQL or native SQL
Add this dependency in your pom.xml
<dependency>
<groupId>io.github.andersoncrocha</groupId>
<artifactId>jpql-query-builder</artifactId>
<version>1.2.5</version>
</dependency>
public List<Order> findOrdersWithItemsByClientAndSellerIfNotNull(Client client, Seller seller) {
return QueryBuilder.newQuery(entityManager)
.from(Order.class, "order")
.joinFetch("order.items")
.where("order.client = :client", client)
.whereIf("order.seller = :seller", seller, Objects.nonNull(seller))
.getResultList(Order.class);
}