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?