Search code examples
grailsgrails-orm

Grails - Weird behavior when using max param in domain list() method


I'm using spring-security-acl plugin and have User & Role defined as a many-to-many relationship. I also have a UserRole domain class.

I have 40 users in my DB.

If I do:

User.list()

I get all 40 users.

But I if I do something like:

User.list(offset: 0, max: 20)

I only get 12 users, when I should be getting 20. The use of the max param produces very weird behaviors for that domain.

I can't figure out what's happening. I guess it might have something to do with the many-to-many relationship.

Any help will be really appreciated.


Solution

  • I assume, the issue here is with one-to-many/many-to-many association. Your code is trying to fetch User and his Roles using join. This means when you try to fetch a User with three Roles, three rows are returned instead one. (This is SQL issue and you can do nothing about it)

    So when you give 40 as the max, there are 40 rows returned by SQL, but that contains the duplicate rows. Now when Grails converts the the data into objects, it removes the duplicates, thus leaving you with 12 rows.

    Solution : Remove eager fetching of Roles from User and try to fetch just the User objects.U