{"id":26560,"date":"2015-09-13T22:09:46","date_gmt":"2015-09-13T16:39:46","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=26560"},"modified":"2015-09-14T10:24:54","modified_gmt":"2015-09-14T04:54:54","slug":"write-criteria-with-has-many-string-types","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/write-criteria-with-has-many-string-types\/","title":{"rendered":"Write Criteria with has many String Types"},"content":{"rendered":"<p>Taking further <a href=\"http:\/\/www.tothenew.com\/blog\/querying-domain-class-with-hasmany-string-type\/\" target=\"_blank\">this scenario<\/a>, what if we need to query for partial matches of String, like just &#8216;rai&#8217; not &#8216;grails&#8217;.<\/p>\n<p>We face many issues while creating criteria with has many relationship on <em>String<\/em> types as normal way of querying doesn&#8217;t work with has many relationship for <em>String<\/em>\u00a0 types.<\/p>\n<p>One way of getting this done is to use <em>HQL<\/em>. Above mentioned blog is a very good solution when we need to query for exact match. But what if\u00a0requirement is to query for partial matches of String. We\u00a0would modify our <em>HQL<\/em>\u00a0to add more complex <em>where<\/em> clauses, in turn, we\u00a0would end up writing more complex and error prone <em>HQL<\/em>.<\/p>\n<p>Solution is to write <em>Alias<\/em>. Creating criteria is easier and less error prone. Here we can use <em>Alias<\/em> in <em>Criteria<\/em> to\u00a0get this done.<\/p>\n<p>Considering the same example as described in the <a href=\"http:\/\/www.tothenew.com\/blog\/querying-domain-class-with-hasmany-string-type\/\" target=\"_blank\">referred blog<\/a>:<\/p>\n<p>[java]class Blog {<\/p>\n<p>static hasMany = [tags: String]<\/p>\n<p>} [\/java]<\/p>\n<p><em>createAlias<\/em> can be used to query for all the blogs containing string &#8216;grails&#8217; in its tags.<\/p>\n<p>[java]createCriteria().list {<\/p>\n<p>createAlias(&#8216;tags&#8217; , &#8216;n&#8217;)<br \/>\nilike(&#8216;n.elements&#8217;,&#8217;%&#8217;+tagName+&#8217;%&#8217;)<\/p>\n<p>}[\/java]<\/p>\n<p>PS : This solution works above 2.4.x grails version.<\/p>\n<p>Hope This helps !!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Taking further this scenario, what if we need to query for partial matches of String, like just &#8216;rai&#8217; not &#8216;grails&#8217;. We face many issues while creating criteria with has many relationship on String types as normal way of querying doesn&#8217;t work with has many relationship for String\u00a0 types. One way of getting this done is [&hellip;]<\/p>\n","protected":false},"author":227,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":2},"categories":[1],"tags":[4840,265],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/26560"}],"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\/227"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=26560"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/26560\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=26560"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=26560"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=26560"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}