【问题标题】:Is it possible to insert case builder inside factory expression?是否可以在工厂表达式中插入案例生成器?
【发布时间】:2021-09-30 08:37:50
【问题描述】:

有没有办法将 case builder 放入工厂表达式中?

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

【问题讨论】:

    标签: java spring jpa hql querydsl


    【解决方案1】:

    您可以将 case-builder 结果包装在 Number 表达式中并在其上调用 sum:

    Expressions.asNumber(
        new CaseBuilder()
            .when(actionDescription.eq("import").then(product.quantity)
            .otherwise(product.quantity.negate())
    ).sum()
    

    【讨论】:

    • 你怎么把它放在我的工厂表达式中?我认为这是误导对不起
    【解决方案2】:
    1. 检查什么是 BooleanBuilder 以及如何使用它。
    2. 您可以使用 if-else 语句。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多