grails - PagedResultList .size()和.getTotalCount()在grails gorm中返回不同的值

标签 grails gorm

我有以下代码

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/

相关文章:

java - 在 Tomcat 中作为 WAR 运行的 Grails 项目中 Maven-build JAR 的 SLF4J 配置位置

grails - Grails/Groovy:在运行时修改 “query”闭包

grails - 无法解析类 org.apache.shiro.grails.ConfigUtils

grails - 在应用程序的变更日志中包含 Grails 插件迁移

grails - Grails-防止递归一对多关系

grails - 如何在BootStrap.groovy init中创建具有多个关系的对象

grails - 如何使用键/值对表进行搜索?

grails - 自定义插入和更新方法被自动保存击败

grails - executeUpdate查询不适用于Grails Spock测试

grails - Grails中是否可以替代ElementCollection/CollectionOfElements