{"id":62,"date":"2007-01-17T14:21:48","date_gmt":"2007-01-17T13:21:48","guid":{"rendered":"http:\/\/blogs.igalia.com\/eocanha\/?p=62"},"modified":"2015-11-08T00:21:10","modified_gmt":"2015-11-07T23:21:10","slug":"curious-facts-about-hibernate","status":"publish","type":"post","link":"https:\/\/eocanha.org\/blog\/2007\/01\/17\/curious-facts-about-hibernate\/","title":{"rendered":"Curious facts about Hibernate"},"content":{"rendered":"<p>Hibernate is a very useful framework to do Object-Relational mapping in Java. It saves a lot of time, gives the programmer a lot of flexibility and allows you to do amazing things like mapping object inheritance.<\/p>\n<p>Nevertheless, it has some &#8220;strange&#8221; behaviours that, in my opinion, seem a bit courterintuitive. I&#8217;m going to tell you about the way Hibernate translates object relations into joins.<\/p>\n<p>Let&#8217;s imagine we have the class Person, with attributes id and name, and the class Car, with attributes id and brand. Class Person has a one-to-one relationship with Car named car, and there&#8217;ll be persons who have a car, and persons who haven&#8217;t.<br \/>\nIf we want to query about the cars each people have, the &#8220;intuitive&#8221; query would be this:<\/p>\n<blockquote><p>select p.name, p.car from Person p<\/p><\/blockquote>\n<p>And the &#8220;intuitive&#8221; result would be: (John, Car instance #1); (Peter, null); (Mary, Car instance #2), isn&#8217;t it?<\/p>\n<p>It isn&#8217;t. The real thing is that Hibernate translates the HQL sencence to a SQL one that selects from Person and does an (inner) join with Car, in order to instantiate a Car with all its attribute values set up. As a consequence of that inner join, all the people having a null car are missing from the result.<\/p>\n<p>Intuitive solution: force the outer join by hand<\/p>\n<blockquote><p>select p.name, c.id, c.brand from Person p left join p.car c<\/p><\/blockquote>\n<p>Non intuitive (but valid) solution: use the required attributes directly<\/p>\n<blockquote><p>select p.name, p.car.id, p.car.brand from Person p<\/p><\/blockquote>\n<p>In this last query, I would expect a NullPointerException when the person has no car, since if there is no car, there is no attribute to evaluate, but the exception doesn&#8217;t get thrown, the problem doesn&#8217;t arise and you get the work done.<\/p>\n<p>Quite strange, isn&#8217;t it?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hibernate is a very useful framework to do Object-Relational mapping in Java. It saves a lot of time, gives the programmer a lot of flexibility and allows you to do amazing things like mapping object inheritance. Nevertheless, it has some &#8220;strange&#8221; behaviours that, in my opinion, seem a bit courterintuitive. I&#8217;m going to tell you &hellip; <a href=\"https:\/\/eocanha.org\/blog\/2007\/01\/17\/curious-facts-about-hibernate\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Curious facts about Hibernate<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/eocanha.org\/blog\/wp-json\/wp\/v2\/posts\/62"}],"collection":[{"href":"https:\/\/eocanha.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eocanha.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eocanha.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/eocanha.org\/blog\/wp-json\/wp\/v2\/comments?post=62"}],"version-history":[{"count":1,"href":"https:\/\/eocanha.org\/blog\/wp-json\/wp\/v2\/posts\/62\/revisions"}],"predecessor-version":[{"id":439,"href":"https:\/\/eocanha.org\/blog\/wp-json\/wp\/v2\/posts\/62\/revisions\/439"}],"wp:attachment":[{"href":"https:\/\/eocanha.org\/blog\/wp-json\/wp\/v2\/media?parent=62"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eocanha.org\/blog\/wp-json\/wp\/v2\/categories?post=62"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eocanha.org\/blog\/wp-json\/wp\/v2\/tags?post=62"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}