【问题标题】:How to make a join in a GORM criteria with a domain class without association ?如何在没有关联的域类中加入 GORM 标准?
【发布时间】:2012-01-13 05:19:07
【问题描述】:

我使用 GORM 标准来检索 Spring Security 用户列表,并对用户的属性进行过滤和排序,并且我想对每个用户受影响的 Spring Security 角色进行排序。通常,我可以简单地做这样的事情:

User.createCriteria() {
    [...]
    userRoles {
        role {
            order("code", "asc")
        }
    }
}

但我不能在这里这样做,因为 Spring Security 没有定义 User 和 UserRole 之间以及 userRole 和 Role 之间的关联。尽管没有与 User 关联,但我试图找到一种方法来加入 UserRole,但没有成功。我还可以在 User 和 Role 中定义 hasMany 关联,但也许 Spring Security 没有这样做是有充分理由的。在这种情况下,更好的解决方案是什么?

谢谢

【问题讨论】:

    标签: grails spring-security grails-orm


    【解决方案1】:

    现在可能已经晚了。

    如果您使用的是 Spring Security 插件(我假设),您会发现该插件生成了一个将 SecUser 链接到 SecRole 的类 SecUserSecRoleReference

    这允许用户和角色之间的多对多关系。

    在您的情况下,我认为在 SecUserSecRole 而不是 User 上创建标准会更好

    【讨论】:

      【解决方案2】:

      据我所知,您没有理由不自己添加这些映射

      【讨论】:

      • 我最终这样做了,但是我不得不删除 UserRole 中的复合键定义,因为它阻止了 UserRole 和 Role 之间的连接。我希望它不会产生任何不良后果(到目前为止还没有)。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-08
      • 1970-01-01
      • 2010-12-22
      • 2019-12-31
      • 1970-01-01
      • 1970-01-01
      • 2020-04-24
      相关资源
      最近更新 更多