title: Using lazy for properties in Hibernate
body: <p>The lazy attribute for property tag in hibernate allows to lazily load the property as per the link: <a href="http://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/mapping.html#mapping-declaration-property" rel="nofollow">http://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/mapping.html#mapping-declaration-property</a></p>
  
  <blockquote>
    <p>lazy (optional - defaults to false): specifies that this property
    should be fetched lazily when the instance variable is first accessed.
    It requires build-time bytecode instrumentation.</p>
  </blockquote>
  
  <p>But when I tried to set lazy=true for one of my property it is not loading it lazily in this example:</p>
  
  <p>Hibernate Mapping file:</p>
  
  <pre><code>&lt;hibernate-mapping package="org.hibernate.tutorial.domain"&gt;
  
      &lt;class name="Event" table="EVENTS" select-before-update="true"&gt;
          &lt;id name="id" column="EVENT_ID"&gt;
              &lt;generator class="native" /&gt;
          &lt;/id&gt;
          &lt;property name="date" type="timestamp" column="EVENT_DATE" /&gt;
          &lt;property name="title" lazy="true"/&gt;
  
          &lt;set name="participants" table="PERSON_EVENT" inverse="true"&gt;
              &lt;key column="EVENT_ID" /&gt;
              &lt;many-to-many column="PERSON_ID" class="Person" /&gt;
          &lt;/set&gt;
      &lt;/class&gt;
  
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>Program:</p>
  
  <pre><code>public static void main(String[] args) {
          Session session = HibernateUtil.getSessionFactory().getCurrentSession();
          session.beginTransaction();
          Event event = (Event) session.get(Event.class, 135L);
          session.getTransaction().commit();
          System.out.println(event);
          HibernateUtil.getSessionFactory().close();
      }
  </code></pre>
  
  <p>Query generated by hibernate:</p>
  
  <pre><code>Hibernate: select event0_.EVENT_ID as EVENT1_0_0_, event0_.EVENT_DATE as EVENT2_0_0_, event0_.title as title0_0_ from EVENTS event0_ where event0_.EVENT_ID=?
  </code></pre>
  
  <p>Please help me in understanding why the lazy is not working in this case?</p>