grails 3.3 gorm where query with projection count() different than list().size()

与世无争的帅哥 提交于 2019-12-10 18:26:42

问题


According to the Gorm 6 documentation section 7.4 the Where Query returns a DetachedCriteria, which provides a method count() that is supposed to return the number of records returned by the query. In fact, as far as I can tell, if dc is an instance of DetachedCriteria, then

dc.count() == dc.list().size()

must hold true.

In the case of a Where Query containing a projection, count() seems to return something else. In this simple example:

query = Runner.where {
    projections {
        groupProperty 'finishPosition'
        rowCount()
    }
}

println "query.count() ${query.count()}"
println "query.list().size() ${query.list().size()}"

the result printed is:

query.count() 576
query.list().size() 22

If I also print query.list(), it appears as

query.list() [[14, 576], [12, 1945], [8, 5682], [17, 78], [1, 91842], [15, 174], [10, 3836], [11, 2873], [4, 90688], [18, 36], [0, 336177], [16, 110], [6, 63957], [19, 6], [2, 91669], [21, 2], [3, 91550], [20, 4], [13, 956], [5, 72852], [9, 4811], [7, 6238]]

that is, list() and list.size() are consistent (and match an SQL query on the underlying database).

Does anyone have any ideas about why count() seems to off in this case? I find it interesting that the number returned by count() - 576 - is the same as the projection rowCount() for the first record returned...

For the time being, I guess I'll use query.list().size().

来源:https://stackoverflow.com/questions/50284909/grails-3-3-gorm-where-query-with-projection-count-different-than-list-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!