简体   繁体   English

为什么我的Play Framework(1.2.4)计数查询失败?

[英]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.

相关问题 Play Framework 1.2.4:#{select}模板的选定选项 - Play Framework 1.2.4: Selected Option for #{select} template scala play 1.2.4是否与postgres兼容? - Does scala play 1.2.4 have compatibility with postgres? 播放框架1.2.4:play.exceptions.MailException的构造函数中的NullPointerException - Play framework 1.2.4: NullPointerException in the constructor of play.exceptions.MailException 为什么我的Play框架模型作为空白记录保存到数据库? - Why does my Play framework model save to the database as a blank record? 如何解决Play Framework 1.2.4中的ClassFormatException? - How do I resolve a ClassFormatException in Play Framework 1.2.4? 播放框架1.2.4战争环境的特定配置文件 - Play framework 1.2.4 war environemnt specific configuration file 文本编码可在Play中转换垃圾字符! 1.2.4框架 - Text encoding converts junk character in Play! 1.2.4 framework Java Play! 框架1.2.4:使用selenium从Mockmail中提取链接值 - Java Play! Framework 1.2.4: Exracting link value from Mockmail with selenium 玩框架! (1.2.4):前几个动态参数为null - Play Framework! (1.2.4): null first several dynamic parameters Play Framework 1.2.4将项目导出/导入到Eclipse IDE - Play Framework 1.2.4 Exporting/Importing project into Eclipse IDE
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM