[英]QueryDsl BooleanBuilder: How to create a predicate that compares another field?
I have simple example: to find all items that are sold out.我有一个简单的例子:查找所有售罄的商品。
Item has initialQuantity and soldQuantity integer fields. Item 具有 initialQuantity 和 sellQuantity 整数字段。 (for some reason I need initialQuantity stored) (出于某种原因,我需要存储初始数量)
I'm trying to do something like:我正在尝试执行以下操作:
builder = new BooleanBuilder();
Predicate predicate = builder.and(item.initialQuantity.eq(item.soldQuantity)).getValue();
Iterable<Item> iterable = itemRepository.findAll(predicate);
but it does not return as expected.但它没有按预期返回。 ".eq()" looks like it expects an integer and not Path<> “.eq()”看起来像是一个整数而不是 Path<>
First declare it :首先声明它:
JPAQueryFactory query = new JPAQueryFactory(em);
BooleanBuilder booleanBuilder = new BooleanBuilder();
QCreditRequest creditRequest = QCreditRequest.creditRequest;
Then, you can include your business logique, for example :然后,您可以包含您的业务逻辑,例如:
booleanBuilder.and(creditRequest.creditRequestId.eq(dataRequest.getCreditRequestId()));
if (StringUtils.isNotBlank(dataRequest.getProductId())){
booleanBuilder.and(creditRequest.product.productId.eq(dataRequest.getProductId()));}
finally return the result, mapped to your DTO :最后返回结果,映射到您的 DTO :
return new HashSet<>(
query
.select(
Projections.constructor(
APResponse.class,
creditRequest.creditRequestId,
creditRequest.creditRequestDate,
creditRequest.product.productId,
creditRequest.creditRequestComment))
.from(creditRequest)
.innerJoin(creditRequest.product, product)
.innerJoin(creditRequest.status, creditRequestStatus)
.where(booleanBuilder)
.fetch());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.