{"id":1057,"date":"2010-06-14T15:22:03","date_gmt":"2010-06-14T09:52:03","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=1057"},"modified":"2016-12-19T15:28:30","modified_gmt":"2016-12-19T09:58:30","slug":"grails-createcriteria-query-example","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/grails-createcriteria-query-example\/","title":{"rendered":"Grails criteria query example for unidirectional one-to-many relationships"},"content":{"rendered":"<p>Following is an example of unidirectional one-to-many relationship:<\/p>\n<pre lang=\"groovy\">\r\nclass Employee {\r\n    String name\r\n    static hasMany = [roles: Role]\r\n}\r\n\r\nclass Role {\r\n    String name\r\n}\r\n<\/pre>\n<p>How can we find all the employees with a particular role &#8211; &#8220;Grails Developer&#8221;?<\/p>\n<pre lang=\"groovy\">\r\nRole role = Role.findByName(\"Grails Developer\")\r\n<\/pre>\n<p>One way of doing this can be: <\/p>\n<pre lang=\"groovy\">\r\nEmployee.list().findAll{role in it.roles}\r\n<\/pre>\n<p>This is an inefficient way since, we are fetching all the records from the database every time. Also, in case we are showing paginated view of results, we will need to manage pagination ourself. <\/p>\n<p>A better way of this will be using createCriteria() query:<\/p>\n<pre lang=\"groovy\">\r\nList<Employee> employees = Employee.createCriteria().list{\r\n    roles{\r\n        eq('id', role.id)\r\n    }\t\r\n    firstResult(20)\r\n    maxResults(20)\t\r\n}\t\r\n<\/pre>\n<p>I am wondering what will be the query in case Role.groovy is an enum.<\/p>\n<p>\u2013<br \/>\n~Aman Aggarwal<br \/>\naman@intelligrape.com<\/p>\n<p>http:\/\/www.tothenew.com\/blog\/<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Following is an example of unidirectional one-to-many relationship: class Employee { String name static hasMany = [roles: Role] } class Role { String name } How can we find all the employees with a particular role &#8211; &#8220;Grails Developer&#8221;? Role role = Role.findByName(&#8220;Grails Developer&#8221;) One way of doing this can be: Employee.list().findAll{role in it.roles} This [&hellip;]<\/p>\n","protected":false},"author":282,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":33},"categories":[7],"tags":[266,269,4840,265,270],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1057"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/282"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=1057"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1057\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=1057"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=1057"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=1057"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}