title: How to get data without getting "could not initialize proxy - no Session" exception
body: <p>I have a method to retrieve data from DB:</p>
  
  <pre><code>public static List&lt;ClassA&gt; getAll() {
      Session session = HibernateUtil.getSessionFactory().openSession();
      Transaction tx = null;
      try {
          tx = session.beginTransaction();
          Criteria criteria = session.createCriteria(ClassA.class);
          List result = criteria.list();
          tx.commit();
          return result;
      } catch (HibernateException ex) {
          transactionRollBack(tx);
          log.error(ex.getMessage());
      } finally {
          session.close();
      }
      return null;
  }
  </code></pre>
  
  <p>Usage example:</p>
  
  <pre><code>List&lt;ClassA&gt; results = getAll();
  for (ClassA result : results) {
      System.out.println(result); //"could not initialize proxy - no Session" is thrown here
  }
  </code></pre>
  
  <p>If I correctly understood Hibernate documentation, this occurs because session was closed. But how can I get data and close session (because it needs to be closed before next transaction that is about to begin in a minute)?</p>
  
  <p><strong>Updated</strong></p>
  
  <p>Here is ClassA content including <code>toString()</code>:</p>
  
  <pre><code>@Entity
  @Table(name = "classA")
  public class ClassA implements Serializable {
  
      private static final long serialVersionUID = 5163856045361200311L;
      private int id;
      private String name;
  
      private Set&lt;ClassB&gt; classB = new HashSet&lt;ClassB&gt;(0);
  
      public ClassA() {}
      public ClassA(int id, String name) {
          this.id = id;
          this.name = name;
      }
  
      public ClassA(String name) {
          this.name = name;
      }
  
      public ClassA(ClassA classA) {
          this.id = classA.getId();
          this.name = classA.getName();
      }
  
      @Id
      @GeneratedValue(strategy = AUTO)
      @Column(name = "id", nullable = false, unique = true)
      public int getId() {
          return id;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      @Column(name = "name")
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      @OneToMany(fetch = LAZY, mappedBy = "classA_id")
      public Set&lt;ClassB&gt; getClassB() {
          return classB;
      }
  
      public void setClassB(Set&lt;ClassB&gt; classB) {
          this.classB = classB;
      }
  
      @Override
      public String toString() {
          return "ClassA{ id=" + id + ", name='" + name + ", classB =" + classB + '}';
      }
  }
  </code></pre>
  
  <p>It seems that the problem is with <code>ClassB</code>, but I cannot understand what it is. Here is <code>ClassB</code>:</p>
  
  <pre><code>@Entity
  @Table(name = "classB")
  public class ClassB implements Serializable{
  
      private static final long serialVersionUID = -8481028941663340911L;
      private int id;
      private Date date;
      private ClassA classA_id;
      private int classA_counter;
  
      public ClassB() {}
  
      public ClassB(ClassB rec) {
          this.id = rec.getId();
          this.date = rec.getDate();
          this.classA_id = rec.getClassA_id();
          this.classA_counter = rec.classA_counter();
      }
  
      @Id
      @GeneratedValue
      @Column(name = "id", nullable = false)
      public int getId() {
          return id;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      @Temporal(DATE)
      @Column(name = "date")
      public Date getDate() {
          return date;
      }
  
      public void setDate(Date date) {
          this.date = date;
      }
  
      @ManyToOne(fetch = LAZY)
      @JoinColumn(name = "classA_id", nullable = false)
      public ClassA getCLassA_id() {
          return classA_id;
      }
  
      public void setClassA_id(ClassA classA_id) {
          this.classA_id = classA_id;
      }
  
      @Column(name = "classA_counter", columnDefinition = "int default 0")
      public int getClassA_counter() {
          return classA_counter;
      }
  
      public void setClassA_counter(int classA_counter) {
          this.classA_counter = classA_counter;
      }
  
      @Override
      public String toString() {
          return "ClassB { id=" + id + ", date=" + date + ", classA_id=" + classA_id.getName() + ", classA_counter=" + classA_counter +'}';
      }
  }
  </code></pre>