我尝试使用 Criteria 生成以下查询:
select order_number, order_date, sum(order_amount)
from st_pur_orders
group by ORDER_NUMBER,ORDER_DATE;
这是我的标准:
Criteria cr = session.createCriteria(StPurOrders.class);
cr.setProjection(Projections.projectionList()
.add(Projections.property("orderNumber"), "orderDate")
.add(Projections.sum("orderAmount"))
.add(Projections.groupProperty("orderNumber"))
.add(Projections.groupProperty("orderDate")));
cr.setResultTransformer(Transformers.aliasToBean(PurOrderColl.class));
list = cr.list();
但是在后台生成的查询是这样的:
select this_.ORDER_NUMBER as y0_, sum(this_.ORDER_AMOUNT) as y1_,
this_.ORDER_NUMBER as y2_, this_.ORDER_DATE as y3_
from STOCK.ST_PUR_ORDERS this_
group by this_.ORDER_NUMBER, this_.ORDER_DATE;
我的问题是为什么 ORDER_NUMBER
字段被列出两次?
最佳答案
My Question is why is the ORDER_NUMBER field being listed twice?
您的条件已具有 Projections.groupProperty("orderNumber")
。因此,生成的查询将在其 select 子句中包含 orderNumber
。
我认为不需要显式指定 Projections.property("orderNumber")
。您可以删除它并尝试一下吗?
这同样适用于使用 groupProperty(..)
指定的任何其他字段。
关于java - hibernate criteria group查询错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36214412/