Sunday, March 28, 2010

Using Distinct and Criteria Object with Pagination Groovy

I have discovered an issue with the Criteria Object when it comes to pagination.

My code looks like this:

def softwareList = c.listDistinct {
environments {
if (params.environmentId) {
eq('id', new Long(params.environmentId))
}
}

statuses {
if (params.active) {
gt('endDate', new Date())
reference {
eq('name', 'Active')
}
}
}
maxResults(5)


firstResult(params.offset)

}

Even though I am declaring my maxResults to be 5, I am getting different number or rows returned each time the user changes to a different page. I want each page to display 5 items. However, on the first page there might be only one item, and the next page there could be 4. The number of rows returned will always be between 1 and 5 though. I found out that the issue has to do with using distinct in this part of the code: c.listDistinct. If I removed distinct, then each page will display 5 items. I am not sure why it does this, but in my case I didn't need to use distinct.

1 comments:

Mikel April 27, 2010 2:45 AM  

Critería does the distinct over the page, this is the reason!