开发者

Grails paginate database results on same transaction?

开发者 https://www.devze.com 2023-03-06 07:34 出处:网络
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.

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
0

精彩评论

暂无评论...
验证码 换一张
取 消