hibernate - Grails N+1 query -
my grails app uses spring security , has usual user, userrole, , role classes. way these classes modelled little unusual, in there no hasmany mappings in either user or role. instead classes referenced solely through userrole.
class userrole implements serializable { user user role role } my understanding relationship has been modelled performance reasons, specifically, reduce possibility of n+1 queries.
in 1 part of application need load users , roles. conscious of aforementioned problem, tried this:
def usersbyrole = userrole.createcriteria().list { fetchmode("user", fetchmode.join) fetchmode("role", fetchmode.join) } however when try access user objects
usersbyrole.each { it.user } a separate query issued retrieve data user table, i've run problem trying avoid. tried following, suffers same problem.
def usersbyrole = userrole.createcriteria().list { fetchmode("user", fetchmode.select) fetchmode("role", fetchmode.select) } i should confess i'm not altogether clear on the difference between fetchmode.join , fetchmode.select, if set me straight on that, it'd appreciated.
i tried several combinations had same results - if @ generated sql there's no join in original query, has queries load users , roles.
others have had issues domain class - looks there's bug in gorm composite key made of domain classes, or that. people satisfied hql workaround, , luck :)
def usersbyrole = userrole.executequery( 'select ur userrole ur ' + 'left join fetch ur.user ' + 'left join fetch ur.role')
Comments
Post a Comment