title: Lazy Initialized object load without fetch with hibernate Spring
body: <p><strong>SchoolDto</strong>:</p>
  
  <pre><code>    @Id
      @Column(name = "c_sm_npk_id", nullable = false)
      @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "school_sequence")
      @SequenceGenerator(name = "school_sequence", sequenceName = "t_school_master_c_sm_npk_id_seq", initialValue = 1, allocationSize = 1)
      private Long id;
      @Column(name="c_sm_vnm_name")
      private String name;
      @ManyToOne(cascade = {CascadeType.MERGE},fetch=FetchType.LAZY)
      UserDto owner;
  
     /*Many other fileds omit for simplicity with getter/setter methods*/
  </code></pre>
  
  <p><strong>UserDto</strong></p>
  
  <pre><code>public class UserDto implements Serializable {
      private static final long serialVersionUID = 1L;
  
      public UserDto() {
      }
  
      @Id
      @Column(name = "c_um_npk_id", nullable = false)
      @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "user_sequence")
      @SequenceGenerator(name = "user_sequence", sequenceName = "t_user_master_c_um_npk_id_seq", initialValue = 1, allocationSize = 1)
      private Long id;
      @Column(name = "c_um_vnm_username", nullable = false)
      private String username;
      @Column(name = "c_um_vnm_email_address")
      private String emailId;
      @JsonSerialize(using = JsonDateSerializer.class)
      @Column(name = "c_um_dnm_birth_date",nullable=false)
      @Temporal(TemporalType.DATE)
      private Date birthdate;
      @Column(name="c_um_nnm_age",nullable=false)
  
      private int age;
      @Column(name = "c_um_vnm_first_name")
      private String firstName;
      @Column(name = "c_um_vnm_last_name")
      private String lastName;
      @Column(name = "c_um_vnm_mobile_number")
      private String mobileNumber;
      @Column(name = "c_um_vnm_gender")
      private String gender;
      @JsonManagedReference("user-role")
      @OneToMany(mappedBy = "userDto", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
      private List&lt;RoleDto&gt; roleDtos;
   /* getter setter of properties */
  }
  </code></pre>
  
  <p>Here, I have defined owner as Lazy collection. but when I try to find all schools as below:</p>
  
  <pre><code>public List&lt;SchoolDto&gt; getAllSchoolsWithAdvanceSearch(SchoolSearchDto schoolSearchDto) {
          return getEntityManager().createQuery("SELECT s from SchoolDto s").setFirstResult(schoolSearchDto.getFrom()).setMaxResults(schoolSearchDto.getSize()).getResultList();
      }
  </code></pre>
  
  <p>this return list of schools, with all its owner info including roles of the owner (which is again Lazy collection form UserDto) , when I log the hibernate query,I get only one select as below:</p>
  
  <pre><code>select
          schooldto0_.c_sm_npk_id as c1_24_,
          schooldto0_.c_sm_vnm_address_line_1 as c2_24_,
          schooldto0_.owner_c_um_npk_id as owner17_24_
      from
          t_school_master schooldto0_
  </code></pre>
  
  <p>during search, I come across that it may be Jackson serialization issue, so I did below changes as per <a href="https://stackoverflow.com/questions/25906985/jackson-do-not-serialize-lazy-objects">jackson - do not serialize lazy objects</a> &amp; <a href="https://stackoverflow.com/questions/21708339/avoid-jackson-serialization-on-non-fetched-lazy-objects">Avoid Jackson serialization on non fetched lazy objects</a> as below</p>
  
  <p><strong>application-servlet.xml</strong></p>
  
  <pre><code>    &lt;mvc:annotation-driven&gt;
          &lt;mvc:message-converters&gt;
              &lt;ref bean="jsonHttpMessageConverter"/&gt;
          &lt;/mvc:message-converters&gt;
      &lt;/mvc:annotation-driven&gt;
  
  
         &lt;bean id="jsonHttpMessageConverter"  class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"&gt;
                  &lt;property name="objectMapper"&gt;
                      &lt;bean class="com.util.HibernateAwareObjectMapper" /&gt;
                  &lt;/property&gt;
              &lt;/bean&gt;
  </code></pre>
  
  <p><strong>HibernateAwareObjectMapper.java</strong></p>
  
  <pre><code>public HibernateAwareObjectMapper() {
          this.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
          Hibernate4Module module = new Hibernate4Module();
          module.configure(Hibernate4Module.Feature.FORCE_LAZY_LOADING, false);
          module.disable(Hibernate4Module.Feature.USE_TRANSIENT_ANNOTATION);
          registerModule(module);
      }
  </code></pre>
  
  <p>Note : I need owner detail later, so I can not mark it as <code>@JsonIgnore</code>.</p>
  
  <p>Please help me to solve this issue. Thanks in advance</p>
  
  <p><strong>Edit :</strong> current output</p>
  
  <pre><code>0:  {
         "id": 1
         "name": "school1"
         "address1": null
         owner : {
             "id" : 1,
             "firstName": "Rutesha",
             "lastName" : "Patel",
             "roles":[{
                  "name" : "SCHOOL_ADMIN"
              }]
         }
  }
  </code></pre>
  
  <p><strong>Edit :</strong> Expected output</p>
  
  <pre><code>0:  {
         "id": 1
         "name": "school1"
         "address1": null
  }
  
  OR 
  
  0:  {
         "id": 1
         "name": "school1"
         "address1": null
         owner : null
  }
  </code></pre>