2012-12-11 67 views
1

我有三個域類作者,註釋和圖書:如何在Grails中對複雜的mongoDB查詢進行分頁?

class Author { 
String name 
static hasMany = [books: Book, comments: Comment] 
} 

class Book { 
static belongsTo = [author: Author] 
static hasMany = [comments: Comment] 
} 

class Comment { 
static belongsTo = [book: Book, author: Author] 
} 

我有以下MongoDB的查詢查找所有具有給定作者的評論書籍。

Comment.findAllByAuthor(author1).collect { 
     it.book 
    }.unique().findAll { 
     it != null 
} 

我該如何對這個查詢使用分頁,即對所有書籍進行分頁?

回答

1

使用條件查詢與預訂,而不是評論:

def c = Book.createCriteria() 
def PaginatedBookList = c.list(max: params.max, offset: params.offset) { 
    and{ 
     eq('author',author1) 
     isNotNull('comments') 
    } 
}