如何在休眠标准预测中进行Divide(column1 / column2)

时间:2022-11-10 04:31:00

I am new to Hibernate. I need to perform select item.itemName, (item.Qty * item.Price) as total Price from Item item' in Hibernate criteria query. i tried,

我是Hibernate的新手。我需要在Hibernate条件查询中执行select item.itemName,(item.Qty * item.Price)作为项目项的总价格。我试过了,

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 achieve it?

但我需要使用HQL名称而不是直接的sql查询列名。怎么实现呢?

1 个解决方案

#1


Not sure if you are still interested in this, but I believe you can get this working by actually putting the 'sum' keyword in your sqlProjection.

不确定你是否仍然对此感兴趣,但我相信你可以通过在你的sqlProjection中实际使用'sum'关键字来实现这一点。

Your query would then look like:

您的查询将如下所示:

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 should result in returning an Object[] for each item that matches, with the name in the first array element, and the computed 'totalCost' in the second.

这应该导致返回每个匹配的项目的Object [],第一个数组元素中的名称和第二个中的计算“totalCost”。

#1


Not sure if you are still interested in this, but I believe you can get this working by actually putting the 'sum' keyword in your sqlProjection.

不确定你是否仍然对此感兴趣,但我相信你可以通过在你的sqlProjection中实际使用'sum'关键字来实现这一点。

Your query would then look like:

您的查询将如下所示:

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 should result in returning an Object[] for each item that matches, with the name in the first array element, and the computed 'totalCost' in the second.

这应该导致返回每个匹配的项目的Object [],第一个数组元素中的名称和第二个中的计算“totalCost”。