title: avoid hibernate lazy initialization exception
body: <p>I have two classes called Lecturer and Course. My Lecturer class has a List of Course objects a variable as shown</p>
  
  <pre><code>@OneToMany(mappedBy = "lecturer")
  @NotFound(action = NotFoundAction.IGNORE)
  private List&lt;Course&gt; courseList = new ArrayList&lt;Course&gt;();
  </code></pre>
  
  <p>In my course class I have a Lecturer object as a variable as below.</p>
  
  <pre><code>@ManyToOne
  @JoinTable(name = "course_lecturer", joinColumns = @JoinColumn(name = "course_id"), inverseJoinColumns = @JoinColumn(name = "lecturer_id"))
  private Lecturer lecturer;
  </code></pre>
  
  <p>Now I have a spring Controller method that returns a json object of Lecturer as below.</p>
  
  <pre><code>@RequestMapping(value = "/getlecturerbyid", method = RequestMethod.POST)
  public @ResponseBody
  Object getLecturer(@ModelAttribute(value = "id") Lecturer lecturer) {
      Map&lt;String, Object&gt; response = new HashMap&lt;String, Object&gt;();
      response.put("message", "succeess");
      response.put("lecturer", lecturer);
      return response;
  }
  </code></pre>
  
  <p>The problem is it throws a lazy initialization exception. Therefore I set fetch type in both variables in Lecturer and Course class to eager. Now the problem is it infinitely tries to fetch objects eagerly(if I unset fetch of either, it raises the same exception).</p>
  
  <p>On the other hand if I remove the variable of either class, then the hibernate does not know that there is a relationship for another. eg: if I remove course list from Lecturer and delete one of the Lecturer objects then hibernate does not remove the course objects that are related with the deleted Lecturer object. so i have to write a hibernate query and remove it manually in the code.</p>
  
  <p>Can you please suggest me on how to over come this issue?</p>