QueryDsl BooleanBuilder:如何创建比较另一个字段的谓词?

问题描述 投票:1回答:1

我有一个简单的示例:查找所有售罄的商品。

项目具有initialQuantity和soldQuantity整数字段。(由于某种原因,我需要存储initialQuantity)

我正在尝试做类似的事情:

builder = new BooleanBuilder();
Predicate predicate = builder.and(item.initialQuantity.eq(item.soldQuantity)).getValue();
Iterable<Item> iterable = itemRepository.findAll(predicate);

但是它没有按预期返回。 “ .eq()”看起来像是一个整数,而不是Path <>

java sql querydsl boolean-expression
1个回答
0
投票

首先声明它:

JPAQueryFactory query = new JPAQueryFactory(em);
BooleanBuilder booleanBuilder = new BooleanBuilder();

QCreditRequest creditRequest = QCreditRequest.creditRequest;

然后,您可以包括您的业务逻辑,例如:

    booleanBuilder.and(creditRequest.customer.in(dataRequest.getCustomerListToSearchFor()));
          booleanBuilder.and(creditRequest.creditRequestId.eq(dataRequest.getCreditRequestId()));

如果(StringUtils.isNotBlank(dataRequest.getProductId()))booleanBuilder.and(creditRequest.product.productId.eq(dataRequest.getProductId()));

finally return the result, mapped to your 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());
© www.soinside.com 2019 - 2024. All rights reserved.