{"id":1651,"date":"2012-08-21T19:00:00","date_gmt":"2012-08-21T19:00:00","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/2012\/10\/hibernate-lazyeager-loading-example.html"},"modified":"2012-10-22T06:24:01","modified_gmt":"2012-10-22T06:24:01","slug":"hibernate-lazyeager-loading-example","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2012\/08\/hibernate-lazyeager-loading-example.html","title":{"rendered":"Hibernate lazy\/eager loading example"},"content":{"rendered":"<div dir=\"ltr\" style=\"text-align: left\">This post will focus on why and how we use the concepts known as                     <a href=\"http:\/\/docs.oracle.com\/javaee\/5\/api\/javax\/persistence\/FetchType.html#LAZY\">LAZY <\/a>and                     <a href=\"http:\/\/docs.oracle.com\/javaee\/5\/api\/javax\/persistence\/FetchType.html#EAGER\">EAGER <\/a>loading in an application and how to use Spring&#8217;s hibernate template to load our LAZY entities in an EAGER fashion.<\/p>\n<p>And of course as the title itself suggests, we will show this by an example. The scenario is as such;                    <\/p>\n<p>You are a parent who has a kid with a lot of toys. But the current issue is whenever you call him (we assume you have a boy), he comes to you with all his toys as well. Now this is an issue since you do not want him carrying around his toys all the time.                    <\/p>\n<p>So being the rationale parent, you go right ahead and define the toys of the child as LAZY. Now whenever you call him, he just comes to you without his toys.                    <\/p>\n<p>But you are faced with another issue. When the time comes for a family trip, you want him to bring along his toys because the kid will be bored with the trip otherwise. But since you strictly enforced LAZY on the child&#8217;s toy, you are unable to ask him to bring along the toys. This is where EAGER fetching comes into play. Let us first see our domain classes.<\/p>\n<pre class=\"brush:java\">package com.fetchsample.example.domain;\r\n\r\nimport java.util.HashSet;\r\nimport java.util.Set;\r\n\r\nimport javax.persistence.CascadeType;\r\nimport javax.persistence.Column;\r\nimport javax.persistence.Entity;\r\nimport javax.persistence.FetchType;\r\nimport javax.persistence.GeneratedValue;\r\nimport javax.persistence.GenerationType;\r\nimport javax.persistence.Id;\r\nimport javax.persistence.NamedQuery;\r\nimport javax.persistence.OneToMany;\r\nimport javax.persistence.Table;\r\n\r\n\/**\r\n * Holds information about the child\r\n * \r\n * @author dinuka.arseculeratne\r\n * \r\n *\/\r\n@Entity\r\n@Table(name = 'CHILD')\r\n@NamedQuery(name = 'findChildByName', query = 'select DISTINCT(chd) from Child chd left join fetch chd.toyList where chd.childName=:chdName')\r\npublic class Child {\r\n\r\n public static interface Constants {\r\n  public static final String FIND_CHILD_BY_NAME_QUERY = 'findChildByName';\r\n\r\n  public static final String CHILD_NAME_PARAM = 'chdName';\r\n }\r\n\r\n @Id\r\n @GeneratedValue(strategy = GenerationType.AUTO)\r\n \/**\r\n  * The primary key of the CHILD table\r\n  *\/\r\n private Long childId;\r\n\r\n @Column(name = 'CHILD_NAME')\r\n \/**\r\n  * The name of the child\r\n  *\/\r\n private String childName;\r\n\r\n @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)\r\n \/**\r\n  * The toys the child has. We do not want the child to have the same toy more than\r\n  * once, so we have used a set here.\r\n  *\/\r\n private Set&lt;Toy&gt; toyList = new HashSet&lt;Toy&gt;();\r\n\r\n public Long getChildId() {\r\n  return childId;\r\n }\r\n\r\n public void setChildId(Long childId) {\r\n  this.childId = childId;\r\n }\r\n\r\n public String getChildName() {\r\n  return childName;\r\n }\r\n\r\n public void setChildName(String childName) {\r\n  this.childName = childName;\r\n }\r\n\r\n public Set&lt;Toy&gt; getToyList() {\r\n  return toyList;\r\n }\r\n\r\n public void addToy(Toy toy) {\r\n  toyList.add(toy);\r\n }\r\n\r\n}\r\n<\/pre>\n<pre class=\"brush:java\">package com.fetchsample.example.domain;\r\n\r\nimport javax.persistence.Column;\r\nimport javax.persistence.Entity;\r\nimport javax.persistence.GeneratedValue;\r\nimport javax.persistence.GenerationType;\r\nimport javax.persistence.Id;\r\nimport javax.persistence.Table;\r\n\r\n\/**\r\n * Hols data related to the toys a child possess\r\n * \r\n * @author dinuka.arseculeratne\r\n * \r\n *\/\r\n@Entity\r\n@Table(name = 'TOYS')\r\npublic class Toy {\r\n\r\n @Id\r\n @GeneratedValue(strategy = GenerationType.AUTO)\r\n @Column(name = 'TOY_ID')\r\n \/**\r\n  * The primary key of the TOYS table\r\n  *\/\r\n private Long toyId;\r\n\r\n @Column(name = 'TOY_NAME')\r\n \/**\r\n  * The name of the toy\r\n  *\/\r\n private String toyName;\r\n\r\n public Long getToyId() {\r\n  return toyId;\r\n }\r\n\r\n public void setToyId(Long toyId) {\r\n  this.toyId = toyId;\r\n }\r\n\r\n public String getToyName() {\r\n  return toyName;\r\n }\r\n\r\n public void setToyName(String toyName) {\r\n  this.toyName = toyName;\r\n }\r\n\r\n @Override\r\n public int hashCode() {\r\n  final int prime = 31;\r\n  int result = 1;\r\n  result = prime * result + ((toyName == null) ? 0 : toyName.hashCode());\r\n  return result;\r\n }\r\n\r\n @Override\r\n \/**\r\n  * Overriden because within the child class we use a Set to\r\n  * hold all unique toys\r\n  *\/\r\n public boolean equals(Object obj) {\r\n  if (this == obj)\r\n   return true;\r\n  if (obj == null)\r\n   return false;\r\n  if (getClass() != obj.getClass())\r\n   return false;\r\n  Toy other = (Toy) obj;\r\n  if (toyName == null) {\r\n   if (other.toyName != null)\r\n    return false;\r\n  } else if (!toyName.equals(other.toyName))\r\n   return false;\r\n  return true;\r\n }\r\n\r\n @Override\r\n public String toString() {\r\n  return 'Toy [toyId=' + toyId + ', toyName=' + toyName + ']';\r\n }\r\n\r\n}\r\n<\/pre>\n<p>So as you can see, we have two simple entities representing the child and toy. The child has a one-to-many relationship with the toys which means one child can have many toys (oh how i miss my childhood days). Afterwards we need to interact with the data, so let us go ahead and define out DAO(Data access object) interface and implementation.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<pre class=\"brush:java\">package com.fetchsample.example.dao;\r\n\r\nimport org.springframework.transaction.annotation.Propagation;\r\nimport org.springframework.transaction.annotation.Transactional;\r\n\r\nimport com.fetchsample.example.domain.Child;\r\n\r\n\/**\r\n * The basic contract for dealing with the {@link Child} entity\r\n * \r\n * @author dinuka.arseculeratne\r\n * \r\n *\/\r\n@Transactional(propagation = Propagation.REQUIRED, readOnly = false)\r\npublic interface ChildDAO {\r\n\r\n \/**\r\n  * This method will create a new instance of a child in the child table\r\n  * \r\n  * @param child\r\n  *            the entity to be persisted\r\n  *\/\r\n public void persistChild(Child child);\r\n\r\n \/**\r\n  * Retrieves a child without his\/her toys\r\n  * \r\n  * @param childId\r\n  *            the primary key of the child table\r\n  * @return the child with the ID passed in if found\r\n  *\/\r\n public Child getChildByIdWithoutToys(Long childId);\r\n\r\n \/**\r\n  * Retrieves the child with his\/her toys\r\n  * \r\n  * @param childId\r\n  *            the primary key of the child table\r\n  * @return the child with the ID passed in if found\r\n  *\/\r\n public Child getChildByIdWithToys(Long childId);\r\n\r\n \/**\r\n  * Retrieves the child by the name and with his\/her toys\r\n  * \r\n  * @param childName\r\n  *            the name of the child\r\n  * @return the child entity that matches the name passed in\r\n  *\/\r\n public Child getChildByNameWithToys(String childName);\r\n\r\n}\r\n<\/pre>\n<pre class=\"brush:java\">package com.fetchsample.example.dao.hibernate;\r\n\r\nimport org.springframework.orm.hibernate3.support.HibernateDaoSupport;\r\n\r\nimport com.fetchsample.example.dao.ChildDAO;\r\nimport com.fetchsample.example.domain.Child;\r\n\r\n\/**\r\n * The hibernate implementation of our {@link ChildDAO} interface\r\n * \r\n * @author dinuka.arseculeratne\r\n * \r\n *\/\r\npublic class ChildDAOHibernateImpl extends HibernateDaoSupport implements\r\n  ChildDAO {\r\n\r\n \/**\r\n  * {@inheritDoc}\r\n  *\/\r\n public void persistChild(Child child) {\r\n  getHibernateTemplate().persist(child);\r\n }\r\n\r\n \/**\r\n  * {@inheritDoc}\r\n  *\/\r\n public Child getChildByIdWithoutToys(Long childId) {\r\n  return getHibernateTemplate().get(Child.class, childId);\r\n }\r\n\r\n \/**\r\n  * {@inheritDoc}\r\n  *\/\r\n public Child getChildByIdWithToys(Long childId) {\r\n  Child child = getChildByIdWithoutToys(childId);\r\n  \/**\r\n   * Since by default the toys are not loaded, we call the hibernate\r\n   * template's initialize method to populate the toys list of that\r\n   * respective child.\r\n   *\/\r\n  getHibernateTemplate().initialize(child.getToyList());\r\n  return child;\r\n }\r\n\r\n \/**\r\n  * {@inheritDoc}\r\n  *\/\r\n public Child getChildByNameWithToys(String childName) {\r\n  return (Child) getHibernateTemplate().findByNamedQueryAndNamedParam(\r\n    Child.Constants.FIND_CHILD_BY_NAME_QUERY,\r\n    Child.Constants.CHILD_NAME_PARAM, childName).get(0);\r\n\r\n }\r\n\r\n}\r\n<\/pre>\n<p>A simple contract. I have four main methods. The first one of course just persists a child entity to the database.                    <\/p>\n<p>The second method retrieves the Child by the primary key passed in, but does not fetch the toys.                    <\/p>\n<p>The third method first fetches the Child and then retrieves the Child&#8217;s toys using the Hibernate template&#8217;s                     <a href=\"http:\/\/static.springsource.org\/spring\/docs\/2.5.x\/api\/org\/springframework\/orm\/hibernate3\/HibernateTemplate.html#initialize(java.lang.Object)\">initialize() <\/a>method. Note that when you call the initialize() method, hibernate will fetch you LAZY defined collection to the Child proxy you retrieved.                    <\/p>\n<p>The final method also retrieves the Child&#8217;s toys, but this time using a named query. If we go back to the Child entity&#8217;s Named query, you can see that we have used &#8216;left join                     <strong>fetch<\/strong>&#8216;. It is the keyword                     <strong>fetch <\/strong>that actually will initialize the toys collection as well when returning the Child entity that qualifies. Finally i have my main class to test our functionality;<\/p>\n<pre class=\"brush:java\">package com.fetchsample.example;\r\n\r\nimport org.springframework.context.ApplicationContext;\r\nimport org.springframework.context.support.ClassPathXmlApplicationContext;\r\n\r\nimport com.fetchsample.example.dao.ChildDAO;\r\nimport com.fetchsample.example.domain.Child;\r\nimport com.fetchsample.example.domain.Toy;\r\n\r\n\/**\r\n * A test class\r\n * \r\n * @author dinuka.arseculeratne\r\n * \r\n *\/\r\npublic class ChildTest {\r\n\r\n public static void main(String[] args) {\r\n  ApplicationContext context = new ClassPathXmlApplicationContext(\r\n    'com\/fetchsample\/example\/spring-context.xml');\r\n\r\n  \/**\r\n   * First we initialize a child\r\n   *\/\r\n  Child child = new Child();\r\n\r\n  \/**\r\n   * A cool ben 10 action figure\r\n   *\/\r\n  Toy ben10 = new Toy();\r\n  ben10.setToyName('Ben 10 figure');\r\n\r\n  \/**\r\n   * A even more cooler spider man action figure\r\n   *\/\r\n  Toy spiderMan = new Toy();\r\n  spiderMan.setToyName('Spider man figure');\r\n\r\n  child.setChildName('John');\r\n  \/**\r\n   * Add the toys to the collection\r\n   *\/\r\n  child.addToy(ben10);\r\n  child.addToy(spiderMan);\r\n\r\n  ChildDAO childDAO = (ChildDAO) context.getBean('childDAO');\r\n\r\n  childDAO.persistChild(child);\r\n\r\n  Child childWithoutToys = childDAO.getChildByIdWithoutToys(1L);\r\n  \/\/ The following line will throw a lazy initialization error since we have\r\n  \/\/ defined fetch type as LAZY in the Child domain class.\r\n  \/\/ System.out.println(childWithToys.getToyList().size());\r\n\r\n  Child childWithToys = childDAO.getChildByIdWithToys(1L);\r\n  System.out.println(childWithToys.getToyList().size());\r\n\r\n  Child childByNameWithToys = childDAO.getChildByNameWithToys('John');\r\n\r\n  System.out.println(childByNameWithToys.getToyList().size());\r\n\r\n }\r\n}\r\n<\/pre>\n<p>Defining your base Entity as LAZY is a good practice since in many occasions, you might not want the collections within an entity, but just want to interact with the data in your base entity. But in the event of you needing the data of your collections, then you can use either of the methods mentioned before.                    <\/p>\n<p>That is about it for today. For anyone who wants to try out the example, i have uploaded it                     <a href=\"https:\/\/sites.google.com\/site\/dinukatech\/FetchSample.rar?attredirects=0&amp;d=1\">here<\/a>.                    <\/p>\n<p><strong><i>Reference: <\/i><\/strong><a href=\"http:\/\/dinukaroshan.blogspot.com\/2012\/08\/lazyeager-loading-using-hibernate-by.html\">Lazy\/Eager loading using hibernate by example<\/a> from our <a href=\"http:\/\/www.javacodegeeks.com\/p\/jcg.html\">JCG partner<\/a> Dinuka Arseculeratne at the <a href=\"http:\/\/dinukaroshan.blogspot.com\/\">My Journey Through IT <\/a> blog.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This post will focus on why and how we use the concepts known as LAZY and EAGER loading in an application and how to use Spring&#8217;s hibernate template to load our LAZY entities in an EAGER fashion. And of course as the title itself suggests, we will show this by an example. The scenario is &hellip;<\/p>\n","protected":false},"author":56,"featured_media":153,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[31],"class_list":["post-1651","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-enterprise-java","tag-jboss-hibernate"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Hibernate lazy\/eager loading example - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"This post will focus on why and how we use the concepts known as LAZY and EAGER loading in an application and how to use Spring&#039;s hibernate template to\" \/>\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\/2012\/08\/hibernate-lazyeager-loading-example.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Hibernate lazy\/eager loading example - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"This post will focus on why and how we use the concepts known as LAZY and EAGER loading in an application and how to use Spring&#039;s hibernate template to\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2012\/08\/hibernate-lazyeager-loading-example.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:published_time\" content=\"2012-08-21T19:00:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2012-10-22T06:24:01+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/jboss-hibernate-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=\"Dinuka Arseculeratne\" \/>\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=\"Dinuka Arseculeratne\" \/>\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\\\/2012\\\/08\\\/hibernate-lazyeager-loading-example.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/08\\\/hibernate-lazyeager-loading-example.html\"},\"author\":{\"name\":\"Dinuka Arseculeratne\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/dcc3fe556ddfb0a9003206d37dab7f85\"},\"headline\":\"Hibernate lazy\\\/eager loading example\",\"datePublished\":\"2012-08-21T19:00:00+00:00\",\"dateModified\":\"2012-10-22T06:24:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/08\\\/hibernate-lazyeager-loading-example.html\"},\"wordCount\":529,\"commentCount\":3,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/08\\\/hibernate-lazyeager-loading-example.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/jboss-hibernate-logo.jpg\",\"keywords\":[\"JBoss Hibernate\"],\"articleSection\":[\"Enterprise Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/08\\\/hibernate-lazyeager-loading-example.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/08\\\/hibernate-lazyeager-loading-example.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/08\\\/hibernate-lazyeager-loading-example.html\",\"name\":\"Hibernate lazy\\\/eager loading example - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/08\\\/hibernate-lazyeager-loading-example.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/08\\\/hibernate-lazyeager-loading-example.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/jboss-hibernate-logo.jpg\",\"datePublished\":\"2012-08-21T19:00:00+00:00\",\"dateModified\":\"2012-10-22T06:24:01+00:00\",\"description\":\"This post will focus on why and how we use the concepts known as LAZY and EAGER loading in an application and how to use Spring's hibernate template to\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/08\\\/hibernate-lazyeager-loading-example.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/08\\\/hibernate-lazyeager-loading-example.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/08\\\/hibernate-lazyeager-loading-example.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/jboss-hibernate-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/jboss-hibernate-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/08\\\/hibernate-lazyeager-loading-example.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\":\"Hibernate lazy\\\/eager loading example\"}]},{\"@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\\\/dcc3fe556ddfb0a9003206d37dab7f85\",\"name\":\"Dinuka Arseculeratne\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/63322ad58386141a936a1643794f9f949c8a0006b1134a697df69ae1a04c2aca?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/63322ad58386141a936a1643794f9f949c8a0006b1134a697df69ae1a04c2aca?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/63322ad58386141a936a1643794f9f949c8a0006b1134a697df69ae1a04c2aca?s=96&d=mm&r=g\",\"caption\":\"Dinuka Arseculeratne\"},\"sameAs\":[\"http:\\\/\\\/dinukaroshan.blogspot.com\\\/\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/Dinuka-Arseculeratne\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Hibernate lazy\/eager loading example - Java Code Geeks","description":"This post will focus on why and how we use the concepts known as LAZY and EAGER loading in an application and how to use Spring's hibernate template to","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\/2012\/08\/hibernate-lazyeager-loading-example.html","og_locale":"en_US","og_type":"article","og_title":"Hibernate lazy\/eager loading example - Java Code Geeks","og_description":"This post will focus on why and how we use the concepts known as LAZY and EAGER loading in an application and how to use Spring's hibernate template to","og_url":"https:\/\/www.javacodegeeks.com\/2012\/08\/hibernate-lazyeager-loading-example.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2012-08-21T19:00:00+00:00","article_modified_time":"2012-10-22T06:24:01+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/jboss-hibernate-logo.jpg","type":"image\/jpeg"}],"author":"Dinuka Arseculeratne","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Dinuka Arseculeratne","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2012\/08\/hibernate-lazyeager-loading-example.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2012\/08\/hibernate-lazyeager-loading-example.html"},"author":{"name":"Dinuka Arseculeratne","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/dcc3fe556ddfb0a9003206d37dab7f85"},"headline":"Hibernate lazy\/eager loading example","datePublished":"2012-08-21T19:00:00+00:00","dateModified":"2012-10-22T06:24:01+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2012\/08\/hibernate-lazyeager-loading-example.html"},"wordCount":529,"commentCount":3,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2012\/08\/hibernate-lazyeager-loading-example.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/jboss-hibernate-logo.jpg","keywords":["JBoss Hibernate"],"articleSection":["Enterprise Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2012\/08\/hibernate-lazyeager-loading-example.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2012\/08\/hibernate-lazyeager-loading-example.html","url":"https:\/\/www.javacodegeeks.com\/2012\/08\/hibernate-lazyeager-loading-example.html","name":"Hibernate lazy\/eager loading example - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2012\/08\/hibernate-lazyeager-loading-example.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2012\/08\/hibernate-lazyeager-loading-example.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/jboss-hibernate-logo.jpg","datePublished":"2012-08-21T19:00:00+00:00","dateModified":"2012-10-22T06:24:01+00:00","description":"This post will focus on why and how we use the concepts known as LAZY and EAGER loading in an application and how to use Spring's hibernate template to","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2012\/08\/hibernate-lazyeager-loading-example.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2012\/08\/hibernate-lazyeager-loading-example.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2012\/08\/hibernate-lazyeager-loading-example.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/jboss-hibernate-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/jboss-hibernate-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2012\/08\/hibernate-lazyeager-loading-example.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":"Hibernate lazy\/eager loading example"}]},{"@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\/dcc3fe556ddfb0a9003206d37dab7f85","name":"Dinuka Arseculeratne","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/63322ad58386141a936a1643794f9f949c8a0006b1134a697df69ae1a04c2aca?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/63322ad58386141a936a1643794f9f949c8a0006b1134a697df69ae1a04c2aca?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/63322ad58386141a936a1643794f9f949c8a0006b1134a697df69ae1a04c2aca?s=96&d=mm&r=g","caption":"Dinuka Arseculeratne"},"sameAs":["http:\/\/dinukaroshan.blogspot.com\/"],"url":"https:\/\/www.javacodegeeks.com\/author\/Dinuka-Arseculeratne"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/1651","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\/56"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=1651"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/1651\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/153"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=1651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=1651"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=1651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}