title: How to insert ManyToOne relationship record in hibernate?
body: <p>I have two classes as following, </p>
  
  <p><strong>Human.java</strong></p>
  
  <pre><code>@Entity
  @Inheritance(strategy = InheritanceType.JOINED)
  public class Human implements Serializable {
  
     private long id;
     private String name;
      ....
  }
  </code></pre>
  
  <p><strong>Student.java</strong> </p>
  
  <pre><code>@Entity
  @DynamicUpdate
  public class Student extends MyFactories {
  
      private List&lt;Know&gt; KnowList;
  
      @OneToMany(fetch = FetchType.LAZY)
      public List&lt;Know&gt; getKnowlist() {
          return knowlist;
      }
  
      public void setKnowlist(List&lt;Know&gt; KnowList) {
          return Knowlist;
      }
  }
  </code></pre>
  
  <p><strong>Know.java</strong></p>
  
  <pre><code>@Entity
  public class Know implements Serializable {
  
      private long id;
      private Human hu;
      private Student st;
  
      ....
  
      @ManyToOne 
      public Person getHu() {
          return hu;
      }
  
      @ManyToOne
      public Client getSt() {
          return st;
      }
  
      ....  setters .....
  }
  </code></pre>
  
  <p><strong>Code</strong></p>
  
  <pre><code>            Know kw = new Know();
              kw.setSt(studentObject);
              kw.setHu(humanObject);
              session.save(kw);
              tx.commit();
  </code></pre>
  
  <p>I am able to insert into Know table but hibernate does not insert any record to student_know table which it has created.</p>
  
  <p>I have found this <a href="https://stackoverflow.com/questions/11746499/how-to-resolve-failed-to-lazily-initialize-a-collection-of-role-exception">answer</a> but it says I need to use that method if I always want to retrieve all the records. Which I do not (at times, I may just need to retrieve the student class not list of its know)</p>
  
  <pre><code>    System.out.println(this.student.getKnowList().size());
  </code></pre>
  
  <p>When I try to access the list it runs into following exception.</p>
  
  <pre><code>org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.myproject.Student.knowList, could not initialize proxy - no Session
  </code></pre>