Hibernate标准顺序依据 [英] Hibernate Criteria Order By
问题描述
我有一张名为Gift的表格,它与一个名为ClickThrough的表格有一对多的关系 - 表示特定礼物被点击了多少次。我需要查询所有Gift对象,按ClickThrough计数排序。我不需要返回的ClickThrough计数,因为我不需要做任何事情,我只是想用它来订购。
我需要查询直接返回礼物对象列表,只需按ClickThrough计数排序即可。我如何使用Criteria API来做到这一点?我可以在这里找到很多有关此类信息的文档,但没有什么比我需要的更多。 解决方案
如果你想要返回一个实体或属性列表,以及您将需要分组的计数。在标准中,这通过 ProjectionList
和 Projections
完成。例如
final ProjectionList props = Projections.projectionList();
props.add(Projections.groupProperty(giftID));
props.add(Projections.groupProperty(giftName));
props.add(Projections.count(giftID),count); //为计数创建一个别名
crit.setProjection(道具);
crit.add(Order.desc(count)); //使用计数别名来命令
但是,由于您使用 ProjectionList
您还需要使用 AliasToBeanConstructorResultTransformer 。
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 need to query for all of the Gift objects, ordered by ClickThrough count. I do not need the ClickThrough count returned, as I don't need anything done with it, I just want to use it for purposes of ordering.
I need the query to return a List of Gift objects directly, just ordered by ClickThrough count. How do I do this using the Criteria API? I can find a lot of documentation out there on similar information to this, but nothing quite like what I need.
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.
这篇关于Hibernate标准顺序依据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!