title: EntityManagerHolder cannot be cast to org.springframework.orm.hibernate5.SessionHolder
body: <p>I'm using this code to configure Spring with Hibernate:</p>
  
  <pre><code>@SpringBootApplication
  @Configuration
  @EnableTransactionManagement
  public class ContextServer {
  
      @Bean
      public LocalSessionFactoryBean getSessionFactory() {
          LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
  
          try {
              sessionFactory.setDataSource(dataSource());
          } catch (NamingException e) {
              e.printStackTrace();
          }
          sessionFactory.setPackagesToScan(new String[] { "org.plugin.database.models" });
          sessionFactory.setHibernateProperties(hibernateProperties());
          // factoryBean.setAnnotatedClasses(User.class, Authorities.class);
  
          return sessionFactory;
      }
  
      @Bean
      public DataSource dataSource() throws NamingException {
          return (DataSource) new JndiTemplate().lookup("java:/global/production_gateway");
      }
  
      private final Properties hibernateProperties() {
          final Properties hibernateProperties = new Properties();
          hibernateProperties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
          hibernateProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.MariaDBDialect");
          hibernateProperties.setProperty("hibernate.show_sql", "true");
          hibernateProperties.setProperty("hibernate.format_sql", "true");
  
          return hibernateProperties;
      }
  
      @Bean
      public HibernateTransactionManager getTransactionManager() {
          HibernateTransactionManager transactionManager = new HibernateTransactionManager();
          transactionManager.setSessionFactory(getSessionFactory().getObject());
          return transactionManager;
      }
  }
  </code></pre>
  
  <p>I use the factory this way:</p>
  
  <pre><code>@Component("authorize")
  @Transactional
  public class AuthorizeService implements MessageProcessor {
  
      @Autowired
      SessionFactory sessionFactory;
  
  
      @Override
      public void processMessage(.....) {     
  
          Session session = sessionFactory.getCurrentSession();
      }
  </code></pre>
  
  <p>But I get exception:</p>
  
  <pre><code>java.lang.ClassCastException: 
  org.springframework.orm.jpa.EntityManagerHolder cannot be cast to 
  org.springframework.orm.hibernate5.SessionHolder
  </code></pre>
  
  <p>I found these answers:
  <a href="https://stackoverflow.com/questions/42476261/classcastexception-org-springframework-orm-jpa-entitymanagerholder-cannot-be-ca">ClassCastException: org.springframework.orm.jpa.EntityManagerHolder cannot be cast to org.springframework.orm.hibernate5.SessionHolder</a></p>
  
  <p><a href="https://stackoverflow.com/questions/38627491/spring-4-hibernate-5-org-springframework-orm-jpa-entitymanagerholder-cannot">Spring 4 + Hibernate 5 = org.springframework.orm.jpa.EntityManagerHolder cannot be cast to org.springframework.orm.hibernate5.SessionHolder</a></p>
  
  <p>Is there any other solution that I can use? Above solutions are quick hacks.</p>