是否可以在工厂表达式中插入案例生成器?

Is it possible to insert case builder inside factory expression?

有没有办法将案例生成器放入工厂表达式中?

public List<StockRecoveryDTO> FindTest(@Param("id_product") Long id_product, @Param("date") String date) {
    JPAQuery<StockRecoveryDTO> stockRecoveryDTOJPQLQuery = new JPAQuery<>(entityManager);
    QManagement management = QManagement.management;
    QProduct product = QProduct.product;

    FactoryExpression<StockRecoveryDTO> fe = new QStockRecoveryDTO(
            product.id_product,
            management.date,
            product.quantity_product,
            management.action_description,
            management.id_action,
            management.quantity
    );

这个:

NumberExpression<Integer> numberExpression = new CaseBuilder()
        .when(management.action_description.eq("import"))
        .then(product.quantity_product)
        .otherwise(product.quantity_product.negate()).sum();

变成这样:

        return stockRecoveryDTOJPQLQuery.select(fe)
                .from(management)
                .innerJoin(product)
                .on(management.product_id.eq(product))
                .where(product.id_product.eq(id_product).and(management.date.eq(date)))
                .groupBy(management.product_id)
                .fetch();

    }
}

Target --> 如何将我的案例生成器放入工厂表达式中,我找不到任何相关文档。或者也许还有其他更好的方法?

您可以将 case-builder 结果包装在数字表达式中并对其调用求和:

Expressions.asNumber(
    new CaseBuilder()
        .when(actionDescription.eq("import").then(product.quantity)
        .otherwise(product.quantity.negate())
).sum()
  1. 检查什么是 BooleanBuilder 以及如何使用它。
  2. 您可以尝试使用 if-else 语句。