title: Spring boot hibernate 5 integration error
body: <p>I am trying to integrate spring Boot with Hibernate 5 using Java configs.
  It has been some time since I have used Hibernate.
  I am getting the below mentioned exception. Can someone please suggest how to resolve this ?</p>
  
  <p>Following is my java config file followed by the exception stack trace.</p>
  
  <pre><code>package com.example;
  
  import com.example.dao.CustomerDao;
  import com.example.model.Customer;
  import org.apache.commons.dbcp2.BasicDataSource;
  import org.springframework.boot.CommandLineRunner;
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.context.annotation.Bean;
  import org.springframework.orm.hibernate5.HibernateTemplate;
  import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
  import org.springframework.transaction.annotation.EnableTransactionManagement;
  
  import javax.sql.DataSource;
  import java.util.Properties;
  
  @SpringBootApplication
  @EnableTransactionManagement
  public class SpringHibernateDemoApplication implements CommandLineRunner {
  
      public static void main(String[] args) {
  
          SpringApplication.run(SpringHibernateDemoApplication.class, args);
      }
  
      @Bean
      public DataSource dataSource() throws ClassNotFoundException {
          //SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
          // dataSource.setDriverClass((Class&lt;? extends Driver&gt;)Class.forName("com.mysql.jdbc.Driver"));
          BasicDataSource dataSource = new BasicDataSource();
          dataSource.setDriverClassName("com.mysql.jdbc.Driver");
          dataSource.setUrl("jdbc:mysql://localhost:3306/customerDB");
          dataSource.setUsername("root");
          dataSource.setPassword("welcome");
          return  dataSource;
      }
  
      @Bean
      public LocalSessionFactoryBean  sessionFactory() throws ClassNotFoundException {
          Properties properties = new Properties();
          properties.put("hibernate.dialect","org.hibernate.dialect.MySQLDialect");
          properties.put("hibernate.show_sql", "true");
          properties.put("hibernate.hbm2ddl.auto","create");
         // properties.put("hibernate.connection.provider_class","org.hibernate.connection.DatasourceConnectionProvider");
          LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();
          localSessionFactoryBean.setDataSource(dataSource());
          localSessionFactoryBean.setPackagesToScan("com.example.model");
          localSessionFactoryBean.setHibernateProperties(properties);
          /*SessionFactory sessionFactory = new LocalSessionFactoryBuilder(dataSource()).
                  addAnnotatedClass(Customer.class).setProperties(properties).buildSessionFactory();*/
          return localSessionFactoryBean;
      }
  
      @Bean(name = "hibernateTemplate")
      public HibernateTemplate hibernateTemplate() throws ClassNotFoundException {
  
          return new HibernateTemplate(sessionFactory().getObject());
      }
  
      @Bean
      public CustomerDao customerDao() {
          return  new CustomerDao();
      }
  
      @Override
      public void run(String... args) throws Exception {
          Customer customer = new Customer();
          customer.setFistName("amar");
          customer.setLastName("dev");
          customer.setEmail("amar@gmail.com");
          customerDao().saveCustomer(customer);
      }
  }
  </code></pre>
  
  <p>The exception.</p>
  
  <pre><code>java.lang.IllegalStateException: Failed to execute CommandLineRunner
      at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:809) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
      at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:790) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
      at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:777) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
      at com.example.SpringHibernateDemoApplication.main(SpringHibernateDemoApplication.java:23) [main/:na]
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_74]
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_74]
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_74]
      at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_74]
      at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) [idea_rt.jar:na]
  Caused by: java.lang.IllegalStateException: Already value [org.springframework.orm.jpa.EntityManagerHolder@10753c9] for key [org.hibernate.internal.SessionFactoryImpl@ba5d30] bound to thread [main]
      at org.springframework.transaction.support.TransactionSynchronizationManager.bindResource(TransactionSynchronizationManager.java:190) ~[spring-tx-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.springframework.orm.hibernate5.SpringSessionContext.currentSession(SpringSessionContext.java:127) ~[spring-orm-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:454) ~[hibernate-core-5.2.0.Final.jar:5.2.0.Final]
      at org.springframework.orm.hibernate5.HibernateTemplate.doExecute(HibernateTemplate.java:326) ~[spring-orm-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.springframework.orm.hibernate5.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:309) ~[spring-orm-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.springframework.orm.hibernate5.HibernateTemplate.save(HibernateTemplate.java:616) ~[spring-orm-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at com.example.dao.CustomerDao.saveCustomer(CustomerDao.java:11) ~[main/:na]
      at com.example.dao.CustomerDao$$FastClassBySpringCGLIB$$fb200467.invoke(&lt;generated&gt;) ~[main/:na]
      at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:720) ~[spring-aop-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) ~[spring-tx-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281) ~[spring-tx-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) ~[spring-tx-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655) ~[spring-aop-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at com.example.dao.CustomerDao$$EnhancerBySpringCGLIB$$22cf791b.saveCustomer(&lt;generated&gt;) ~[main/:na]
      at com.example.SpringHibernateDemoApplication.run(SpringHibernateDemoApplication.java:71) [main/:na]
      at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:806) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
  </code></pre>