简体   繁体   中英

Criteria api multiple projection

I have a problem with hibernate criteria api. I am trying to get two sum projections. However I always get only one column result with the second requested sum (for instant i.totalCostPrice), if I change the order of the sums in code, I will get the second one (i.totalPrice), but never both of them as I require. Does anybody know the solution please?

DetachedCriteria totalSumsCriteria = DetachedCriteria.forClass(Invoice.class, "i");
ProjectionList pList = Projections.projectionList();

pList.add(Projections.alias(Projections.sum("i.totalPrice"), "totalListPriceSum"));

pList.add(Projections.alias(Projections.sum("i.totalCostPrice"), "totalCostPriceSum"));

totalSumsCriteria.setProjection(pList);
        totalSumsCriteria.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);

the result is [589704.0], I expect [6127123.0, 589704.0], if I swap adding sum projectiosn, I get [6127123.0]

in SQL it would be something like SELECT SUM(total_price), SUM(total_cost_price) FROM invoice

If anyone is interested, the problem is with the DetachedCriteria. It works well with Crieteria, but not with DetachedCriteria.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM