{"id":36288,"date":"2015-02-02T07:00:01","date_gmt":"2015-02-02T05:00:01","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/?p=36288"},"modified":"2015-02-01T20:43:49","modified_gmt":"2015-02-01T18:43:49","slug":"grails-tutorial-for-beginners-criteria-api","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2015\/02\/grails-tutorial-for-beginners-criteria-api.html","title":{"rendered":"Grails Tutorial for Beginners &#8211; Criteria API"},"content":{"rendered":"<p>This tutorial will show how to use Criteria API to search data from the database. Criteria is a powerful API and an elegant alternative to HQL.<\/p>\n<h2>Introduction<\/h2>\n<p>This <a href=\"http:\/\/www.javacodegeeks.com\/2015\/01\/grails-tutorial-for-beginners-hql-queries-executequery-and-executeupdate.html\">previous post<\/a> showed that HQL is a powerful way to perform complex queries. However, it is not very elegant if an HQL statement is not fixed and needs to be concatenated. For example: if we need to query the database only if a parameter is not null, our code will look like this:<br \/>\n&nbsp;<br \/>\n&nbsp;<\/p>\n<pre class=\" brush:java\">def hqlQueryString = \"from Person where 1 = 1\"\r\ndef hqlNamedParams = [:]\r\nif (firstNameToSearch != null) {\r\n    hqlQueryString = hqlQueryString + ' and firstName = :firstNameToSearch '\r\n    hqlNamedParams= hqlNamedParams + [firstNameToSearch:firstNameToSearch]\r\n}\r\nif (lastNameToSearch != null) {\r\n    hqlQueryString = hqlQueryString + ' and lastName = :lastNameToSearch '\r\n    hqlNamedParams= hqlNamedParams + [lastNameToSearch:lastNameToSearch]\r\n}\r\ndef result = Person.executeQuery(hqlQueryString, hqlNamedParams)<\/pre>\n<p>As you could see, it could be dirty and cumbersome to maintain. It is prone to errors too. Criteria is a more elegant alternative. Here is how the new code will look like:<\/p>\n<pre class=\" brush:java\">def criteria = Person.createCriteria()\r\ndef result = criteria.list {\r\n    if (firstNameToSearch != null) {\r\n        eq('firstName', firstNameToSearch)\r\n    }\r\n    if (lastNameToSearch != null) {\r\n        eq('lastName', lastNameToSearch)\r\n    }\r\n}<\/pre>\n<p>The code now is more readable and easy to understand even at just one glance.<\/p>\n<h2>Criteria methods<\/h2>\n<p>Here are the available methods for a criteria instance <\/p>\n<h3>list<\/h3>\n<p>The <b>list<\/b> method will return all matching rows of the given closure criteria.<\/p>\n<p>This will return all Person instances<\/p>\n<pre class=\" brush:java\">def criteria = Person.createCriteria()\r\ndef result = criteria.list{}<\/pre>\n<p>This will return all Person instances with first name John<\/p>\n<pre class=\" brush:java\">def criteria = Person.createCriteria()\r\ndef result = criteria.list{ \r\n    eq ('firstName', 'John')\r\n}<\/pre>\n<h3>get<\/h3>\n<p>The <b>get<\/b> method will return a single row given the closure criteria. Note that <b>get<\/b> will throw an exception when the criteria matches more than 1 row. If no row is matched, null value is returned.<\/p>\n<p>This example will get the Person instance givn the driver&#8217;s license.<\/p>\n<pre class=\" brush:java\">def criteria = Person.createCriteria()\r\ndef result = criteria.get{\r\n    eq ('driverslicenseNo', 'C1922-8DFG-1155')\r\n}<\/pre>\n<h3>scroll<\/h3>\n<p>The <b>scroll<\/b> method will return a scrollable result set. This is useful when you need to work with large number of rows as the result set will only transfer data on as required basis.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p>Here is an example of iterating through all person with last name Doe.<\/p>\n<pre class=\" brush:java\">def criteria = Person.createCriteria()\r\ndef result = criteria.scroll{\r\n    eq ('lastName', 'Doe')\r\n}\r\nwhile (result.next()) {\r\n    def person = result.get()[0]\r\n    println \"Hello ${person.firstName} ${person.lastName}\"\r\n}<\/pre>\n<h2>AND OR Operators<\/h2>\n<p>We can use <b>and<\/b> and <b>or<\/b> operators to construct complex logic.<\/p>\n<pre class=\" brush:java\">def criteria = Person.createCriteria()\r\ndef result = criteria.list {\r\n    or {\r\n        and {\r\n            eq('lastName', 'Doe')\r\n            gt('age', 15)\r\n        }\r\n        and {\r\n            eq('lastName', 'Smith')\r\n            gt('age', 18)\r\n        }\r\n    }\r\n}<\/pre>\n<h2>Pagination<\/h2>\n<p>We can perform pagination by passing <b>offset<\/b> and <b>max<\/b> parameters. The <b>max<\/b> parameter is the maximum number of rows to be returned while the <b>offset<\/b> parameter is the number of rows to skip before retrieving the first result.<\/p>\n<p>If we wish to retrieve 10 rows at a time, here is the code for retrieving the first page:<\/p>\n<pre class=\" brush:java\">def criteria = Person.createCriteria()\r\ndef result = criteria.list (max:10, offset:0) {\r\n    eq ('firstName', 'John')\r\n}<\/pre>\n<p>And here is the code for retrieving the second page. Note that the only difference is the value for offset.<\/p>\n<pre class=\" brush:java\">def criteria = Person.createCriteria()\r\ndef result = criteria.list (max:10, offset:0) {\r\n    eq ('firstName', 'John')\r\n}<\/pre>\n<p>Here is an alternative way of retrieving the first and second page using <b>firstResult<\/b> and <b>maxResults<\/b><\/p>\n<pre class=\" brush:java\">def criteria = Person.createCriteria()\r\ndef result = criteria.list {\r\n    eq ('firstName', 'John')\r\n    firstResult(0)\r\n    maxResults(10)\r\n}<\/pre>\n<pre class=\" brush:java\">def criteria = Person.createCriteria()\r\ndef result = criteria.list {\r\n    eq ('firstName', 'John')\r\n    firstResult(10)\r\n    maxResults(10)\r\n}<\/pre>\n<p>It is highly recommended to have a <b>sort by<\/b> clause when doing pagination. Here is an example on how to sort by last name:<\/p>\n<pre class=\" brush:java\">def criteria = Person.createCriteria()\r\ndef result = criteria.list {\r\n    eq ('firstName', 'John')\r\n    order('lastName', 'asc')\r\n    firstResult(0)\r\n    maxResults(10)\r\n}<\/pre>\n<h2>Projection<\/h2>\n<p>It is possible to control the resulting columns of a query. For example:<\/p>\n<pre class=\" brush:java\">def criteria = Person.createCriteria()\r\ndef result = criteria.list {\r\n    projections {\r\n        property('firstName')\r\n        property('lastName')\r\n    }\r\n}<\/pre>\n<p>Instead of having a list of person instances, it will return a 2 dimensional list. Here is the example code on how to use the result data:<\/p>\n<pre class=\" brush:java\">def firstPerson = result[0]\r\ndef firstName = firstPerson[0]\r\ndef lastName = firstPerson[1]\r\nprintln \"First Name = ${firstName}\"\r\nprintln \"Last Name = ${lastName}\"<\/pre>\n<h2>Aggregate Functions<\/h2>\n<p>Similar to SQL, it is also possible to use aggregate functions inside criteria.<\/p>\n<ul>\n<li><b>distinct<\/b> &#8211; here is an example on how to retrieve all unique last names in the database\n<pre class=\" brush:java\">def criteria = Person.createCriteria()\r\ndef result = criteria.list {\r\n    projections {\r\n        distinct('lastName')\r\n    }\r\n}\r\nprintln \"Here are the list of unique last names\"\r\nresult.each { lastName -&gt;\r\n    println \"${lastName}\"\r\n}<\/pre>\n<\/li>\n<li><b>avg<\/b> &#8211; here is an example on how to get the average age of all people\n<pre class=\" brush:java\">def criteria = Person.createCriteria()\r\ndef result = criteria.list {\r\n    projections {\r\n        avg('age')\r\n    }\r\n}\r\nprintln \"The average age is ${result[0]}\"<\/pre>\n<\/li>\n<li><b>count<\/b> &#8211; here is an example on how to get the number of records in the person table\n<pre class=\" brush:java\">def criteria = Person.createCriteria()\r\ndef result = criteria.list {\r\n    projections {\r\n        count()\r\n    }\r\n}\r\nprintln \"The number of rows is ${result[0]}\"<\/pre>\n<\/li>\n<li><b>sum<\/b> &#8211; here is an example on how to use sum\n<pre class=\" brush:java\">def criteria = Purchase.createCriteria()\r\ndef result = criteria.list {\r\n    projections {\r\n        sum('price')\r\n    }\r\n}\r\nprintln \"The sum of all price ${result[0]}\"<\/pre>\n<\/li>\n<li><b>max<\/b> and <b>min<\/b> &#8211; here is an example of how to use max and min\n<pre class=\" brush:java\">def criteria = Person.createCriteria()\r\ndef result = criteria.list {\r\n    projections {\r\n        max('age')\r\n        min('age')\r\n    }\r\n}\r\nprintln \"The maximum age is ${result[0][0]}\"\r\nprintln \"The minimum age is ${result[0][1]}\"<\/pre>\n<\/li>\n<\/ul>\n<h2>Other Criterion Examples<\/h2>\n<p>Here are other examples on how to filter results.<\/p>\n<ul>\n<li><b>Property value comparison<\/b> &#8211; a domain property can be compared to a particular value. Here are some related criterions:<b>eq<\/b>, <b>gt<\/b>, <b>ge<\/b>, <b>lt<\/b>, <b>le<\/b> and <b>ne<\/b>.<br \/>\nHere is a sample code that list all teenagers with surname Doe:<\/p>\n<pre class=\" brush:java\">def criteria = Person.createCriteria()\r\ndef result = criteria.list {\r\n    eq('lastName', 'Doe')\r\n    ge('age', 13)\r\n    le('age', 17)\r\n}<\/pre>\n<\/li>\n<li><b>Property to property comparison<\/b> &#8211; a domain property can be compared to another property. Here are some related criterions:<b>eqProperty<\/b>, <b>gtProperty<\/b>, <b>geProperty<\/b>, <b>ltProperty<\/b>, <b>leProperty<\/b> and <b>neProperty<\/b>.<br \/>\nHere is a sample code that list all person whose first name is the same as their last name:<\/p>\n<pre class=\" brush:java\">def criteria = Person.createCriteria()\r\ndef result = criteria.list {\r\n    eqProperty('firstName', 'lastName')\r\n}<\/pre>\n<\/li>\n<li><b>Test null value<\/b> &#8211; <b>isNull<\/b> and <b>isNotNull<\/b> can be used to check if a domain property is null or not. Here is an example that list all person that are encoded without first name:\n<pre class=\" brush:java\">def criteria = Person.createCriteria()\r\ndef result = criteria.list {\r\n    isNull('firstName')\r\n}<\/pre>\n<\/li>\n<\/ul>\n<p>For other criterion, refer to the latest <a href=\"http:\/\/grails.github.io\/grails-doc\/latest\/ref\/Domain%20Classes\/createCriteria.html\">Grails doc.<\/a><\/p>\n<div class=\"attribution\">\n<table>\n<tbody>\n<tr>\n<td><span class=\"reference\">Reference: <\/span><\/td>\n<td><a href=\"http:\/\/grails.asia\/grails-tutorial-for-beginners-criteria\">Grails Tutorial for Beginners &#8211; Criteria API<\/a> from our <a href=\"http:\/\/www.javacodegeeks.com\/jcg\/\">JCG partner<\/a> Jonathan Tan at the <a href=\"http:\/\/grails.asia\/\">Grails cookbook<\/a> blog.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial will show how to use Criteria API to search data from the database. Criteria is a powerful API and an elegant alternative to HQL. Introduction This previous post showed that HQL is a powerful way to perform complex queries. However, it is not very elegant if an HQL statement is not fixed and &hellip;<\/p>\n","protected":false},"author":613,"featured_media":130,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[192],"class_list":["post-36288","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-groovy","tag-grails"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Grails Tutorial for Beginners - Criteria API - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"This tutorial will show how to use Criteria API to search data from the database. Criteria is a powerful API and an elegant alternative to HQL.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2015\/02\/grails-tutorial-for-beginners-criteria-api.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Grails Tutorial for Beginners - Criteria API - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"This tutorial will show how to use Criteria API to search data from the database. Criteria is a powerful API and an elegant alternative to HQL.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2015\/02\/grails-tutorial-for-beginners-criteria-api.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2015-02-02T05:00:01+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/grails-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Jonathan Tan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jonathan Tan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/02\\\/grails-tutorial-for-beginners-criteria-api.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/02\\\/grails-tutorial-for-beginners-criteria-api.html\"},\"author\":{\"name\":\"Jonathan Tan\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/aa6c54395d81041b16aff3785d6e09cb\"},\"headline\":\"Grails Tutorial for Beginners &#8211; Criteria API\",\"datePublished\":\"2015-02-02T05:00:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/02\\\/grails-tutorial-for-beginners-criteria-api.html\"},\"wordCount\":695,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/02\\\/grails-tutorial-for-beginners-criteria-api.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/grails-logo.jpg\",\"keywords\":[\"Grails\"],\"articleSection\":[\"Groovy\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/02\\\/grails-tutorial-for-beginners-criteria-api.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/02\\\/grails-tutorial-for-beginners-criteria-api.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/02\\\/grails-tutorial-for-beginners-criteria-api.html\",\"name\":\"Grails Tutorial for Beginners - Criteria API - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/02\\\/grails-tutorial-for-beginners-criteria-api.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/02\\\/grails-tutorial-for-beginners-criteria-api.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/grails-logo.jpg\",\"datePublished\":\"2015-02-02T05:00:01+00:00\",\"description\":\"This tutorial will show how to use Criteria API to search data from the database. Criteria is a powerful API and an elegant alternative to HQL.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/02\\\/grails-tutorial-for-beginners-criteria-api.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/02\\\/grails-tutorial-for-beginners-criteria-api.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/02\\\/grails-tutorial-for-beginners-criteria-api.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/grails-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/grails-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/02\\\/grails-tutorial-for-beginners-criteria-api.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JVM Languages\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/jvm-languages\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Groovy\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/jvm-languages\\\/groovy\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Grails Tutorial for Beginners &#8211; Criteria API\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/aa6c54395d81041b16aff3785d6e09cb\",\"name\":\"Jonathan Tan\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4e0b0fc719b8ab677eb7f91293285b9dbefc8a843253329d5baa74582bf7673c?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4e0b0fc719b8ab677eb7f91293285b9dbefc8a843253329d5baa74582bf7673c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4e0b0fc719b8ab677eb7f91293285b9dbefc8a843253329d5baa74582bf7673c?s=96&d=mm&r=g\",\"caption\":\"Jonathan Tan\"},\"description\":\"Jon is a professional software engineer currently working in financial trading systems. He has worked in a wide variety of projects that includes mobile games, 3D visualization, artificial intelligence, banking and trading systems. He loves to teach people and expresses his passion through blogging.\",\"sameAs\":[\"http:\\\/\\\/grails.asia\\\/\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/jonathan-tan\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Grails Tutorial for Beginners - Criteria API - Java Code Geeks","description":"This tutorial will show how to use Criteria API to search data from the database. Criteria is a powerful API and an elegant alternative to HQL.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/2015\/02\/grails-tutorial-for-beginners-criteria-api.html","og_locale":"en_US","og_type":"article","og_title":"Grails Tutorial for Beginners - Criteria API - Java Code Geeks","og_description":"This tutorial will show how to use Criteria API to search data from the database. Criteria is a powerful API and an elegant alternative to HQL.","og_url":"https:\/\/www.javacodegeeks.com\/2015\/02\/grails-tutorial-for-beginners-criteria-api.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2015-02-02T05:00:01+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/grails-logo.jpg","type":"image\/jpeg"}],"author":"Jonathan Tan","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Jonathan Tan","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2015\/02\/grails-tutorial-for-beginners-criteria-api.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/02\/grails-tutorial-for-beginners-criteria-api.html"},"author":{"name":"Jonathan Tan","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/aa6c54395d81041b16aff3785d6e09cb"},"headline":"Grails Tutorial for Beginners &#8211; Criteria API","datePublished":"2015-02-02T05:00:01+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/02\/grails-tutorial-for-beginners-criteria-api.html"},"wordCount":695,"commentCount":2,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/02\/grails-tutorial-for-beginners-criteria-api.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/grails-logo.jpg","keywords":["Grails"],"articleSection":["Groovy"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2015\/02\/grails-tutorial-for-beginners-criteria-api.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2015\/02\/grails-tutorial-for-beginners-criteria-api.html","url":"https:\/\/www.javacodegeeks.com\/2015\/02\/grails-tutorial-for-beginners-criteria-api.html","name":"Grails Tutorial for Beginners - Criteria API - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/02\/grails-tutorial-for-beginners-criteria-api.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/02\/grails-tutorial-for-beginners-criteria-api.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/grails-logo.jpg","datePublished":"2015-02-02T05:00:01+00:00","description":"This tutorial will show how to use Criteria API to search data from the database. Criteria is a powerful API and an elegant alternative to HQL.","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/02\/grails-tutorial-for-beginners-criteria-api.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2015\/02\/grails-tutorial-for-beginners-criteria-api.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2015\/02\/grails-tutorial-for-beginners-criteria-api.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/grails-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/grails-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2015\/02\/grails-tutorial-for-beginners-criteria-api.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"JVM Languages","item":"https:\/\/www.javacodegeeks.com\/category\/jvm-languages"},{"@type":"ListItem","position":3,"name":"Groovy","item":"https:\/\/www.javacodegeeks.com\/category\/jvm-languages\/groovy"},{"@type":"ListItem","position":4,"name":"Grails Tutorial for Beginners &#8211; Criteria API"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/aa6c54395d81041b16aff3785d6e09cb","name":"Jonathan Tan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4e0b0fc719b8ab677eb7f91293285b9dbefc8a843253329d5baa74582bf7673c?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4e0b0fc719b8ab677eb7f91293285b9dbefc8a843253329d5baa74582bf7673c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4e0b0fc719b8ab677eb7f91293285b9dbefc8a843253329d5baa74582bf7673c?s=96&d=mm&r=g","caption":"Jonathan Tan"},"description":"Jon is a professional software engineer currently working in financial trading systems. He has worked in a wide variety of projects that includes mobile games, 3D visualization, artificial intelligence, banking and trading systems. He loves to teach people and expresses his passion through blogging.","sameAs":["http:\/\/grails.asia\/"],"url":"https:\/\/www.javacodegeeks.com\/author\/jonathan-tan"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/36288","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/613"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=36288"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/36288\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/130"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=36288"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=36288"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=36288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}