Hibernate Criteria Order By

Dus*_*lmi 15 java hibernate criteria

我有一个名为Gift的表,它与一个名为ClickThrough的表有一对多的关系 - 表示特定Gift被点击的次数.我需要查询按ClickThrough计数排序的所有Gift对象.我不需要返回ClickThrough计数,因为我不需要做任何事情,我只想将它用于订购目的.

我需要查询直接返回一个Gift对象列表,只是按ClickThrough计数排序.如何使用Criteria API执行此操作?我可以在这里找到很多关于类似信息的文档,但没有什么比我需要的更多.

mus*_*n3d 28

注意通过这里来到这里寻找按房产/列排序的任何其他人:

使用此处提到的方法时,未找到任何结果.修复是用来criteria.addOrder(Order.asc(property));代替.注意区别在于使用addOrder,而不是add;

我在这里跑了几次以便快速参考这个问题.


Sti*_*ens 11

如果要返回实体或属性列表以及计数,则需要一个分组依据.在标准中,这是通过ProjectionList和完成的Projections.例如

    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
Run Code Online (Sandbox Code Playgroud)

但是,由于您正在使用,ProjectionList您还需要使用AliasToBeanConstructorResultTransformer.