Search code examples
javaspringjpahqlquerydsl

Is it possible to insert case builder inside factory expression?


Is there a way to put 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
    );

this:

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

into this:

        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 --> How to put my case builder inside to factory expression, I can't find any related documentation. Or maybe there is another better way?


Solution

    1. Check what is BooleanBuilder and how can you use it.
    2. You can play around with if-else statements.