Hibernate Criteria Order By

后端 未结 3 646
执笔经年
执笔经年 2021-01-04 02:58

I have a table called Gift, which has a one-to-many relationship to a table called ClickThrough - which indicates how many times that particular Gift has been clicked. I nee

3条回答
  •  春和景丽
    2021-01-04 03:21

    If you want to return a list of entities or properties in combination with a count you will need a group by. In criteria this is done through ProjectionList and Projections. e.g.

        final ProjectionList props = Projections.projectionList();
        props.add(Projections.groupProperty("giftID"));
        props.add(Projections.groupProperty("giftName"));
        props.add(Projections.count("giftID"), "count"); //create an alias for the count
        crit.setProjection(props);
    
        crit.add(Order.desc("count")); //use the count alias to order by
    

    However, since you're using ProjectionList you will also need to use AliasToBeanConstructorResultTransformer.

提交回复
热议问题