问题
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