Search code examples
javahibernategrailsgroovygrails-orm

Pagination with Grails/GORM/DataServices @Query annotation


How do you add pagination when using GORM JPA-QL Queries via the grails.gorm.services.Query annotation

@Service(User)
abstract class UserDataService implements IUserService {

    @Query($/SELECT DISTINCT new Map(user.id as id, user.username as username)
             FROM $User user
              join $UserRelationship ur on ur.user.id = user.id or ur.manager.id = user.id
              join ur.manager manager
           /$)
   abstract List<Map> searchAllUsers(String searchString, [offset: 0, max: 10])

}

Solution

  • you can pass Map args in the method like listWithQuery and listWithQuery2, args would need to look like [offset: (rows to offset) , max: (limit of rows to return)]

    @Service(User)
    interface IUserService {
    
        List<User> findAll()
    
        List<User> list(Map args)
    
        @Query("from ${User user} where ${user.lastName} like '%halp%'")
        List<User> listWithQuery(Map args)
    
        @Query("from ${User} user where user.lastName like $nameLike")
        List<User> listWithQuery2(String nameLike, Map args)
    }