I'd like to be able to paginate my database results with something like the CriteriaBuilder
, but I need my results to be consistent as to a single point in time.
Is ther开发者_JAVA百科e an easy way to somehow paginate and keep the select within the same transaction/
Have you discarded to store the whole result set in memory using ScrollableResult
?
Look here
DomainObject.createCriteria().scroll{}
returns a ScrollableResult
Specify the offset you want in the criteria, based in the current page number.
def results = DomainClass.withCriteria {
firstResult ( (currentPageNumber - 1) * itemsPerPage)
maxResults (itemsPerPage)
}
To know the total number of pages, you'll need another query:
def numberOfPages = DomainClass.count()
if(numberOfPages != 0) numberOfPages = numberOfPages / itemsPerPage + 1
精彩评论