GORM createCriteria 和 list 不返回相同的结果:我该怎么办? [英] GORM createCriteria and list do not return the same results : what can I do?

查看:39
本文介绍了GORM createCriteria 和 list 不返回相同的结果:我该怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 NimbleShiro 用于我的安全框架,我刚刚遇到了一个 GORM 错误.确实:

I am using Nimble and Shiro for my security frameworks and I've just come accross a GORM bug. Indeed :

User.createCriteria().list { 
   maxResults 10 
} 

返回 10 个用户User.list(max: 10) 返回 9 个用户

returns 10 users whereas User.list(max: 10) returns 9 users !

经过进一步调查,我发现createCriteria 返回了两次相同的用户(admin)因为admin有2个角色!!!(我不是在开玩笑).

After further investigations, I found out that createCriteria returns twice the same user (admin) because admin has 2 roles!!! (I am not joking).

似乎具有超过 1 个角色的任何用户将在 createCriteria 调用中返回两次,User.list 将返回 max-1 实例(即 9 个用户而不是 10 个用户)

It appears that any user with more than 1 role will be returned twice in the createCriteria call and User.list will return max-1 instances (i.e 9 users instead of 10 users)

为了返回 10 个唯一用户,我可以使用什么解决方法?

这很烦人,因为我无法正确使用分页.

This is a very annoying because I have no way to use pagination correctly.

我的域类是:

class UserBase { 
   String username 
   static belongsTo = [Role, Group] 
   static hasMany = [roles: Role, groups: Group] 
   static fetchMode = [roles: 'eager', groups: 'eager'] 
   static mapping = { 
     roles cache: true, 
     cascade: 'none', 
     cache usage: 'read-write', include: 'all' 
   } 
}

class User extends UserBase { 
  static mapping = {cache: 'read-write'} 
} 

class Role { 
  static hasMany = [users: UserBase, groups: Group] 
  static belongsTo = [Group] 
  static mapping = { cache usage: 'read-write', include: 'all' 
    users cache: true 
    groups cache: true 
  } 
} 

推荐答案

不太简洁明了,但使用 HQL 查询似乎是解决此问题的一种方法.如 Grails 文档(executeQuery 部分)中所述,分页参数可以作为额外参数添加执行查询.

Less concise and clear, but using an HQL query seems a way to solve this problem. As described in the Grails documentation (executeQuery section) the paginate parameters can be added as extra parameters to executeQuery.

User.executeQuery("select distinct user from User user", [max: 2, offset: 2])

这篇关于GORM createCriteria 和 list 不返回相同的结果:我该怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆