hibernate条件组查询错误

时间:2016-03-25 05:14:13

标签: java hibernate hibernate-criteria

我正在尝试使用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字段被列出两次?

1 个答案:

答案 0 :(得分:2)

  

我的问题是为什么ORDER_NUMBER字段被列出两次?

您的条件已经Projections.groupProperty("orderNumber")。因此,生成的查询将在其select子句中包含orderNumber

我认为没有必要明确指定Projections.property("orderNumber")。你可以取消它并尝试。

同样适用于使用groupProperty(..)指定的任何其他字段。