表达式在休眠条件 [英] Expressions in hibernate criteria
本文介绍了表达式在休眠条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有一种方法可以构建一个计算数量*价格总和的Criteria?
解决方案
我认为你可以也使用SQL投影。它应该是这样的:
session.createCriteria(Item.class)
.createAlias(item, (Projections.groupProperty(i.id))
.add(Projections.groupProperty(i.price)) )
.add(Projections.groupProperty(i.quantity))
.add(Projections.sqlProjection(
price * quantity as total),
new String [] {total},
新类型[] {Hibernate.DOUBLE}
)
)
);
Ori
Let's say I have a persistent class Item with a quantity field and a price field. Is there a way to build a Criteria that calculates the sum of quantity*price?
解决方案
I think you can also use an SQL projection. It should be something like:
session.createCriteria(Item.class)
.createAlias("item", "i")
.setProjection( Projections.projectionList()
.add( Projections.groupProperty("i.id") )
.add( Projections.groupProperty("i.price") )
.add( Projections.groupProperty("i.quantity") )
.add( Projections.sqlProjection(
"price * quantity as total",
new String[] { "total" },
new Type[] { Hibernate.DOUBLE }
)
)
);
Ori
这篇关于表达式在休眠条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文