[英]Why does my Play Framework (1.2.4) count query fail?
I have a simple model involving title and description. 我有一个涉及标题和描述的简单模型。 It extends play.db.jpa.Model 它扩展了play.db.jpa.Model
The following search method works perfectly 以下搜索方法可以完美运行
public static SearchResults search(String search, Integer page) {
String likeSearch = "%" + search + "%";
long count = find("title like ? OR description like ? order by " +
"title ASC", likeSearch, likeSearch).fetch().size();
List<Must> items = find("title like ? OR description like ? order by " +
"title ASC", likeSearch, likeSearch).fetch(page, 20);
return new SearchResults(items, count);
}
However when I tweak count as follows 但是当我调整如下时
long count = count("title like ? OR description like ? order by " +
"title ASC", likeSearch, likeSearch);
I get 我懂了
PersistenceException occured : org.hibernate.exception.SQLGrammarException: could not execute query 发生PersistenceException:org.hibernate.exception.SQLGrammarException:无法执行查询
ERROR ~ ERROR: column "must0_.title" must appear in the GROUP BY clause or be used in an aggregate function 错误〜错误:“ must0_.title”列必须出现在GROUP BY子句中或在聚合函数中使用
Why is the error asking me to use an aggregate function when the query has not changed at all? 当查询完全没有变化时,为什么错误要求我使用聚合函数?
This is because in the first query, all the records are returned and then counted in the result list. 这是因为在第一个查询中,将返回所有记录,然后将其计入结果列表。
In your second query the count is done in the database so your sql must be formed correctly. 在第二个查询中,计数是在数据库中完成的,因此您的sql必须正确形成。 I think the order by is causing the error you described, try removing it. 我认为订单是引起您所描述的错误的原因,请尝试将其删除。 You are trying to order on column which are not part of the return (count return numbers not columns). 您正在尝试对不属于退货的列进行订购(计算退货数量而不是列)。
You can set the jpa.debugSQL=true in your application.conf if you need to see the sql generated. 如果需要查看生成的sql,可以在application.conf中设置jpa.debugSQL = true 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.