[英]How to use Criteria with Projections and Order?
Criteria criteria = sessionProvider.getCurrentSession().createCriteria(Foo.class);
criteria.createAlias("game", "game");
if (from != null) criteria.add(Restrictions.ge("game.created", from.toDate()));
if (to != null) criteria.add(Restrictions.lt("game.created", to.toDate()));
criteria.setProjection(Projections.distinct(Projections.property("game")));
criteria.addOrder(Order.desc(("game.created")));
criteria.setMaxResults(100);
games = criteria.list();
我使用上面的代码来获取游戏。 实体Foo有一个叫做游戏的场地。 我需要通过Foo获取游戏引用。 但是,当我运行上面的代码时,我得到以下错误。
Caused by: org.postgresql.util.PSQLException: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list
Position: 246
如何正确添加投影和订单?
你在逻辑上有错误,你试过像这样制作smth:
Select Distinct game From Foo Order By created
它在逻辑上是错误的,服务器不能通过“创建”来命令结果,因为只有“游戏”列存在,并且按“创建”排序是不可能的。
有同样的问题,我用过
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
代替
Projections.distinct
它对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.