{"id":34550,"date":"2014-12-17T22:00:26","date_gmt":"2014-12-17T20:00:26","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/?p=34550"},"modified":"2014-12-16T16:12:43","modified_gmt":"2014-12-16T14:12:43","slug":"eager-fetching-is-a-code-smell","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2014\/12\/eager-fetching-is-a-code-smell.html","title":{"rendered":"EAGER fetching is a code smell"},"content":{"rendered":"<h2>Introduction<\/h2>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/2013\/11\/hibernate-facts-the-importance-of-fetch-strategy.html\">Hibernate fetching strategies<\/a> can really make a difference between an application that barely crawls and a highly responsive one. In this post I\u2019ll explain why you should prefer query based fetching instead of global fetch plans.<\/p>\n<h2>Fetching 101<\/h2>\n<p>Hibernate defines <a href=\"https:\/\/docs.jboss.org\/hibernate\/orm\/4.3\/manual\/en-US\/html\/ch20.html#performance-fetching\">four association retrieving strategies<\/a>:<br \/>\n&nbsp;<br \/>\n&nbsp;<\/p>\n<table border=\"1\" style=\"width:850px\">\n<tbody align=\"center\">\n<tr>\n<th>Fetching Strategy<\/th>\n<th>Description<\/th>\n<\/tr>\n<tr>\n<td>Join<\/td>\n<td>The association is OUTER JOINED in the original SELECT statement<\/td>\n<\/tr>\n<tr>\n<td>Select<\/td>\n<td>An additional SELECT statement is used to retrieve the associated entity(entities)<\/td>\n<\/tr>\n<tr>\n<td>Subselect<\/td>\n<td>An additional SELECT statement is used to retrieve the whole associated collection. This mode is meant for to-many associations<\/td>\n<\/tr>\n<tr>\n<td>Batch<\/td>\n<td>An additional number of SELECT statements is used to retrieve the whole associated collection. Each additional SELECT will retrieve a fixed number of associated entities. This mode is meant for to-many associations<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<br \/>\nThese fetching strategies might be applied in the following scenarios:<\/p>\n<ul>\n<li>the association is always initialized along with its owner (e.g. <a href=\"https:\/\/docs.oracle.com\/javaee\/7\/api\/javax\/persistence\/FetchType.html#EAGER\">EAGER<\/a> FetchType)<\/li>\n<li>the uninitialized association (e.g. <a href=\"https:\/\/docs.oracle.com\/javaee\/7\/api\/javax\/persistence\/FetchType.html#LAZY\">LAZY<\/a> FetchType) is navigated, therefore the association must be retrieved with a secondary SELECT<\/li>\n<\/ul>\n<p>The Hibernate mappings fetching information forms the <em>global fetch plan<\/em>. At query time, we may override the global fetch plan, but <strong>only for LAZY associations<\/strong>. For this we can use the <em>fetch<\/em> HQL\/JPQL\/Criteria directive. EAGER associations cannot be overridden, therefore tying your application to the global fetch plan.<\/p>\n<p><a href=\"https:\/\/docs.jboss.org\/hibernate\/orm\/3.3\/reference\/en\/html\/performance.html\">Hibernate 3<\/a> acknowledged that LAZY should be the default association fetching strategy:<\/p>\n<blockquote>\n<p>By default, Hibernate3 uses lazy select fetching for collections and lazy proxy fetching for single-valued associations. These defaults make sense for most associations in the majority of applications.<\/p>\n<\/blockquote>\n<p>This decision was taken after noticing many performance issues associated with Hibernate 2 default eager fetching. Unfortunately JPA has taken a different approach and decided that to-many associations be LAZY while to-one relationships be fetched eagerly.<\/p>\n<table border=\"1\" style=\"width:850px\">\n<tbody align=\"center\">\n<tr>\n<th>Association type<\/th>\n<th>Default fetching policy<\/th>\n<\/tr>\n<tr>\n<td><a href=\"https:\/\/docs.oracle.com\/javaee\/7\/api\/javax\/persistence\/OneToMany.html#fetch%28%29\">@OneTMany<\/a><\/td>\n<td>LAZY<\/td>\n<\/tr>\n<tr>\n<td><a href=\"https:\/\/docs.oracle.com\/javaee\/7\/api\/javax\/persistence\/ManyToMany.html#fetch%28%29\">@ManyToMany<\/a><\/td>\n<td>LAZY<\/td>\n<\/tr>\n<tr>\n<td><a href=\"https:\/\/docs.oracle.com\/javaee\/7\/api\/javax\/persistence\/ManyToOne.html#fetch%28%29\">@ManyToOne<\/a><\/td>\n<td>EAGER<\/td>\n<\/tr>\n<tr>\n<td><a href=\"https:\/\/docs.oracle.com\/javaee\/7\/api\/javax\/persistence\/OneToOne.html#fetch%28%29\">@OneToOne<\/a><\/td>\n<td>EAGER<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h2>EAGER fetching inconsistencies<\/h2>\n<p>While it may be convenient to just mark associations as EAGER, delegating the fetching responsibility to Hibernate, it\u2019s advisable to resort to query based fetch plans.<\/p>\n<p>An EAGER association will always be fetched and the fetching strategy is not consistent across all querying techniques.<\/p>\n<p>Next, I\u2019m going to demonstrate how EAGER fetching behaves for all Hibernate querying variants. I will reuse the same entity model I\u2019ve previously introduced in my <a href=\"2013\/10\/17\/hibernate-facts-the-importance-of-fetch-strategy\/\">fetching strategies article<\/a>:<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/product2.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-34629\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/product2.png\" alt=\"product2\" width=\"601\" height=\"955\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/product2.png 601w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/product2-188x300.png 188w\" sizes=\"(max-width: 601px) 100vw, 601px\" \/><\/a><\/p>\n<p>The Product entity has the following associations:<\/p>\n<pre class=\" brush:java;wrap-lines:false\">@ManyToOne(fetch = FetchType.EAGER)\r\n@JoinColumn(name = \"company_id\", nullable = false)\r\nprivate Company company;\r\n\r\n@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = \"product\", optional = false)\r\nprivate WarehouseProductInfo warehouseProductInfo;\r\n\r\n@ManyToOne(fetch = FetchType.LAZY)\r\n@JoinColumn(name = \"importer_id\")\r\nprivate Importer importer;\r\n\r\n@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = \"product\", orphanRemoval = true)\r\n@OrderBy(\"index\")\r\nprivate Set&lt;Image&gt; images = new LinkedHashSet&lt;Image&gt;();<\/pre>\n<p>The company association is marked as EAGER and Hibernate will always employ a fetching strategy to initialize it along with its owner entity.<\/p>\n<h2>Persistence Context loading<\/h2>\n<p>First we\u2019ll load the entity using the Persistence Context API:<\/p>\n<pre class=\" brush:java\">Product product = entityManager.find(Product.class, productId);<\/pre>\n<p>Which generates the following SQL SELECT statement:<\/p>\n<pre class=\" brush:sql\">Query:{[\r\nselect \r\n    product0_.id as id1_18_1_, \r\n    product0_.code as code2_18_1_, \r\n    product0_.company_id as company_6_18_1_, \r\n    product0_.importer_id as importer7_18_1_, \r\n    product0_.name as name3_18_1_, \r\n    product0_.quantity as quantity4_18_1_, \r\n    product0_.version as version5_18_1_, \r\n    company1_.id as id1_6_0_, \r\n    company1_.name as name2_6_0_ \r\nfrom Product product0_ \r\ninner join Company company1_ on product0_.company_id=company1_.id \r\nwhere product0_.id=?][1]<\/pre>\n<p>The EAGER company association was retrieved using an inner join. For <em>M<\/em> such associations the owner entity table is going to be joined <em>M<\/em> times.<\/p>\n<p>Each extra join adds up to the overall query complexity and execution time. If we don\u2019t even use all these associations, for every possible business scenario, then we\u2019ve just paid the extra performance penalty for nothing in return.<\/p>\n<h3>Fetching using JPQL and Criteria<\/h3>\n<pre class=\" brush:java\">Product product = entityManager.createQuery(\r\n\t\"select p \" +\r\n\t\t\t\"from Product p \" +\r\n\t\t\t\"where p.id = :productId\", Product.class)\r\n\t.setParameter(\"productId\", productId)\r\n\t.getSingleResult();<\/pre>\n<p>or with<\/p>\n<pre class=\" brush:java\">CriteriaBuilder cb = entityManager.getCriteriaBuilder();\r\nCriteriaQuery&lt;Product&gt; cq = cb.createQuery(Product.class);\r\nRoot&lt;Product&gt; productRoot = cq.from(Product.class);\r\ncq.where(cb.equal(productRoot.get(\"id\"), productId));\r\nProduct product = entityManager.createQuery(cq).getSingleResult();<\/pre>\n<p>Generates the following SQL SELECT statements:<\/p>\n<pre class=\" brush:sql\">Query:{[\r\nselect \r\n    product0_.id as id1_18_, \r\n    product0_.code as code2_18_, \r\n    product0_.company_id as company_6_18_, \r\n    product0_.importer_id as importer7_18_, \r\n    product0_.name as name3_18_, \r\n    product0_.quantity as quantity4_18_, \r\n    product0_.version as version5_18_ \r\nfrom Product product0_ \r\nwhere product0_.id=?][1]} \r\n\r\nQuery:{[\r\nselect \r\n    company0_.id as id1_6_0_, \r\n    company0_.name as name2_6_0_ \r\nfrom Company company0_ \r\nwhere company0_.id=?][1]}<\/pre>\n<p>Both JPQL and Criteria queries default to <em>select<\/em> fetching, therefore issuing a secondary select for each individual EAGER association. The larger the associations number, the more additional individual SELECTS, the more it will affect our application performance.<\/p>\n<h2>Hibernate Criteria API<\/h2>\n<p>While JPA 2.0 added support for Criteria queries, Hibernate has long been offering a specific <a href=\"https:\/\/docs.jboss.org\/hibernate\/orm\/4.3\/manual\/en-US\/html\/ch17.html\">dynamic query implementation<\/a>.<\/p>\n<p>If the EntityManager implementation delegates method calls the the legacy Session API, the JPA Criteria implementation was written from scratch. That\u2019s the reason why <a href=\"http:\/\/stackoverflow.com\/questions\/27314299\/why-would-hibernates-createquery-and-createcriteria-return-a-different-number-o\/27316466#27316466\">Hibernate and JPA Criteria API behave differently<\/a> for similar querying scenarios.<\/p>\n<p>The previous example Hibernate Criteria equivalent looks like this:<\/p>\n<pre class=\" brush:java\">Product product = (Product) session.createCriteria(Product.class)\r\n\t.add(Restrictions.eq(\"id\", productId))\r\n\t.uniqueResult();<\/pre>\n<p>And the associated SQL SELECT is:<\/p>\n<pre class=\" brush:sql\">Query:{[\r\nselect \r\n    this_.id as id1_3_1_, \r\n    this_.code as code2_3_1_, \r\n    this_.company_id as company_6_3_1_, \r\n    this_.importer_id as importer7_3_1_, \r\n    this_.name as name3_3_1_, \r\n    this_.quantity as quantity4_3_1_, \r\n    this_.version as version5_3_1_, \r\n    hibernatea2_.id as id1_0_0_, \r\n    hibernatea2_.name as name2_0_0_ \r\nfrom Product this_ \r\ninner join Company hibernatea2_ on this_.company_id=hibernatea2_.id \r\nwhere this_.id=?][1]}<\/pre>\n<p>This query uses the <em>join<\/em> fetch strategy as opposed to <em>select<\/em> fetching, employed by JPQL\/HQL and Criteria API.<\/p>\n<h3>Hibernate Criteria and to-many EAGER collections<\/h3>\n<p>Let\u2019s see what happens when the <em>image<\/em> collection fetching strategy is set to EAGER:<\/p>\n<pre class=\" brush:java;wrap-lines:false\">@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = \"product\", orphanRemoval = true)\r\n@OrderBy(\"index\")\r\nprivate Set&lt;Image&gt; images = new LinkedHashSet&lt;Image&gt;();<\/pre>\n<p>The following SQL is going to be generated:<\/p>\n<pre class=\" brush:sql\">Query:{[\r\nselect \r\n    this_.id as id1_3_2_, \r\n    this_.code as code2_3_2_, \r\n    this_.company_id as company_6_3_2_, \r\n    this_.importer_id as importer7_3_2_, \r\n    this_.name as name3_3_2_, \r\n    this_.quantity as quantity4_3_2_, \r\n    this_.version as version5_3_2_, \r\n    hibernatea2_.id as id1_0_0_, \r\n    hibernatea2_.name as name2_0_0_, \r\n    images3_.product_id as product_4_3_4_, \r\n    images3_.id as id1_1_4_, \r\n    images3_.id as id1_1_1_, \r\n    images3_.index as index2_1_1_, \r\n    images3_.name as name3_1_1_, \r\n    images3_.product_id as product_4_1_1_ \r\nfrom Product this_ \r\ninner join Company hibernatea2_ on this_.company_id=hibernatea2_.id \r\nleft outer join Image images3_ on this_.id=images3_.product_id \r\nwhere this_.id=? \r\norder by images3_.index][1]}<\/pre>\n<p>Hibernate Criteria doesn\u2019t automatically groups the parent entities list. Because of the one-to-many children table JOIN, for each child entity we are going to get a new parent entity object reference (all pointing to the same object in our current Persistence Context):<\/p>\n<pre class=\" brush:java\">product.setName(\"TV\");\r\nproduct.setCompany(company);\r\n\r\nImage frontImage = new Image();\r\nfrontImage.setName(\"front image\");\r\nfrontImage.setIndex(0);\r\n\r\nImage sideImage = new Image();\r\nsideImage.setName(\"side image\");\r\nsideImage.setIndex(1);\r\n\r\nproduct.addImage(frontImage);\r\nproduct.addImage(sideImage);\r\n\r\nList products = session.createCriteria(Product.class)\r\n\t.add(Restrictions.eq(\"id\", productId))\r\n\t.list();\r\nassertEquals(2, products.size());\r\nassertSame(products.get(0), products.get(1));<\/pre>\n<p>Because we have two image entities, we will get two Product entity references, both pointing to the same first level cache entry.<\/p>\n<p>To fix it we need to instruct Hibernate Criteria to use distinct root entities:<\/p>\n<pre class=\" brush:java\">List products = session.createCriteria(Product.class)\r\n\t.add(Restrictions.eq(\"id\", productId))\r\n\t.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY)\r\n\t.list();\r\nassertEquals(1, products.size());<\/pre>\n<h2>Conclusion<\/h2>\n<p>The EAGER fetching strategy is a code smell. Most often it\u2019s used for simplicity sake without considering the long-term performance penalties. The fetching strategy should never be the entity mapping responsibility. Each business use case has different entity load requirements and therefore the fetching strategy should be delegated to each individual query.<\/p>\n<p>The global fetch plan should only define LAZY associations, which are fetched on a per query basis. Combined with the <a href=\"2013\/12\/10\/hibernate-facts-always-check-criteria-api-sql-queries\/\">always check generated queries strategy<\/a>, the query based fetch plans can improve application performance and reduce maintaining costs.<\/p>\n<ul>\n<li>Code available for <a href=\"https:\/\/github.com\/vladmihalcea\/hibernate-master-class\">Hibernate<\/a> and <a href=\"https:\/\/github.com\/vladmihalcea\/vladmihalcea.wordpress.com\/tree\/master\/hibernate-facts\">JPA<\/a>.<\/li>\n<\/ul>\n<div class=\"attribution\">\n<table>\n<tbody>\n<tr>\n<td><span class=\"reference\">Reference: <\/span><\/td>\n<td><a href=\"http:\/\/vladmihalcea.com\/2014\/12\/15\/eager-fetching-is-a-code-smell\/\">EAGER fetching is a code smell<\/a> from our <a href=\"http:\/\/www.javacodegeeks.com\/jcg\/\">JCG partner<\/a> Vlad Mihalcea at the <a href=\"http:\/\/vladmihalcea.com\/\">Vlad Mihalcea&#8217;s Blog<\/a> blog.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Hibernate fetching strategies can really make a difference between an application that barely crawls and a highly responsive one. In this post I\u2019ll explain why you should prefer query based fetching instead of global fetch plans. Fetching 101 Hibernate defines four association retrieving strategies: &nbsp; &nbsp; Fetching Strategy Description Join The association is OUTER &hellip;<\/p>\n","protected":false},"author":507,"featured_media":112,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[33],"class_list":["post-34550","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-enterprise-java","tag-jpa"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>EAGER fetching is a code smell<\/title>\n<meta name=\"description\" content=\"Introduction Hibernate fetching strategies can really make a difference between an application that barely crawls and a highly responsive one. In this\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2014\/12\/eager-fetching-is-a-code-smell.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"EAGER fetching is a code smell\" \/>\n<meta property=\"og:description\" content=\"Introduction Hibernate fetching strategies can really make a difference between an application that barely crawls and a highly responsive one. In this\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2014\/12\/eager-fetching-is-a-code-smell.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/vlad.mihalcea.71\" \/>\n<meta property=\"article:published_time\" content=\"2014-12-17T20:00:26+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Vlad Mihalcea\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Vlad Mihalcea\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/12\\\/eager-fetching-is-a-code-smell.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/12\\\/eager-fetching-is-a-code-smell.html\"},\"author\":{\"name\":\"Vlad Mihalcea\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/2c2d5059ee4fd88b1b3b9e52efc5b129\"},\"headline\":\"EAGER fetching is a code smell\",\"datePublished\":\"2014-12-17T20:00:26+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/12\\\/eager-fetching-is-a-code-smell.html\"},\"wordCount\":875,\"commentCount\":24,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/12\\\/eager-fetching-is-a-code-smell.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"keywords\":[\"JPA\"],\"articleSection\":[\"Enterprise Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/12\\\/eager-fetching-is-a-code-smell.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/12\\\/eager-fetching-is-a-code-smell.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/12\\\/eager-fetching-is-a-code-smell.html\",\"name\":\"EAGER fetching is a code smell\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/12\\\/eager-fetching-is-a-code-smell.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/12\\\/eager-fetching-is-a-code-smell.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"datePublished\":\"2014-12-17T20:00:26+00:00\",\"description\":\"Introduction Hibernate fetching strategies can really make a difference between an application that barely crawls and a highly responsive one. In this\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/12\\\/eager-fetching-is-a-code-smell.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/12\\\/eager-fetching-is-a-code-smell.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/12\\\/eager-fetching-is-a-code-smell.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"width\":150,\"height\":150,\"caption\":\"java-interview-questions-answers\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/12\\\/eager-fetching-is-a-code-smell.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Enterprise Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/enterprise-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"EAGER fetching is a code smell\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/2c2d5059ee4fd88b1b3b9e52efc5b129\",\"name\":\"Vlad Mihalcea\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f9f4ac0b2229b9f9fb993393b822ffbf63e60c1665a244176d3c4728565a9a9f?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f9f4ac0b2229b9f9fb993393b822ffbf63e60c1665a244176d3c4728565a9a9f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f9f4ac0b2229b9f9fb993393b822ffbf63e60c1665a244176d3c4728565a9a9f?s=96&d=mm&r=g\",\"caption\":\"Vlad Mihalcea\"},\"description\":\"Vlad Mihalcea is a software architect passionate about software integration, high scalability and concurrency challenges.\",\"sameAs\":[\"http:\\\/\\\/vladmihalcea.wordpress.com\\\/\",\"https:\\\/\\\/www.facebook.com\\\/vlad.mihalcea.71\",\"http:\\\/\\\/www.linkedin.com\\\/pub\\\/vlad-mihalcea\\\/20\\\/a59\\\/580\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/vlad-mihalcea\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"EAGER fetching is a code smell","description":"Introduction Hibernate fetching strategies can really make a difference between an application that barely crawls and a highly responsive one. In this","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/2014\/12\/eager-fetching-is-a-code-smell.html","og_locale":"en_US","og_type":"article","og_title":"EAGER fetching is a code smell","og_description":"Introduction Hibernate fetching strategies can really make a difference between an application that barely crawls and a highly responsive one. In this","og_url":"https:\/\/www.javacodegeeks.com\/2014\/12\/eager-fetching-is-a-code-smell.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_author":"https:\/\/www.facebook.com\/vlad.mihalcea.71","article_published_time":"2014-12-17T20:00:26+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","type":"image\/jpeg"}],"author":"Vlad Mihalcea","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Vlad Mihalcea","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2014\/12\/eager-fetching-is-a-code-smell.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/12\/eager-fetching-is-a-code-smell.html"},"author":{"name":"Vlad Mihalcea","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/2c2d5059ee4fd88b1b3b9e52efc5b129"},"headline":"EAGER fetching is a code smell","datePublished":"2014-12-17T20:00:26+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/12\/eager-fetching-is-a-code-smell.html"},"wordCount":875,"commentCount":24,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/12\/eager-fetching-is-a-code-smell.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","keywords":["JPA"],"articleSection":["Enterprise Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2014\/12\/eager-fetching-is-a-code-smell.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2014\/12\/eager-fetching-is-a-code-smell.html","url":"https:\/\/www.javacodegeeks.com\/2014\/12\/eager-fetching-is-a-code-smell.html","name":"EAGER fetching is a code smell","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/12\/eager-fetching-is-a-code-smell.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/12\/eager-fetching-is-a-code-smell.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","datePublished":"2014-12-17T20:00:26+00:00","description":"Introduction Hibernate fetching strategies can really make a difference between an application that barely crawls and a highly responsive one. In this","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/12\/eager-fetching-is-a-code-smell.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2014\/12\/eager-fetching-is-a-code-smell.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2014\/12\/eager-fetching-is-a-code-smell.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","width":150,"height":150,"caption":"java-interview-questions-answers"},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2014\/12\/eager-fetching-is-a-code-smell.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Enterprise Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/enterprise-java"},{"@type":"ListItem","position":4,"name":"EAGER fetching is a code smell"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/2c2d5059ee4fd88b1b3b9e52efc5b129","name":"Vlad Mihalcea","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/f9f4ac0b2229b9f9fb993393b822ffbf63e60c1665a244176d3c4728565a9a9f?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/f9f4ac0b2229b9f9fb993393b822ffbf63e60c1665a244176d3c4728565a9a9f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/f9f4ac0b2229b9f9fb993393b822ffbf63e60c1665a244176d3c4728565a9a9f?s=96&d=mm&r=g","caption":"Vlad Mihalcea"},"description":"Vlad Mihalcea is a software architect passionate about software integration, high scalability and concurrency challenges.","sameAs":["http:\/\/vladmihalcea.wordpress.com\/","https:\/\/www.facebook.com\/vlad.mihalcea.71","http:\/\/www.linkedin.com\/pub\/vlad-mihalcea\/20\/a59\/580"],"url":"https:\/\/www.javacodegeeks.com\/author\/vlad-mihalcea"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/34550","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/507"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=34550"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/34550\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/112"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=34550"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=34550"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=34550"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}