{"id":119,"date":"2010-06-09T06:51:38","date_gmt":"2010-06-09T13:51:38","guid":{"rendered":"http:\/\/journeyman.ivystreetinc.com\/?p=119"},"modified":"2010-06-09T06:51:44","modified_gmt":"2010-06-09T13:51:44","slug":"defaults-hibernate-lazy-jpa-eager","status":"publish","type":"post","link":"http:\/\/10kdev.net\/?p=119","title":{"rendered":"Defaults: Hibernate Lazy; JPA Eager"},"content":{"rendered":"<p>Now this is interesting.<\/p>\n<p>We were trying to debug some Hibernate rich objects we had converted from hbm files to annotations, and the objects were blowing up.&nbsp; One of our more expertish developers with some Hibernate experience insisted that default fetching was lazy.&nbsp; We decided to accept that as the word and left certain objects with their hbms attributing their failure to annotations not doing something mysterious correctly.<\/p>\n<p>Well, I started to write some unit and integration tests on one of the newer annotated objects and was stubbing the data out with HSQL.&nbsp; I noticed that there were errors in running the tests of the nature of&nbsp; failure to hydrate objects.&nbsp; It smelled like eager fetching.&nbsp; <\/p>\n<p>So I made the fetching on the many-to-one relationships explicity lazy in an annotation.&nbsp; I couldn&#8217;t believe it.&nbsp; Searching through the documentation I found this.&nbsp; Who would have thought that Hibernate and JPA have taken two different approaches!<\/p>\n<h1><font color=\"#000066\"><i>Fetch strategies and types<\/i><\/font><\/h1>\n<p><font color=\"#000066\"><i>In native Hibernate, many-to-one association are lazy by default.<br \/>\nThis means that selecting a list of objects with an HQL query will not<br \/>\ninitialize the objects at the other end of the many-to-one association.<br \/>\nIn JPA the default fetch type is eager (i.e. non-lazy), with separate<br \/>\nselects. This means that any JPAQL query for an entity that has<br \/>\nmany-to-one associations in it will result in an N+1 SELECTS problem<br \/>\neven if those associations are not used. To eliminate the unnecessary<br \/>\nqueries, simply set the fetch type to LAZY using:<\/i><\/font><\/p>\n<div class=\"code panel\" style=\"border-width: 1px;\">\n<div class=\"codeContent panelContent\">\n<pre class=\"code-java\">&lt;font color=\"#000066\"&gt;&lt;i&gt; @ManyToOne(fetch=FetchType.LAZY)\n&lt;\/i&gt;&lt;\/font&gt;<\/pre>\n<\/div>\n<\/div>\n<p><font color=\"#000066\"><i>Then, in any JPA query the association can be eagerly fetched using <tt>left join fetch<\/tt>.<\/i><\/font><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Now this is interesting. We were trying to debug some Hibernate rich objects we had converted from hbm files to annotations, and the objects were blowing up.&nbsp; One of our more expertish developers with some Hibernate experience insisted that default fetching was lazy.&nbsp; We decided to accept that as the word and left certain objects [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[9,3],"tags":[],"_links":{"self":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts\/119"}],"collection":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=119"}],"version-history":[{"count":1,"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts\/119\/revisions"}],"predecessor-version":[{"id":120,"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts\/119\/revisions\/120"}],"wp:attachment":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=119"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}