简体   繁体   English

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

[英]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.

相关问题 QueryDsl BooleanBuilder:如何创建一个比较列表内容的谓词? - QueryDsl BooleanBuilder: How to create a predicate that compares contents of list? java Querydsl BooleanBuilder OneToMany查询 - java Querydsl BooleanBuilder OneToMany query 如何使用带有Spring数据的Querydsl谓词(BooleanExpression)按计算字段进行过滤? - How to filter by calculated field using Querydsl Predicate (BooleanExpression) with Spring Data? QueryDSL:如何从案例表达式创建谓词? - QueryDSL: How can I create a predicate from a case expression? 如何向querydsl Predicate添加比较器? - How to add a comparator to querydsl Predicate? 是否可以使用原始 SQL 字符串创建 QueryDSL 谓词? - Is it possible to create a QueryDSL Predicate with a String of raw SQL? 如何在Java中使用QueryDSL,Spring-Data,MongoDB创建嵌入式谓词 - How to create embedded predicate using QueryDSL, Spring-Data, MongoDB in Java 如何使用 Spring 中的 QueryDSL 谓词处理日期范围? - How to handle date ranges with QueryDSL Predicate in Spring? 我有一个 Querydsl 问题。 (壮举。BooleanBuilder) - I have a Querydsl question. (Feat. BooleanBuilder) QueryDsl - 创建一个谓词来检查 localdate 是否是当前周的一部分 - QueryDsl - Create a predicate to check if localdate is part of the current week
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM