{"id":4453,"date":"2011-11-01T11:28:55","date_gmt":"2011-11-01T05:58:55","guid":{"rendered":"https:\/\/2thenew.online\/blog\/?p=4453"},"modified":"2016-11-30T13:06:46","modified_gmt":"2016-11-30T07:36:46","slug":"criteria-query-with-left-outer-join","status":"publish","type":"post","link":"https:\/\/2thenew.online\/blog\/criteria-query-with-left-outer-join\/","title":{"rendered":"Criteria Query with Left Outer Join"},"content":{"rendered":"<p>In <a title=\"grails development services\" href=\"https:\/\/2thenew.online\/grails-application-development\">Grails app development<\/a>, when using criteria queries, we often find cases for Outer joins. Lets take an example. We have two domain classes with us:<\/p>\n<p>[java]<br \/>\nclass Blog {<\/p>\n<p>\tString title<br \/>\n\tString content<\/p>\n<p>\tstatic hasMany = [comments: Comment]<\/p>\n<p>    static constraints = {<br \/>\n    }<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>and<\/p>\n<p>[java]<br \/>\nclass Comment {<\/p>\n<p>\tString text<\/p>\n<p>\tstatic belongsTo = [blog: Blog]<\/p>\n<p>    static constraints = {<br \/>\n    }<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>Now, lets say, we want get the title of all the blogs along with the count of the comments on it. The query that we try is:<\/p>\n<p>[java]<br \/>\nList blogStats = Blog.createCriteria().list {<br \/>\n\t\tcreateAlias(&#8216;comments&#8217;, &#8216;c&#8217;)<br \/>\n            \tprojections {<br \/>\n                \tgroupProperty(&#8216;id&#8217;)<br \/>\n                \tgroupProperty(&#8216;title&#8217;)<br \/>\n\t\t\tcount(&#8216;c.id&#8217;)<br \/>\n            }<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>Interestingly, we do not get what we want. We do get a list of list of id, title and comments count, but only for those blogs for which comments exist. So, with creating an alias, we just created a join but what we need is a left outer. As always, stackoverflow had the <a href=\"http:\/\/stackoverflow.com\/questions\/7154364\/how-do-i-make-a-grails-criteria-execute-as-a-left-join\">answer<\/a>. The createAlias method can take another parameter to specify that we want the join to be a Left outer join.<\/p>\n<p>We&#8217;ll need to add the following import:<\/p>\n<p>[java]import org.hibernate.criterion.CriteriaSpecification[\/java]<\/p>\n<p>And the our query looks like:<\/p>\n<p>[java]<br \/>\nList blogStats = Blog.createCriteria().list {<br \/>\n      createAlias(&#8216;comments&#8217;, &#8216;c&#8217;, CriteriaSpecification.LEFT_JOIN)<br \/>\n      projections {<br \/>\n\t\t         groupProperty(&#8216;id&#8217;)<br \/>\n                \tgroupProperty(&#8216;title&#8217;)<br \/>\n         \t\tcount(&#8216;c.id&#8217;)<br \/>\n      }<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>Looks pretty simple now that I know it \ud83d\ude42 How about you? Hope it helps.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Grails app development, when using criteria queries, we often find cases for Outer joins. Lets take an example. We have two domain classes with us: [java] class Blog { String title String content static hasMany = [comments: Comment] static constraints = { } } [\/java] and [java] class Comment { String text static belongsTo [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":68,"footnotes":""},"categories":[7],"tags":[4840,4005,4264],"class_list":["post-4453","post","type-post","status-publish","format-standard","hentry","category-grails","tag-grails","tag-grails-development","tag-groovy-and-grails"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"If you are working on Grails development project, you can use criteria query with outer Join.\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Himanshu Seth\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/2thenew.online\/blog\/criteria-query-with-left-outer-join\/\" \/>\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 with Left Outer Join | TO THE NEW Blog\" \/>\n\t\t<meta property=\"og:description\" content=\"If you are working on Grails development project, you can use criteria query with outer Join.\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/2thenew.online\/blog\/criteria-query-with-left-outer-join\/\" \/>\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 with Left Outer Join | TO THE NEW Blog\" \/>\n\t\t<meta name=\"twitter:description\" content=\"If you are working on Grails development project, you can use criteria query with outer Join.\" \/>\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-with-left-outer-join\\\/#article\",\"name\":\"Criteria Query with Left Outer Join | TO THE NEW Blog\",\"headline\":\"Criteria Query with Left Outer Join\",\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/himanshu\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"},\"datePublished\":\"2011-11-01T11:28:55+05:30\",\"dateModified\":\"2016-11-30T13:06:46+05:30\",\"inLanguage\":\"en-US\",\"commentCount\":7,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/criteria-query-with-left-outer-join\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/criteria-query-with-left-outer-join\\\/#webpage\"},\"articleSection\":\"Grails, Grails, grails development, Groovy and Grails\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/criteria-query-with-left-outer-join\\\/#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-with-left-outer-join\\\/#listItem\",\"name\":\"Criteria Query with Left Outer Join\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/criteria-query-with-left-outer-join\\\/#listItem\",\"position\":3,\"name\":\"Criteria Query with Left Outer Join\",\"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\\\/himanshu\\\/#author\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/himanshu\\\/\",\"name\":\"Himanshu Seth\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/criteria-query-with-left-outer-join\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/62e787d1a52267475ecb3fb2b7bea0715c3f4409cf080f75b5ee57e3bfa770a8?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Himanshu Seth\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/criteria-query-with-left-outer-join\\\/#webpage\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/criteria-query-with-left-outer-join\\\/\",\"name\":\"Criteria Query with Left Outer Join | TO THE NEW Blog\",\"description\":\"If you are working on Grails development project, you can use criteria query with outer Join.\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/criteria-query-with-left-outer-join\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/himanshu\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/himanshu\\\/#author\"},\"datePublished\":\"2011-11-01T11:28:55+05:30\",\"dateModified\":\"2016-11-30T13:06:46+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 with Left Outer Join | TO THE NEW Blog","description":"If you are working on Grails development project, you can use criteria query with outer Join.","canonical_url":"https:\/\/2thenew.online\/blog\/criteria-query-with-left-outer-join\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/2thenew.online\/blog\/criteria-query-with-left-outer-join\/#article","name":"Criteria Query with Left Outer Join | TO THE NEW Blog","headline":"Criteria Query with Left Outer Join","author":{"@id":"https:\/\/2thenew.online\/blog\/author\/himanshu\/#author"},"publisher":{"@id":"https:\/\/2thenew.online\/blog\/#organization"},"datePublished":"2011-11-01T11:28:55+05:30","dateModified":"2016-11-30T13:06:46+05:30","inLanguage":"en-US","commentCount":7,"mainEntityOfPage":{"@id":"https:\/\/2thenew.online\/blog\/criteria-query-with-left-outer-join\/#webpage"},"isPartOf":{"@id":"https:\/\/2thenew.online\/blog\/criteria-query-with-left-outer-join\/#webpage"},"articleSection":"Grails, Grails, grails development, Groovy and Grails"},{"@type":"BreadcrumbList","@id":"https:\/\/2thenew.online\/blog\/criteria-query-with-left-outer-join\/#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-with-left-outer-join\/#listItem","name":"Criteria Query with Left Outer Join"},"previousItem":{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog\/criteria-query-with-left-outer-join\/#listItem","position":3,"name":"Criteria Query with Left Outer Join","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\/himanshu\/#author","url":"https:\/\/2thenew.online\/blog\/author\/himanshu\/","name":"Himanshu Seth","image":{"@type":"ImageObject","@id":"https:\/\/2thenew.online\/blog\/criteria-query-with-left-outer-join\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/62e787d1a52267475ecb3fb2b7bea0715c3f4409cf080f75b5ee57e3bfa770a8?s=96&d=mm&r=g","width":96,"height":96,"caption":"Himanshu Seth"}},{"@type":"WebPage","@id":"https:\/\/2thenew.online\/blog\/criteria-query-with-left-outer-join\/#webpage","url":"https:\/\/2thenew.online\/blog\/criteria-query-with-left-outer-join\/","name":"Criteria Query with Left Outer Join | TO THE NEW Blog","description":"If you are working on Grails development project, you can use criteria query with outer Join.","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/2thenew.online\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/2thenew.online\/blog\/criteria-query-with-left-outer-join\/#breadcrumblist"},"author":{"@id":"https:\/\/2thenew.online\/blog\/author\/himanshu\/#author"},"creator":{"@id":"https:\/\/2thenew.online\/blog\/author\/himanshu\/#author"},"datePublished":"2011-11-01T11:28:55+05:30","dateModified":"2016-11-30T13:06:46+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 with Left Outer Join | TO THE NEW Blog","og:description":"If you are working on Grails development project, you can use criteria query with outer Join.","og:url":"https:\/\/2thenew.online\/blog\/criteria-query-with-left-outer-join\/","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 with Left Outer Join | TO THE NEW Blog","twitter:description":"If you are working on Grails development project, you can use criteria query with outer Join.","twitter:image":"https:\/\/2thenew.online\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png"},"aioseo_meta_data":{"post_id":"4453","title":null,"description":"If you are working on Grails development project, you can use criteria query with outer Join.","keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":"","og_description":"","og_object_type":"blog","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":"","og_custom_url":null,"og_article_section":"","og_article_tags":"","twitter_use_og":false,"twitter_card":"summary","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:26","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 with Left Outer Join\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 with Left Outer Join","link":"https:\/\/2thenew.online\/blog\/criteria-query-with-left-outer-join\/"}],"_links":{"self":[{"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/posts\/4453","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\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/comments?post=4453"}],"version-history":[{"count":0,"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/posts\/4453\/revisions"}],"wp:attachment":[{"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/media?parent=4453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/categories?post=4453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/tags?post=4453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}