title: failed to lazily initialize a collection of role: com.pojo.Student.phonenos, no session or session was closed
body: <p>I am learning hibernate mapping using annotation. I have completed one section. I.e. I can insert child class automatically when I save the parent table.
   <a href="https://stackoverflow.com/q/7819389/481682">see_that</a>.</p>
  
  <p>But I did n't get the child table when I am fetching the master table. Also getting an error </p>
  
  <pre><code>failed to lazily initialize a collection of role: com.pojo.one2many.unidirectional.Student.phonenos, no session or session was closed
  </code></pre>
  
  <p>My code is added here for to review you. Please go through it. And give me the great advice.
  Student.java. ( parent class)</p>
  
  <pre><code>@Entity
      @Table(name="STUDENT")
      public class Student {
      private int studentid;
      private String studentName;
      private Set &lt;Phone&gt; phonenos;
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name="studenId")
      public int getStudentid() {
          return studentid;
      }
      public void setStudentid(int studentid) {
          this.studentid = studentid;
      }
      @Column(name="studenName")
      public String getStudentName() {
          return studentName;
      }
      public void setStudentName(String studentName) {
          this.studentName = studentName;
      }
      @OneToMany(fetch = FetchType.LAZY)
      @JoinColumn(name="studenId") 
      public Set&lt;Phone&gt; getPhonenos() {
          return phonenos;
      }
      public void setPhonenos(Set&lt;Phone&gt; phonenos) {
          this.phonenos = phonenos;
      }
  </code></pre>
  
  <p>Phone.java (child class)</p>
  
  <pre><code>@Entity
  @Table(name = "PHONE")
  public class Phone {
      private int phoneid;
      private String phoneNo;
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name = "phoneId")
      public int getPhoneid() {
          return phoneid;
      }
      public void setPhoneid(int phoneid) {
          this.phoneid = phoneid;
      }
      @Column(name = "phoneno")
      public String getPhoneNo() {
          return phoneNo;
      }
      public void setPhoneNo(String phoneNo) {
          this.phoneNo = phoneNo;
      }
  </code></pre>
  
  <p>my dao class</p>
  
  <pre><code>public List&lt;Student&gt; getAllStudent() {
           List&lt;Student&gt; studentList = null;
           try {
              DetachedCriteria criteria = DetachedCriteria.forClass(Student.class);
               studentList = (List&lt;Student&gt;)getHibernateTemplate().findByCriteria(criteria);
                if(studentList != null &amp;&amp; ! studentList.isEmpty()){
                   for(Student st :studentList){
                       System.out.println(" st name : "+st.getStudentName());
                       if(st.getPhonenos() != null &amp;&amp; ! st.getPhonenos().isEmpty()){
                           for(Phone ph : st.getPhonenos()){
                               System.out.println(" ph no : "+ ph.getPhoneNo());
                           }
                       }else{
                           System.out.println("   phone number is null");
                       }
                   }
               }else{
                   System.out.println("   student null");
               }
          } catch (DataAccessException e) {
              e.printStackTrace();
          }
          return studentList;
      }
  </code></pre>
  
  <p>Out put is </p>
  
  <pre><code>failed to lazily initialize a collection of role: com.pojo.one2many.unidirectional.Student.phonenos, no session or session was closed
  </code></pre>
  
  <p>Here I am using unidirectional (foreign key)  one-To-many mapping (not joint table, bidirectional).</p>
  
  <p><strong>Summerizing my question</strong></p>
  
  <p>1) how to get child table when we fetch the parent table, vice versa</p>
  
  <p>2) what is eager and lazy fetch.</p>
  
  <p>3) unidirection, bidirection, and join table in the case of One-To-Many mapping which one is more power full.</p>