【问题标题】:Grails spring-security UserRole - Role fetch :'join'Grails spring-security UserRole - 角色获取:'join'
【发布时间】:2013-01-11 12:33:04
【问题描述】:

我有典型的由 spring-security 类自动生成的。 + 我想在一个查询中同时选择角色和 UserRoles(表连接),因此我添加了 fetch: 'join'。

class User {
    def springSecurityService
    String username 
    String password 
    static hasMany = [userRoles: UserRole]
    Set<Role> getAuthorities () {
        if (!this.id) {return []}
        def userAuthorities= userRoles*.role
        return userAuthorities
    }
}

class UserRole implements Serializable {

    User user
    Role role
...
    static mapping = {
        table 'UserRole'
        version false
        id composite: ['role', 'user']
        user column: 'UserID'
        role column: 'RoleID', fetch: 'join'
    }
class Role {
    String authority
    String description

    static mapping = {
        cache true
        table 'Role'
        id column: 'RoleID', generator: 'identity'
        authority column: 'Authority'
        description column:  'Description'
    }

但我仍然会延迟初始化角色,只有当我在 getAuthorities() 中访问它时。它会导致“N+1”查询性能问题。为什么 grails/hibernate 会忽略 fetch: 'join' 指令?它是否以某种方式取决于弹簧安全性?

【问题讨论】:

  • 它的设计与最初的设计一样是有原因的。

标签: grails grails-plugin grails-domain-class


【解决方案1】:

正如我对官方文档的解释,当您为您的用户(findBy 非唯一属性方法)进行多选时,grails 使用fetch,而lazy 用于唯一结果(get 或 findBy 唯一属性方法)

官方文档:

你试过lazy的方式吗?

另一种可能性是关于 spring-security 本身的实现,它是 java 实现的一个移植,并且不知道来自 gorm 的信息(可能只翻译 java 代码)

【讨论】:

    猜你喜欢
    • 2017-08-20
    • 2014-03-31
    • 2013-08-26
    • 2015-12-30
    • 2017-05-18
    • 1970-01-01
    • 1970-01-01
    • 2015-12-17
    • 2015-04-13
    相关资源
    最近更新 更多