title: org.hibernate.LazyInitializationException: could not initialize proxy - no Session when I use EAGER initalization
body: <p>I have hibernate mapping:</p>
  
  <p>Prepod:</p>
  
  <pre><code>@Entity
  @Table(name = "prepod")
  public class Prepod {
  
      private Long id;
      private String name;
      @Column
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      List&lt;Student&gt; students = new ArrayList&lt;Student&gt;();
  
      @ManyToMany(fetch=FetchType.EAGER)
      public List&lt;Student&gt; getStudents() {
          return students;
      }
  
      public void setStudents(List&lt;Student&gt; students) {
          this.students = students;
      }
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = "id")
      public Long getId() {
          return id;
      }
  
      public void setId(Long i) {
          id = i;
      }
  }
  </code></pre>
  
  <p>and Student:</p>
  
  <pre><code>@Entity
  @Table(name = "Student")
  public class Student {
  
      private Long id;
      private String name;
      private Long age;
      private List&lt;Prepod&gt; prepods = new ArrayList&lt;Prepod&gt;();
  
      @ManyToMany(mappedBy = "students",fetch=FetchType.EAGER)
      public List&lt;Prepod&gt; getPrepods() {
          return prepods;
      }
  
      public void setPrepods(List&lt;Prepod&gt; prepods) {
          this.prepods = prepods;
      }
  
      public Student() {
          name = null;
      }
  
      public Student(Student s) {
          name = s.getName();
      }
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = "id")
      public Long getId() {
          return id;
      }
  
      @Column(name = "name")
      public String getName() {
          return name;
      }
  
      @Column(name = "age")
      public Long getAge() {
          return age;
      }
  
      public void setId(Long i) {
          id = i;
      }
  
      public void setName(String s) {
          name = s;
      }
  
      public void setAge(Long age) {
          this.age = age;
      }
  }
  </code></pre>
  
  <p>invocation code:</p>
  
  <pre><code>Session session = HibernateUtil.getSessionFactory().openSession();
          session.beginTransaction();
          Prepod prepod = (Prepod) session.load(Prepod.class, 1l);
          Student student = (Student) session.load(Student.class, 1l);
          session.getTransaction().commit();
          session.flush();
          session.close();
                  List&lt;Student&gt; students = new ArrayList&lt;Student&gt;();
          students.add(student);
  
          List&lt;Prepod&gt; prepods = new ArrayList&lt;Prepod&gt;();
          prepods.add(prepod);
  
          prepod.setStudents(students);//exception here
          student.setPrepods(prepods);
  ....
  </code></pre>
  
  <p>When I invoke code above I see next trace:</p>
  
  <blockquote>
    <p>Exception in thread "main" org.hibernate.LazyInitializationException:
    could not initialize proxy - no Session   at
    org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:164)
      at
    org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:285)
      at
    org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:185)
      at
    logic.Prepod_$$_javassist_1.setStudents(Prepod_$$_javassist_1.java)
      at logic.Main.main(Main.java:38)</p>
  </blockquote>
  
  <p>What is the cause of this problem? </p>
  
  <p>The state of my objects is detached in two last rows?</p>
  
  <p>How to fix it?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>after good advice for editing code from Prabhakaran I have next question:</p>
  
  <p>If I update student  - note doesn't added to intermediate table of database but another behavior if I make update for prepod entity. It was happened because prepod is owner of relationship?</p>