1

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.

3
  • Please tell more info about your configuratio. Grails version? Backend Database name and version? DataSource.groovy content? Nov 21, 2013 at 8:08
  • 3
    Keep in mind that using max the list method don't return a simple list but a PagedResultList. Try to do User.list(offset: 0, max: 20).getTotalCount() and User.list(offset: 0, max: 20).size() and tell us the results Nov 21, 2013 at 8:13
  • Hi Fabiano, thanks a lot for your replies. I'm using grails 2.0.3 and MySQL 5.6. User.list(offset: 0, max: 20).getTotalCount() returns 40 and User.list(offset: 0, max: 20).size() returns 12. Nov 21, 2013 at 13:54

1 Answer 1

1

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

1
  • Thanks a lot, ahispj! Yes, that was it. I was using roles fetch: 'join' in my User mapping. Now I'm using lazy: false instead. Nov 22, 2013 at 16:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.