我有以下代码
PagedResultList res = myService.getPage(paginateParams, ...)
println res.size() // returns 2
println res.getTotalCount() // returns 1
getPage看起来像:
def criteria = MyDomain.createCriteria()
criteria.list(max: paginateParams.max, offset: paginateParams.offset) { // max is 10, offset is 0, sortBy is updatedAt and sortOrder is desc
eq('org', org)
order(paginateParams.sortBy, paginateParams.sortOrder)
}
为什么两个方法返回不同的值?该文档没有说明差异,但确实提到getTotalCount是记录数
当前在grails 2.4.5上
编辑:
res上的println打印出:
res: [
com.<hidden>.MyDomain: 41679f98-a7c5-4193-bba8-601725007c1a,
com.<hidden>.MyDomain: 41679f98-a7c5-4193-bba8-601725007c1a]
是的,res两次有一个SINGLE对象-这就是我要修复的错误。我怎么知道我在MyDomain的ID上有一个主键,当我检查数据库时,它还显示了该特定组织的一条记录(请参阅我的条件)
编辑2:我发现此评论(http://docs.grails.org/2.4.5/ref/Domain%20Classes/createCriteria.html)
listDistinct If subqueries or associations are used, one may end up with the same row multiple times in the result set. In Hibernate one would do a "CriteriaSpecification.DISTINCT_ROOT_ENTITY". In Grails one can do it by just using this method.
如果我正确理解的话,这是他们说“列表”方法在这种情况下不起作用的方式,请改用listDistinct,但随后他们会警告:
The listDistinct() method does not work well with the pagination options maxResult and firstResult. If you need distinct results with pagination, we currently recommend that you use HQL. You can find out more information from this blog post.
但是,博客文章是无效链接。
相关:GORM createCriteria and list do not return the same results : what can I do?
最佳答案
问题编辑后与实际问题无关,但此引用似乎很有用
Generally PagedResultList
.size()
perform size() on resultList property (in-memory object represent database record), while.getTotalCount()
do count query against database. If this two value didn't match your list may contain duplicate.
关于grails - PagedResultList .size()和.getTotalCount()在grails gorm中返回不同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46167329/