This example shows how to use pagination and sorting with many-to-many relationship in Grails.
Suppose you have two domain classes: Author, Article and you want to display all articles of some author in a page with pagination.
Domain classes:
class Author {
static hasMany = [articles: Article]
}
class Article {
belongsTo = Author
static hasMany = [authors: Author]
}
Code language: JavaScript (javascript)
The HQL query with pagination parameters:
def author = Author.get(id)
Article.findAll("FROM Article WHERE :author IN ELEMENTS(article.authors)",[author:author],
[max:params.max, offset:params.offset])
Code language: JavaScript (javascript)
For sorting you can use order by:
Article.findAll("FROM Article article WHERE :author IN ELEMENTS(article.authors)" +
(params.sort && params.order ? "order by article.${params.sort} ${params.order}" : ''),
[author: author, max:params.max, offset:params.offset])
Code language: JavaScript (javascript)
Hope this would be helpful!