title: Hibernate - Session closed after getting query
body: <p>I have a central class for all Hibernate logic (which I didn't write most of). Let's call it <code>HibCentral</code>.</p>
  
  <p>My code uses this class to create a query from a SQL string:</p>
  
  <pre><code>public class HibCentral {
  
      ...some hibernate setup and other methods...
  
      @Transactional
      public Query createSQLQuery(final String sql) {
          return getCurrentSession().createSQLQuery(sql);
      }
  }
  </code></pre>
  
  <p><code>getCurrentSession()</code> just calls <code>sessionFactory.getCurrentSession()</code>.</p>
  
  <p>My class (let's call it <code>MyClass</code>) is called by a high level service (<code>Service</code>). It gets a SQL string, calls this method to get a query, populates its parameters, and sends it back to the service. The service then calls the query using <code>query.list()</code>.</p>
  
  <pre><code>public class MyClass {
      public Query getQuery(Args args) {
          Query q = hibCentral.createSQLQuery(getSQLString());
          setParameters(q);
          return q;
      }
  }
  
  public class Service {
      public void doIt() {
          Query q = myClass.getQuery(args);
          // some logic
          List list = q.list();
      }
  }
  </code></pre>
  
  <p>But wait, the session is closed.</p>
  
  <p>I tried to move the <code>list()</code> call to the Hibernate-logic class as well:</p>
  
  <pre><code>public class HibCentral {
  
      ....other methods....
  
      @Transactional
      public List list(final Query query) {
          return query.list();
      }
  }
  </code></pre>
  
  <p>but this produced the same "the session is closed" error.</p>
  
  <p>What do I do to make this work?</p>