title: Configure Spring with JNDI and Hibernate
body: <p>I tried to use this configuration in order to setup Spring with Hibernate 5 and JNDI. But during startup I get errors.</p>
  
  <pre><code>@Configuration
  @EnableTransactionManagement
  public class ContextDatasource {
  
      @Bean
      public LocalSessionFactoryBean sessionFactory() throws NamingException {
          LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
          sessionFactory.setDataSource(dataSource());
          sessionFactory.setPackagesToScan("org.database.models");
          sessionFactory.setHibernateProperties(hibernateProperties());
  
          return sessionFactory;
      }
  
      @Bean
      public PlatformTransactionManager hibernateTransactionManager() throws NamingException {
          HibernateTransactionManager transactionManager
            = new HibernateTransactionManager();
          transactionManager.setSessionFactory(sessionFactory().getObject());
          return transactionManager;
      }
  
      private final Properties hibernateProperties() {
          Properties hibernateProperties = new Properties();
          hibernateProperties.setProperty(
            "hibernate.hbm2ddl.auto", "create-drop");
          hibernateProperties.setProperty(
            "hibernate.dialect", "org.hibernate.dialect.H2Dialect");
  
          return hibernateProperties;
      }
  
      @Bean
      public LocalContainerEntityManagerFactoryBean entityManagerFactory() 
        throws NamingException {
          LocalContainerEntityManagerFactoryBean em 
            = new LocalContainerEntityManagerFactoryBean();
          em.setDataSource(dataSource());
          return em;
      }
  
      @Bean
      public DataSource dataSource() throws NamingException {
          return (DataSource) new JndiTemplate().lookup("java:comp/env/jdbc/production_gateway");
      }
  
      @Bean
      public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
          JpaTransactionManager transactionManager = new JpaTransactionManager();
          transactionManager.setEntityManagerFactory(emf);
          return transactionManager;
      }
  
  }
  </code></pre>
  
  <p>Configured POM dependencies:</p>
  
  <pre><code>        &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
              &lt;version&gt;5.2.14.Final&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-orm&lt;/artifactId&gt;
              &lt;version&gt;5.0.7.RELEASE&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-entitymanager&lt;/artifactId&gt;
              &lt;version&gt;5.3.1.Final&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-validator&lt;/artifactId&gt;
              &lt;version&gt;5.3.6.Final&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.eclipse.persistence&lt;/groupId&gt;
              &lt;artifactId&gt;javax.persistence&lt;/artifactId&gt;
              &lt;version&gt;2.0.0&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-jdbc&lt;/artifactId&gt;
              &lt;version&gt;2.0.3.RELEASE&lt;/version&gt;
          &lt;/dependency&gt;
  </code></pre>
  
  <p>But when I run the code I get error during startup:</p>
  
  <pre><code> Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is javax.naming.NameNotFoundException:
  </code></pre>
  
  <p>What is the proper way to configure JNDI? Probably I need to remove some of the Java methods? I want to use Hibernate 5.</p>