title: Spring 4 + Hibernate 5 = org.springframework.orm.jpa.EntityManagerHolder cannot be cast to org.springframework.orm.hibernate5.SessionHolder
body: <p>OK, I'm a complete beginner when it comes to start a new java project and integrate tools like spring/hibernate, etc. In fact, this is the first time I'm doing it. So I'm sure the error will be obvious for you guys.</p>
  
  <p><strong>Guessings:</strong></p>
  
  <ul>
  <li>The session factory I expected is not the one being injected by spring.</li>
  <li>Wrong dependencies.</li>
  </ul>
  
  <p><strong><em>Error</em></strong></p>
  
  <pre><code>[2016-07-28 01:29:14.869] boot - 22234 ERROR [http-nio-8080-exec-1] --- [dispatcherServlet]: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.ClassCastException: org.springframework.orm.jpa.EntityManagerHolder cannot be cast to org.springframework.orm.hibernate5.SessionHolder] with root cause
      java.lang.ClassCastException: org.springframework.orm.jpa.EntityManagerHolder cannot be cast to org.springframework.orm.hibernate5.SessionHolder
          at org.springframework.orm.hibernate5.HibernateTransactionManager.doGetTransaction(HibernateTransactionManager.java:376)
  </code></pre>
  
  <p><strong><em>pom.xml</em></strong></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;org.pse&lt;/groupId&gt;
      &lt;artifactId&gt;plataforma-ejercicios&lt;/artifactId&gt;
      &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
  
      &lt;properties&gt;
          &lt;java.version&gt;1.8&lt;/java.version&gt;
          &lt;org.springframework.version&gt;4.3.1.RELEASE&lt;/org.springframework.version&gt;
          &lt;spring-boot-starter.version&gt;1.3.5.RELEASE&lt;/spring-boot-starter.version&gt;
          &lt;org.hibernate.version&gt;5.2.0.Final&lt;/org.hibernate.version&gt;
          &lt;mysql-connector-java.version&gt;5.1.38&lt;/mysql-connector-java.version&gt;
          &lt;c3p0.version&gt;0.9.1.2&lt;/c3p0.version&gt;
      &lt;/properties&gt;
  
  
      &lt;dependencies&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;c3p0&lt;/groupId&gt;
              &lt;artifactId&gt;c3p0&lt;/artifactId&gt;
              &lt;version&gt;${c3p0.version}&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;${org.springframework.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- Hibernate --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
              &lt;version&gt;5.2.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.1.3.Final&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate.common&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-commons-annotations&lt;/artifactId&gt;
              &lt;version&gt;5.0.1.Final&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate.javax.persistence&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-jpa-2.0-api&lt;/artifactId&gt;
              &lt;version&gt;1.0.1.Final&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.1.0.Final&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- Maria --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.mariadb.jdbc&lt;/groupId&gt;
              &lt;artifactId&gt;mariadb-java-client&lt;/artifactId&gt;
              &lt;version&gt;1.1.7&lt;/version&gt;
          &lt;/dependency&gt;
  
  
          &lt;!-- Spring framework --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-core&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;!-- Spring boot --&gt;
           ....
             ...
      &lt;/dependencies&gt;
  
  &lt;/project&gt;
  </code></pre>
  
  <p><strong><em>base-context.xml</em></strong></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:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.3.xsd"&gt;
  
      &lt;import resource="classpath:hibernate-context.xml" /&gt;
      &lt;context:property-placeholder location="classpath:hibernate.properties" /&gt;
  
      &lt;bean id="studentDAO" class="org.pse.plataformaejercicios.dao.StudentDAO"&gt;
          &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
      &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong><em>hibernate-context.xml</em></strong></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:tx="http://www.springframework.org/schema/tx"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"&gt;
  
      &lt;bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"&gt;
          &lt;property name="driverClass" value="${hibernate.connection.driver_class}" /&gt;
          &lt;property name="jdbcUrl" value="${hibernate.connection.url}" /&gt;
          &lt;property name="user" value="${hibernate.connection.username}" /&gt;
          &lt;property name="password" value="${hibernate.connection.password}" /&gt;
          &lt;property name="minPoolSize" value="${hibernate.connection.min_pool_size}" /&gt;
          &lt;property name="maxPoolSize" value="${hibernate.connection.max_pool_size}" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="sessionFactory"
          class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"&gt;
          &lt;property name="dataSource" ref="c3p0DataSource" /&gt;
          &lt;property name="hibernateProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
          &lt;property name="packagesToScan" value="org.pse.plataformaejercicios.model" /&gt;
  
      &lt;/bean&gt;
  
      &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;
      &lt;bean id="transactionManager"
          class="org.springframework.orm.hibernate5.HibernateTransactionManager"&gt;
          &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
      &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong><em>hibernate.properties</em></strong></p>
  
  <pre><code>hibernate.dialect org.hibernate.dialect.MySQL5Dialect
  hibernate.connection.driver_class org.mariadb.jdbc.Driver
  hibernate.connection.url jdbc:mariadb://127.0.0.1:3306/pse
  hibernate.connection.username pse
  hibernate.connection.password passpse
  hibernate.connection.min_pool_size 2
  hibernate.connection.max_pool_size 10
  hibernate.connection.autocommit true
  hibernate.hbm2ddl.auto update
  hibernate.show_sql true
  </code></pre>
  
  <p><strong><em>StudentController.java</em></strong></p>
  
  <pre><code>@RestController
  public class StudentController {
  
      private Logger logger = LoggerFactory.getLogger(StudentController.class);
  
      private StudentService studentService;
  
      @RequestMapping(value = "/student/{studentId}/classes", method = RequestMethod.GET)
      public ClassDTO getClassesForStudent(){
          return new ClassDTO();
      }
  
      @RequestMapping("/saveStudent/{nombre}/{apellido}")
      public void saveStudent(@PathVariable String nombre, @PathVariable String apellido) {
          logger.info("Entrada al controller de: TestHibernate");
  
          Student student = new Student();
          student.setLastName(apellido);
          student.setName(nombre);
  
          studentService.save(student);
      }
  
  
      @Autowired
      public void setStudentService(StudentService studentService) {
          this.studentService = studentService;
      }
  
  }
  </code></pre>
  
  <p><strong><em>StudentServiceImpl.java</em></strong></p>
  
  <pre><code>@Service
  public class StudentServiceImpl implements StudentService {
  
      private StudentDAO studentDAO;
  
      @Override
      @Transactional(readOnly = false)
      public void save(Student student) {
          studentDAO.save(student);
      }
  
      @Autowired
      public void setStudentDAO(StudentDAO studentDAO) {
          this.studentDAO = studentDAO;
      }
  }
  </code></pre>
  
  <p><strong><em>StudentDAO.java</em></strong></p>
  
  <pre><code>public class StudentDAO {
  
      private SessionFactory sessionFactory;
  
      public void save(Student student){
          try {
              Session session = sessionFactory.getCurrentSession();
              session.save(student);
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
      @Autowired
      public void setSessionFactory(SessionFactory sessionFactory) {
          this.sessionFactory = sessionFactory;
      }
  
  }
  </code></pre>