How to make Divide (column1 / column2) in sleep criteria predictions

I am new to Hibernate. I need to fulfill select item.itemName, (item.Qty * item.Price)as the total price from the item’s position in the request for Hibernate criteria. I tried,

objCriteria = objSession.createCriteria(ItemVO.class, "IT")
.setProjection(Projections.projectionList()
.add(Projections.property("IT.name"), "itemName")
.add(Projections.sqlProjection("(QTY * cost)", new Float[] {"TotalCost"}, ( new Type[] {Hibernate.Float}))))
.setResultTransformer(Transformers.aliasToBean(ConsumableDTO.class));

But I need with HQL name instead of direct sql query column name. how to reach it

+3
source share
1 answer

Not sure if you are still interested in this, but I believe that you can make it work by putting the keyword "sum" in your sqlProjection.

Then your request will look like this:

objCriteria = objSession.createCriteria(ItemVO.class, "IT")
.setProjection(Projections.projectionList()
.add(Projections.property("IT.name"), "itemName")
.add(Projections.sqlProjection("sum(QTY * cost)", new Float[] {"TotalCost"}, ( new Type[] {Hibernate.Float}))))
.setResultTransformer(Transformers.aliasToBean(ConsumableDTO.class));

This will return an Object [] for each element that matches, with a name in the first element of the array and computed "totalCost" in the second.

+2

Source: https://habr.com/ru/post/1713042/


All Articles