title: How does getCurrentSession work without openSession?
body: <p>This is my code:</p>
  
  <p><strong>hibernate.cfg.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;
  
  &lt;hibernate-configuration&gt;
      &lt;session-factory&gt;
          &lt;property name="connection.driver_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt;
          &lt;property name="connection.url"&gt;jdbc:mysql://localhost:3306/sakila&lt;/property&gt;
          &lt;property name="connection.username"&gt;root&lt;/property&gt;
          &lt;property name="connection.password"/&gt;
  
          &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;
  
          &lt;property name="current_session_context_class"&gt;thread&lt;/property&gt;
          &lt;property name="hibernate.show_sql"&gt;false&lt;/property&gt;
  
          &lt;mapping class="biz.tugay.saqila.model.Actor" /&gt;
      &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p><strong>HibernateUtil.java</strong></p>
  
  <pre><code>package biz.tugay.saqila.dao;
  /* User: koray@tugay.biz Date: 06/08/15 Time: 18:29 */
  
  import org.hibernate.Session;
  import org.hibernate.SessionFactory;
  import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
  import org.hibernate.cfg.Configuration;
  import org.hibernate.service.ServiceRegistry;
  
  public class HibernateUtil {
  
      private static SessionFactory SESSION_FACTORY;
  
      public static void buildSessionFactory() {
          if (SESSION_FACTORY != null) {
              return;
          }
  
          Configuration configuration = new Configuration();
          configuration.configure();
          ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                  .applySettings(configuration.getProperties()).build();
          SESSION_FACTORY = configuration.buildSessionFactory(serviceRegistry);
  
      }
  
      public static Session getCurrentSession() {
          return SESSION_FACTORY.getCurrentSession();
      }
  
      public static void killSessionFactory() {
          if (SESSION_FACTORY != null) {
              SESSION_FACTORY.close();
          }
      }
  
  }
  </code></pre>
  
  <p>and a sample DAO class:</p>
  
  <pre><code>package biz.tugay.saqila.dao;
  /* User: koray@tugay.biz Date: 06/08/15 Time: 18:37 */
  
  import biz.tugay.saqila.model.Actor;
  import org.hibernate.Session;
  
  import java.util.List;
  
  @SuppressWarnings("unchecked")
  public class ActorDao {
  
      public List&lt;Actor&gt; getAllActors() {
          Session session = HibernateUtil.getCurrentSession();
          session.beginTransaction();
          List&lt;Actor&gt; actors = session.createQuery("FROM Actor").list();
          session.close();
          return actors;
      }
  
      public Actor getWithId(int id) {
          Session session = HibernateUtil.getCurrentSession();
          session.beginTransaction();
          Actor actor = (Actor) session.get(Actor.class, id);
          session.close();
          return actor;
      }
  
  }
  </code></pre>
  
  <p>Well as you can see I am not calling <strong>openSession</strong> anywhere, just <strong>getCurrentSession</strong>. But how does this work? </p>
  
  <p>Also, is this the right way to do things or am I just lucky that this works?</p>
  
  <p>Btw I also have this listener:</p>
  
  <pre><code>package biz.tugay.saqila.servlet;
  /* User: koray@tugay.biz Date: 06/08/15 Time: 19:00 */
  
  
  import biz.tugay.saqila.dao.HibernateUtil;
  
  import javax.servlet.ServletContextEvent;
  import javax.servlet.ServletContextListener;
  import javax.servlet.annotation.WebListener;
  
  @WebListener
  public class HibernateConfigurator implements ServletContextListener {
  
      @Override
      public void contextInitialized(ServletContextEvent servletContextEvent) {
          HibernateUtil.buildSessionFactory();
      }
  
      @Override
      public void contextDestroyed(ServletContextEvent servletContextEvent) {
          System.out.println("Killing Session Factory.");
          HibernateUtil.killSessionFactory();
      }
  
  }
  </code></pre>