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 有一个名为 game 的字段。我需要通过 Foo 获取游戏引用。但是当我运行上面的代码时,出现以下错误。
Caused by: org.postgresql.util.PSQLException: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list
Position: 246
如何正确添加预测和订单?
最佳答案
您的逻辑有错误,您尝试像这样制作:
Select Distinct game From Foo Order By created
逻辑上错误,服务器无法按“创建”排序结果,因为仅存在“游戏”列,并且不可能按“创建”排序。
关于java - 如何使用具有预测和顺序的条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21677237/