0

I have typical auto-generated by spring-security classes. + I want to select Roles together with UserRoles (table join) in one query, therefore I have added 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'
    }

But I am still getting lazy initialization of Roles, only when I am accessing it in getAuthorities (). And it cause a 'N+1' query performance problem. Why does grails/hibernate ignores fetch: 'join' directive? Is it somehow depends on spring-security?

1 Answers1

0

As I interpret the official documentation, fetch is used by grails when you do a multi select for your User (findBy non unique attribute methods), and lazy for a unique result (get or findBy unique attribute methods)

Official documentation :

Did you try the lazy way ?

Another possibility is about implementation of spring-security itself what is a portage of java implementation and knowing no information from gorm (only translate java code, perhaps)

Isammoc
  • 883
  • 5
  • 20