{"id":1260,"date":"2010-07-14T22:48:26","date_gmt":"2010-07-14T17:18:26","guid":{"rendered":"https:\/\/2thenew.online\/blog\/?p=1260"},"modified":"2017-01-05T09:30:49","modified_gmt":"2017-01-05T04:00:49","slug":"criteria-query-and-pagination-params","status":"publish","type":"post","link":"https:\/\/2thenew.online\/blog\/criteria-query-and-pagination-params\/","title":{"rendered":"Criteria Query and pagination params"},"content":{"rendered":"<p>I have been using the following code to get paginated result and the total number of results returned irrespective of the pagination params.<\/p>\n<div class=\"code\">\n<pre lang=\"groovy\"> def result=SampleDomain.createCriteria().list(){\r\n\/\/ multiple restrictions\r\n   maxResults(params.max)\r\n   firstResult(params.offset)\r\n} \/\/ Return type is ArrayList\r\n\r\nInteger  totalResult=SampleDomain.createCriteria().count(){\r\n\/\/ multiple restrictions\r\n\/\/ maxResults(params.max)\r\n\/\/ firstResult(params.offset)\r\n}<\/pre>\n<\/div>\n<p>Clearly duplicating the same closure except for the pagination restrictions was not a good solution.<\/p>\n<p>After a little googling and reading mailing lists I got the solution.\u00a0Passing pagination params to createCriteria.list() returns result of type PagedResultList which provides many useful methods. The getTotalCount() method of PagedResultList class returns the actual number of results returned irrespective of the pagination restrictions(maxResults and firstResult). Also the result contains only those records fulfiling maxResults and firstResult restrictions.<\/p>\n<div class=\"code\">\n<pre lang=\"groovy\">def result=SampleDomain.createCriteria().list(max:params.max, offset:params.offset){\r\n\/\/ multiple\/complex restrictions\r\n   maxResults(params.max)\r\n   firstResult(params.offset)\r\n} \/\/ Return type is PagedResultList<\/pre>\n<\/div>\n<p>Thanks to all the active users of Grails mailing list.<\/p>\n<p>Here are few useful links:<\/p>\n<p>Bhagwat Kumar<br \/>\nbhagwat(at)intelligrape(dot)com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have been using the following code to get paginated result and the total number of results returned irrespective of the pagination params. def result=SampleDomain.createCriteria().list(){ \/\/ multiple restrictions maxResults(params.max) firstResult(params.offset) } \/\/ Return type is ArrayList Integer totalResult=SampleDomain.createCriteria().count(){ \/\/ multiple restrictions \/\/ maxResults(params.max) \/\/ firstResult(params.offset) } Clearly duplicating the same closure except for the pagination [&hellip;]<\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":13,"footnotes":""},"categories":[7],"tags":[266,320,276,322,321],"class_list":["post-1260","post","type-post","status-publish","format-standard","hentry","category-grails","tag-createcriteria","tag-gorm-count","tag-gorm-list","tag-pagedresultlist","tag-pagination-params"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"I have been using the following code to get paginated result and the total number of results returned irrespective of the pagination params. def result=SampleDomain.createCriteria().list(){ \/\/ multiple restrictions maxResults(params.max) firstResult(params.offset) } \/\/ Return type is ArrayList Integer totalResult=SampleDomain.createCriteria().count(){ \/\/ multiple restrictions \/\/ maxResults(params.max) \/\/ firstResult(params.offset) } Clearly duplicating the same closure except for the pagination\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Bhagwat Kumar\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/2thenew.online\/blog\/criteria-query-and-pagination-params\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.0.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"TO THE NEW BLOG\" \/>\n\t\t<meta property=\"og:type\" content=\"blog\" \/>\n\t\t<meta property=\"og:title\" content=\"Criteria Query and pagination params | TO THE NEW Blog\" \/>\n\t\t<meta property=\"og:description\" content=\"I have been using the following code to get paginated result and the total number of results returned irrespective of the pagination params. def result=SampleDomain.createCriteria().list(){ \/\/ multiple restrictions maxResults(params.max) firstResult(params.offset) } \/\/ Return type is ArrayList Integer totalResult=SampleDomain.createCriteria().count(){ \/\/ multiple restrictions \/\/ maxResults(params.max) \/\/ firstResult(params.offset) } Clearly duplicating the same closure except for the pagination\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/2thenew.online\/blog\/criteria-query-and-pagination-params\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/2thenew.online\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/2thenew.online\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:site\" content=\"@tothenew\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Criteria Query and pagination params | TO THE NEW Blog\" \/>\n\t\t<meta name=\"twitter:description\" content=\"I have been using the following code to get paginated result and the total number of results returned irrespective of the pagination params. def result=SampleDomain.createCriteria().list(){ \/\/ multiple restrictions maxResults(params.max) firstResult(params.offset) } \/\/ Return type is ArrayList Integer totalResult=SampleDomain.createCriteria().count(){ \/\/ multiple restrictions \/\/ maxResults(params.max) \/\/ firstResult(params.offset) } Clearly duplicating the same closure except for the pagination\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/2thenew.online\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/criteria-query-and-pagination-params\\\/#article\",\"name\":\"Criteria Query and pagination params | TO THE NEW Blog\",\"headline\":\"Criteria Query and pagination params\",\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/bhagwat\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"},\"datePublished\":\"2010-07-14T22:48:26+05:30\",\"dateModified\":\"2017-01-05T09:30:49+05:30\",\"inLanguage\":\"en-US\",\"commentCount\":4,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/criteria-query-and-pagination-params\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/criteria-query-and-pagination-params\\\/#webpage\"},\"articleSection\":\"Grails, createCriteria, GORM count(), GORM list(), PagedResultList, pagination params\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/criteria-query-and-pagination-params\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/#listItem\",\"name\":\"Grails\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/#listItem\",\"position\":2,\"name\":\"Grails\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/criteria-query-and-pagination-params\\\/#listItem\",\"name\":\"Criteria Query and pagination params\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/criteria-query-and-pagination-params\\\/#listItem\",\"position\":3,\"name\":\"Criteria Query and pagination params\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/#listItem\",\"name\":\"Grails\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\",\"name\":\"TO THE NEW Blog\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/bhagwat\\\/#author\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/bhagwat\\\/\",\"name\":\"Bhagwat Kumar\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/criteria-query-and-pagination-params\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d6c506f803fe26906ef802b1eaee463146e449a8b5b3d1f7e4ebc715b933d620?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Bhagwat Kumar\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/criteria-query-and-pagination-params\\\/#webpage\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/criteria-query-and-pagination-params\\\/\",\"name\":\"Criteria Query and pagination params | TO THE NEW Blog\",\"description\":\"I have been using the following code to get paginated result and the total number of results returned irrespective of the pagination params. def result=SampleDomain.createCriteria().list(){ \\\/\\\/ multiple restrictions maxResults(params.max) firstResult(params.offset) } \\\/\\\/ Return type is ArrayList Integer totalResult=SampleDomain.createCriteria().count(){ \\\/\\\/ multiple restrictions \\\/\\\/ maxResults(params.max) \\\/\\\/ firstResult(params.offset) } Clearly duplicating the same closure except for the pagination\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/criteria-query-and-pagination-params\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/bhagwat\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/bhagwat\\\/#author\"},\"datePublished\":\"2010-07-14T22:48:26+05:30\",\"dateModified\":\"2017-01-05T09:30:49+05:30\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\",\"name\":\"TO THE NEW Blog\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Criteria Query and pagination params | TO THE NEW Blog","description":"I have been using the following code to get paginated result and the total number of results returned irrespective of the pagination params. def result=SampleDomain.createCriteria().list(){ \/\/ multiple restrictions maxResults(params.max) firstResult(params.offset) } \/\/ Return type is ArrayList Integer totalResult=SampleDomain.createCriteria().count(){ \/\/ multiple restrictions \/\/ maxResults(params.max) \/\/ firstResult(params.offset) } Clearly duplicating the same closure except for the pagination","canonical_url":"https:\/\/2thenew.online\/blog\/criteria-query-and-pagination-params\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/2thenew.online\/blog\/criteria-query-and-pagination-params\/#article","name":"Criteria Query and pagination params | TO THE NEW Blog","headline":"Criteria Query and pagination params","author":{"@id":"https:\/\/2thenew.online\/blog\/author\/bhagwat\/#author"},"publisher":{"@id":"https:\/\/2thenew.online\/blog\/#organization"},"datePublished":"2010-07-14T22:48:26+05:30","dateModified":"2017-01-05T09:30:49+05:30","inLanguage":"en-US","commentCount":4,"mainEntityOfPage":{"@id":"https:\/\/2thenew.online\/blog\/criteria-query-and-pagination-params\/#webpage"},"isPartOf":{"@id":"https:\/\/2thenew.online\/blog\/criteria-query-and-pagination-params\/#webpage"},"articleSection":"Grails, createCriteria, GORM count(), GORM list(), PagedResultList, pagination params"},{"@type":"BreadcrumbList","@id":"https:\/\/2thenew.online\/blog\/criteria-query-and-pagination-params\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog#listItem","position":1,"name":"Home","item":"https:\/\/2thenew.online\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog\/category\/grails\/#listItem","name":"Grails"}},{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog\/category\/grails\/#listItem","position":2,"name":"Grails","item":"https:\/\/2thenew.online\/blog\/category\/grails\/","nextItem":{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog\/criteria-query-and-pagination-params\/#listItem","name":"Criteria Query and pagination params"},"previousItem":{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog\/criteria-query-and-pagination-params\/#listItem","position":3,"name":"Criteria Query and pagination params","previousItem":{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog\/category\/grails\/#listItem","name":"Grails"}}]},{"@type":"Organization","@id":"https:\/\/2thenew.online\/blog\/#organization","name":"TO THE NEW Blog","url":"https:\/\/2thenew.online\/blog\/"},{"@type":"Person","@id":"https:\/\/2thenew.online\/blog\/author\/bhagwat\/#author","url":"https:\/\/2thenew.online\/blog\/author\/bhagwat\/","name":"Bhagwat Kumar","image":{"@type":"ImageObject","@id":"https:\/\/2thenew.online\/blog\/criteria-query-and-pagination-params\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/d6c506f803fe26906ef802b1eaee463146e449a8b5b3d1f7e4ebc715b933d620?s=96&d=mm&r=g","width":96,"height":96,"caption":"Bhagwat Kumar"}},{"@type":"WebPage","@id":"https:\/\/2thenew.online\/blog\/criteria-query-and-pagination-params\/#webpage","url":"https:\/\/2thenew.online\/blog\/criteria-query-and-pagination-params\/","name":"Criteria Query and pagination params | TO THE NEW Blog","description":"I have been using the following code to get paginated result and the total number of results returned irrespective of the pagination params. def result=SampleDomain.createCriteria().list(){ \/\/ multiple restrictions maxResults(params.max) firstResult(params.offset) } \/\/ Return type is ArrayList Integer totalResult=SampleDomain.createCriteria().count(){ \/\/ multiple restrictions \/\/ maxResults(params.max) \/\/ firstResult(params.offset) } Clearly duplicating the same closure except for the pagination","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/2thenew.online\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/2thenew.online\/blog\/criteria-query-and-pagination-params\/#breadcrumblist"},"author":{"@id":"https:\/\/2thenew.online\/blog\/author\/bhagwat\/#author"},"creator":{"@id":"https:\/\/2thenew.online\/blog\/author\/bhagwat\/#author"},"datePublished":"2010-07-14T22:48:26+05:30","dateModified":"2017-01-05T09:30:49+05:30"},{"@type":"WebSite","@id":"https:\/\/2thenew.online\/blog\/#website","url":"https:\/\/2thenew.online\/blog\/","name":"TO THE NEW Blog","inLanguage":"en-US","publisher":{"@id":"https:\/\/2thenew.online\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"TO THE NEW BLOG","og:type":"blog","og:title":"Criteria Query and pagination params | TO THE NEW Blog","og:description":"I have been using the following code to get paginated result and the total number of results returned irrespective of the pagination params. def result=SampleDomain.createCriteria().list(){ \/\/ multiple restrictions maxResults(params.max) firstResult(params.offset) } \/\/ Return type is ArrayList Integer totalResult=SampleDomain.createCriteria().count(){ \/\/ multiple restrictions \/\/ maxResults(params.max) \/\/ firstResult(params.offset) } Clearly duplicating the same closure except for the pagination","og:url":"https:\/\/2thenew.online\/blog\/criteria-query-and-pagination-params\/","og:image":"https:\/\/2thenew.online\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","og:image:secure_url":"https:\/\/2thenew.online\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","twitter:card":"summary","twitter:site":"@tothenew","twitter:title":"Criteria Query and pagination params | TO THE NEW Blog","twitter:description":"I have been using the following code to get paginated result and the total number of results returned irrespective of the pagination params. def result=SampleDomain.createCriteria().list(){ \/\/ multiple restrictions maxResults(params.max) firstResult(params.offset) } \/\/ Return type is ArrayList Integer totalResult=SampleDomain.createCriteria().count(){ \/\/ multiple restrictions \/\/ maxResults(params.max) \/\/ firstResult(params.offset) } Clearly duplicating the same closure except for the pagination","twitter:image":"https:\/\/2thenew.online\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png"},"aioseo_meta_data":{"post_id":"1260","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"limit_modified_date":false,"created":"2021-04-29 21:28:38","updated":"2024-02-29 09:32:21","focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/2thenew.online\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/2thenew.online\/blog\/category\/grails\/\" title=\"Grails\">Grails<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tCriteria Query and pagination params\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/2thenew.online\/blog"},{"label":"Grails","link":"https:\/\/2thenew.online\/blog\/category\/grails\/"},{"label":"Criteria Query and pagination params","link":"https:\/\/2thenew.online\/blog\/criteria-query-and-pagination-params\/"}],"_links":{"self":[{"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/posts\/1260","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/comments?post=1260"}],"version-history":[{"count":0,"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/posts\/1260\/revisions"}],"wp:attachment":[{"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/media?parent=1260"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/categories?post=1260"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/tags?post=1260"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}