{"id":2651,"date":"2011-01-17T00:26:18","date_gmt":"2011-01-16T18:56:18","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=2651"},"modified":"2016-12-19T15:19:22","modified_gmt":"2016-12-19T09:49:22","slug":"grails-criteria-query-createalias-made-it-easy","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/grails-criteria-query-createalias-made-it-easy\/","title":{"rendered":"Grails criteria query : &#8216;createAlias&#8217; made it easy"},"content":{"rendered":"<p>Hello friends,<\/p>\n<p>In my grails project, I was finding it difficult to write a query using criteria which can be read as &#8216;list all customers whose current account balance is more than minimum account balance of that customer&#8217;. The simplified form of the domain is given below :<br \/>\n[java]<br \/>\nclass Customer {<br \/>\n   String name<br \/>\n   Account account<br \/>\n   BigDecimal minAccountBalance<br \/>\n   &#8230;<br \/>\n}<\/p>\n<p>class Account {<br \/>\n   BigDecimal currentBalance<br \/>\n   &#8230;<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>As far as my understanding goes, criteria provides comparison between the fields in the same domain but I needed to compare values between two domains. After spending some time on it, I could make it work using  &#8216;createAlias&#8217;  statement. Before you go through the query, please have a look at one of the nice blog written by Rob Fletcher which is available <a href=\"http:\/\/adhockery.blogspot.com\/2009\/06\/querying-by-association-redux.html\" target=\"_blank\">here<\/a>  to understand what &#8216;createAlias&#8217; does. <\/p>\n<p>Here is the query :<br \/>\n[java]<br \/>\nCustomer.createCriteria().list(){<br \/>\n      createAlias(&#8216;account&#8217;, &#8216;acc&#8217;)<br \/>\n      gtProperty(&#8216;acc.currentBalance&#8217;, &#8216;minAccountBalance&#8217;)<br \/>\n    }<br \/>\n[\/java]<\/p>\n<p>Hope this helped!<\/p>\n<p>Cheers!<\/p>\n<p>~~Amit Jain~~<br \/>\namit@intelligrape.com<br \/>\nhttp:\/\/www.tothenew.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello friends, In my grails project, I was finding it difficult to write a query using criteria which can be read as &#8216;list all customers whose current account balance is more than minimum account balance of that customer&#8217;. The simplified form of the domain is given below : [java] class Customer { String name Account [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":45},"categories":[7],"tags":[500,499,498,4840],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/2651"}],"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\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=2651"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/2651\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=2651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=2651"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=2651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}