简体   繁体   中英

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

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

ERROR ~ ERROR: column "must0_.title" must appear in the GROUP BY clause or be used in an aggregate function

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM