title: Cannot get session using SessionFactory.getCurrentSession() in Hibernate5 with Spring
body: <p>I am completely new to Spring and Hibernate. I am having some difficulty getting Spring and Hibernate to cooperate so that my Spring configuration returns a Session object from the SessionFactory object.</p>
  
  <p>In particular, I have followed the tutorial at: <a href="https://www.baeldung.com/hibernate-5-spring" rel="nofollow noreferrer">https://www.baeldung.com/hibernate-5-spring</a>. This tutorial is for configuring Spring and Hibernate for the H2 database. However, I am currently configuring Spring and Hibernate for Oracle 12.2. I have reflected the changes in the configuration files.</p>
  
  <p>Just like the aforementioned site suggests, I have written a Maven project with all the required dependencies. Moreover, I am  configuring Hibernate 5 with an XML-based configuration.</p>
  
  <p>Please find the code below:</p>
  
  <p>Here is the Spring - Hibernate configuration file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"&gt;
  
  &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource"/&gt;
      &lt;property name="packagesToScan" value="com.test"&gt;
      &lt;property name="hibernateProperties"&gt;
          &lt;props&gt;
              &lt;prop key="hibernate.hbm2ddl.auto"&gt;create&lt;/prop&gt;
              &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.Oracle10gDialect&lt;/prop&gt;
              &lt;prop key="debug"&gt;true&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp2.BasicDataSource"&gt;
      &lt;property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/&gt;
      &lt;property name="url" value="jdbc:oracle:thin:@localhost:1521:ORCL"/&gt;
      &lt;property name="username" value="sa"/&gt;
      &lt;property name="password" value="sa"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory" ref="sessionFactory"/&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p></p>
  
  <p>Here is the Java code that I am using to try and secure the Hibernate Session object:</p>
  
  <pre><code>@Autowired
  private SessionFactory sessionFactory;
  public Session getSession()
  {
      Session session = null;
      try
      {
          session = sessionFactory.getCurrentSession();
          if(session != null)
          {
              System.out.println("Session is VALID.");
          }
          else
          {
              System.out.println("Session is INVALID.");
          }
      }
      catch(IllegalStateException e)
      {
          System.out.println("getSession: Illegal State Exception: " + e.getMessage());
      }
      catch(Exception e)
      {
          System.out.println("getSession: Exception: " + e.getMessage());
      }
      return session;
  }
  </code></pre>
  
  <p>Here is the project pom.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt; &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt; 
  
  &lt;groupId&gt;com.test&lt;/groupId&gt;
  &lt;artifactId&gt;Flow&lt;/artifactId&gt;
  &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
  &lt;name&gt;Flow&lt;/name&gt;
  &lt;url&gt;http://www.example.com&lt;/url&gt;
  
  &lt;properties&gt;
      &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
      &lt;org.springframework.version&gt;5.0.2.RELEASE&lt;/org.springframework.version&gt;
      &lt;org.springframework.data.version&gt;1.10.6.RELEASE&lt;/org.springframework.data.version&gt;
      &lt;org.springframework.security.version&gt;4.2.1.RELEASE&lt;/org.springframework.security.version&gt;
      &lt;hibernate.version&gt;5.2.10.Final&lt;/hibernate.version&gt;
      &lt;hibernatesearch.version&gt;5.8.2.Final&lt;/hibernatesearch.version&gt;
      &lt;tomcat-dbcp.version&gt;9.0.0.M26&lt;/tomcat-dbcp.version&gt;
      &lt;jta.version&gt;1.1&lt;/jta.version&gt;
      &lt;hsqldb.version&gt;2.3.4&lt;/hsqldb.version&gt;
      &lt;oracle.version&gt;12.2.0.1&lt;/oracle.version&gt;
      &lt;commons-lang3.version&gt;3.5&lt;/commons-lang3.version&gt;
  &lt;/properties&gt;
  
  &lt;repositories&gt;
    &lt;repository&gt;
      &lt;id&gt;maven.oracle.com&lt;/id&gt;
      &lt;releases&gt;
        &lt;enabled&gt;true&lt;/enabled&gt;
      &lt;/releases&gt;
      &lt;snapshots&gt;
        &lt;enabled&gt;false&lt;/enabled&gt;
      &lt;/snapshots&gt;
      &lt;url&gt;https://maven.oracle.com&lt;/url&gt;
      &lt;layout&gt;default&lt;/layout&gt;
    &lt;/repository&gt;
  &lt;/repositories&gt;
  
  &lt;pluginRepositories&gt;
      &lt;pluginRepository&gt;
          &lt;id&gt;maven.oracle.com&lt;/id&gt;
          &lt;url&gt;https://maven.oracle.com&lt;/url&gt;
      &lt;/pluginRepository&gt;
  &lt;/pluginRepositories&gt;
  
  &lt;dependencies&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework.version}&lt;/version&gt;
          &lt;exclusions&gt;
              &lt;exclusion&gt;
                  &lt;artifactId&gt;commons-logging&lt;/artifactId&gt;
                  &lt;groupId&gt;commons-logging&lt;/groupId&gt;
              &lt;/exclusion&gt;
          &lt;/exclusions&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-aspects&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- persistence --&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-orm&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework.version}&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.data&lt;/groupId&gt;
          &lt;artifactId&gt;spring-data-jpa&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework.data.version}&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
          &lt;version&gt;${hibernate.version}&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;javax.transaction&lt;/groupId&gt;
          &lt;artifactId&gt;jta&lt;/artifactId&gt;
          &lt;version&gt;${jta.version}&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;
          &lt;artifactId&gt;tomcat-dbcp&lt;/artifactId&gt;
          &lt;version&gt;${tomcat-dbcp.version}&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.apache.commons&lt;/groupId&gt;
          &lt;artifactId&gt;commons-lang3&lt;/artifactId&gt;
          &lt;version&gt;${commons-lang3.version}&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;junit&lt;/groupId&gt;
          &lt;artifactId&gt;junit&lt;/artifactId&gt;
          &lt;version&gt;4.11&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;com.oracle.jdbc&lt;/groupId&gt;
          &lt;artifactId&gt;ojdbc8&lt;/artifactId&gt;
          &lt;version&gt;${oracle.version}&lt;/version&gt;
      &lt;/dependency&gt;   
  &lt;/dependencies&gt;
  
  &lt;build&gt;
      &lt;finalName&gt;Flows&lt;/finalName&gt;
      &lt;resources&gt;
          &lt;resource&gt;
              &lt;directory&gt;src/main/resources&lt;/directory&gt;
              &lt;filtering&gt;true&lt;/filtering&gt;
          &lt;/resource&gt;
      &lt;/resources&gt;
      &lt;pluginManagement&gt;&lt;!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --&gt;
          &lt;plugins&gt;
              &lt;!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle --&gt;
              &lt;plugin&gt;
                  &lt;artifactId&gt;maven-clean-plugin&lt;/artifactId&gt;
                  &lt;version&gt;3.1.0&lt;/version&gt;
              &lt;/plugin&gt;
              &lt;!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --&gt;
              &lt;plugin&gt;
                  &lt;artifactId&gt;maven-resources-plugin&lt;/artifactId&gt;
                  &lt;version&gt;3.0.2&lt;/version&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                  &lt;version&gt;3.8.0&lt;/version&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
                  &lt;version&gt;2.22.1&lt;/version&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt;
                  &lt;version&gt;3.0.2&lt;/version&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;artifactId&gt;maven-install-plugin&lt;/artifactId&gt;
                  &lt;version&gt;2.5.2&lt;/version&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;artifactId&gt;maven-deploy-plugin&lt;/artifactId&gt;
                  &lt;version&gt;2.8.2&lt;/version&gt;
              &lt;/plugin&gt;
              &lt;!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle --&gt;
              &lt;plugin&gt;
                  &lt;artifactId&gt;maven-site-plugin&lt;/artifactId&gt;
                  &lt;version&gt;3.7.1&lt;/version&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;artifactId&gt;maven-project-info-reports-plugin&lt;/artifactId&gt;
                  &lt;version&gt;3.0.0&lt;/version&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/pluginManagement&gt;
  &lt;/build&gt;
  </code></pre>
  
  <p></p>
  
  <p>What I am expecting to receive is a Session object; however, when I run my code, the variable <code>session</code> i returns null. Any help would be greatly appreciated. Thanks!</p>