title: Main Hibernate Session Closing automatically
body: <p>I have a parent and child entities as below</p>
  
  <pre><code>@Entity
  @Table(name = "PARENT")
  public class Parent implements Comparable&lt;Parent&gt;, Serializable, Cloneable {
  
      private static final long serialVersionUID = 1584115734363172986L;
  
      public static final int NAME_LENGTH = 300;
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO, generator = "gen-seq")
      @GenericGenerator(name = "gen-seq", strategy = "native",
          parameters = {@Parameter(name = "sequence_name", value = "parent_id_seq")})
      private Long id;
  
      @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)
      @BatchSize(size = 50)
      @SortNatural
      private SortedSet&lt;Child&gt; child = new TreeSet&lt;&gt;();
  }
  </code></pre>
  
  <p>Child entity as below</p>
  
  <pre><code>@Entity
  @Table(name = "Child")
  @Inheritance(strategy= InheritanceType.JOINED)
  public class Child implements Comparable&lt;Object&gt;, Serializable, Cloneable, Step&lt;Child&gt; {
  
      private static final long serialVersionUID = -9091312680432977997L;
  
      public static final int NAME_LENGTH = 255;
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO, generator = "gen-seq")
      @GenericGenerator(name = "gen-seq", strategy = "native",
          parameters = {@Parameter(name = "sequence_name", value = "child_id_seq")})
      private Long id;
  
      @ManyToOne()
      @JoinColumn(name = "parentId", nullable = false)
      private Parent parent;
  }
  </code></pre>
  
  <p>I have configured Spring and Hibernate to process database operations, and to process Parent I have the following method added in the serviceimpl class.</p>
  
  <pre><code>public void processParent(final Long parentId, String userEmail) throws Exception {
  getHibernateTemplate().execute(new HibernateCallback&lt;Set&lt;Void&gt;&gt;() {
      public void doInHibernate(Session session) throws HibernateException    {
          Parent parent = (Parent) session.get(Parent.class, parentId);           
          if (parent!=null &amp;&amp; parent.getChild()!=null) {
              for (Child child: parent.getChild()) {
  
                  // here the session is still Open when I call
                  child.getParent();
  
                  //But when I call this
                  Parent p = getParent(child.getParent().getId());
  
                  //now here the session got closed automatically, and now I cannot process the parent object
  
                  }
              }
          }
      }
  }}
  </code></pre>
  
  <p>In the above method, I have the session is opened until I call child.getParent() but if I call the below method which has another session itself which is not null, after returning the below method, then the session in above method is becoming null automatically.</p>
  
  <pre><code>public Parent getParent(final Long id) throws Exception {
  Parent actionPoint = (Parent) getHibernateTemplate().execute(new HibernateCallback&lt;Parent&gt;() {
  
      /* (non-Javadoc)
      * @see org.springframework.orm.hibernate5.HibernateCallback#doInHibernate(org.hibernate.Session)
      */
      public Parent doInHibernate(Session session) throws HibernateException {
          Parent parent = (ActionPoint) session.get(Parent.class, id);
  
          //here also session is still open
          return parent;
      }
  }}
  </code></pre>
  
  <p>Can anyone help me how to fix the main session should not be closed or become null. I know this is a configuration problem, but I don't know what should be configured. </p>
  
  <p>Thanks in Advance, as I am trying to figure it out this issue for past 2 weeks.</p>