title: OUT OF MEMORY in hibernate
body: <p>Hi I have created many to one relationship in hibernate.
  Following is the code for that.</p>
  
  <p>there are thousands of records present in B table which is link to single record of table A.  When i used getBList() method it will returns thousands of record and JAVA goes OUT OF MEMORY.
  So how can i solve this problem.</p>
  
  <pre><code>@Entity
  @Table(name = "A")
  public class A {
  
      private int Id;
      private String aName;
      private List&lt;MksReleaseInfo&gt; bList;
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name = "id")
      public int getId() {
          return releaseId;
      }
  
      public void setId(final int Id) {
          this.Id = Id;
      }
  
      @Column(name = "aname", unique = true)
      public String getAName() {
          return aName;
      }
  
      public void setAName(final String aName) {
          this.aName = aName;
      }
      @OneToMany(mappedBy = "aName")
      public List&lt;MksReleaseInfo&gt; getBList() {
          return bList;
      }
  
      public void setBList(final List&lt;B&gt; bList) {
          this.bList = bList;
      }
  }   
  
  
  @Entity
  @Table(name = "B")
  public class B {
  
      private int bIndex;
      private int bpriority;
      private A aName;
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name = "id")
      protected int getBIndex() {
          return mksReleaseInfoIndex;
      }
  
      protected void setBIndex(final int bIndex) {
          this.bIndex = bIndex;
      }
  
      @Column(name = "priority")
      public int getBPriority() {
          return bpriority;
      }
  
      public void setBPriority(final int bpriority) {
          this.bpriority = bpriority;
      }
  
      @ManyToOne
      @JoinColumn(name = "Id")
      public A getAName() {
          return aName;
      }
  
      public void setAName(final A aName) {
          this.aName = aName;
      }
  }
  </code></pre>
  
  <p>after all the comments i have implemented the following code.  but again it gives OUT OF MEMORY. Should i have to flush the memory explicitly and how?  </p>
  
  <pre><code>public List&lt;B&gt; getList(String name, int offset, int limit) throws DAOException {
          try {
              String hql = "from B where name = :name";
              begin();
              Query query = getSession().createQuery(hql);
              query.setString("name", name);
  
              if(offset &gt; 0){
                  query.setFirstResult(offset);
              }
  
              if(limit &gt; 0){
                  query.setMaxResults(limit);
                  query.setFetchSize(limit);
              }
              commit();
              return query.list();
          } catch (HibernateException e) {
              rollback(); 
          }
      }
  
      public Long countB(String name) throws DAOException {
          try {
              String hql = "select count(*) from B where name = :name";
              begin();
              Query query = getSession().createQuery(hql);
              query.setString("name", name);
              commit();
              return (Long)query.uniqueResult();
          } catch (HibernateException e) {
              rollback();
          }
      }
  
  
      long count = countB(name);
      int counter = (int) (count / 200);
      if(count%200 &gt; 0){
          counter++;
      }
      for(int j = 0;j&lt;counter;j++){
          lists = getList(name, j*200, 200);
  
          for(B count1 : lists){
              System.out.println(count1);
          }
      }
  </code></pre>