org.springframework.orm.jpa.EntityManagerHolder cannot be cast to org.springframework.orm.hibernate5.SessionHolder

I completed my tutorial on Spring Boot + Kotlin + AngularJS + WebJar CRUD application. And now i am going to implement Hibernate in that project. I was worked on Spring 4 and Hibernate 5 and found an anonymous error which reason of a mild headache for me.  But after the waste my valuable time for googling then I found a solution to that error.

The error was:

java.lang.ClassCastException: org.springframework.orm.jpa.EntityManagerHolder cannot be cast to org.springframework.orm.hibernate5.SessionHolder
at org.springframework.orm.hibernate5.HibernateTransactionManager.doGetTransaction( ~[spring-orm-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at ~[spring-tx-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary( ~[spring-tx-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction( ~[spring-tx-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke( ~[spring-tx-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed( ~[spring-aop-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke( ~[spring-aop-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at com.sun.proxy.$Proxy79.registerNewUserAccount(Unknown Source) ~[na:na]


With the release of Hibernate 5.2 the SessionFactory extends the EntityManagerFactoryinterface. This leads to the SessionFactory also being an EntityManagerFactory.

In previous hibernate releases this wasn’t the case.

The easy solution is to downgrade the hibernate version to a version < 5.2 as there is no solution for Spring 5.0 (there will be in Spring 5.1).

Or probably even better don’t use the plain SessionFactory and let Spring Boot autoconfigure the EntityManagerFactory (done by default if hibernate is detected) and use that instead of plain Hibernate.

The solution is:

I added a exclude = HibernateJpaAutoConfiguration.classproperty to @SpringBootApplicationannotation.

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

@SpringBootApplication(exclude = HibernateJpaAutoConfiguration.class)

public class DemoApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {, args);




By Tell Me How

It is a technology blog and admin has excellent experience in programming from 5+ year. You can contact us at

Share your thoughts

Leave a Reply

Loading Facebook Comments ...
Loading Disqus Comments ...