We would like to use Hibernate StatelessSession injected into our Spring Data JPA repositories. Our understanding is this requires HibernateTransactionManager and SharedSessionCreator.createSharedStatelessSession. Our setup is like follows:
@Bean
public HibernateTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
var hibernateTransactionManager = new HibernateTransactionManager(entityManagerFactory.unwrap(SessionFactory.class));
return hibernateTransactionManager;
}
@Bean
public StatelessSession statelessSession(EntityManagerFactory entityManagerFactory) {
SessionFactory sessionFactory = entityManagerFactory.unwrap(SessionFactory.class);
return SharedSessionCreator.createSharedStatelessSession(sessionFactory);
}
An example is here.
We are using spring.jpa.open-in-view as it's the Spring Boot default and some users have crept in. We are therefore getting the following exception
java.lang.ClassCastException: class org.springframework.orm.jpa.EntityManagerHolder caot be cast to class org.springframework.orm.jpa.hibernate.SessionHolder (org.springframework.orm.jpa.EntityManagerHolder and org.springframework.orm.jpa.hibernate.SessionHolder are in uamed module of loader 'app')
at org.springframework.orm.jpa.hibernate.HibernateTransactionManager.doGetTransaction(HibernateTransactionManager.java:395) ~[spring-orm-7.0.9.jar:7.0.9]
at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:379) ~[spring-tx-7.0.9.jar:7.0.9]
at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:628) ~[spring-tx-7.0.9.jar:7.0.9]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:365) ~[spring-tx-7.0.9.jar:7.0.9]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:130) ~[spring-tx-7.0.9.jar:7.0.9]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-7.0.9.jar:7.0.9]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:719) ~[spring-aop-7.0.9.jar:7.0.9]
at com.acme.stateless.session.demo.CityService$$SpringCGLIB$$0.existsCity42(<generated>) ~[classes/:na]
at com.acme.stateless.session.demo.CityController.exists(CityController.java:20) ~[classes/:na]
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:580) ~[na:na]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:252) ~[spring-web-7.0.9.jar:7.0.9]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:184) ~[spring-web-7.0.9.jar:7.0.9]
at org.springframework.web.servlet.mvc.method.aotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117) ~[spring-webmvc-7.0.9.jar:7.0.9]
at org.springframework.web.servlet.mvc.method.aotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:934) ~[spring-webmvc-7.0.9.jar:7.0.9]
at org.springframework.web.servlet.mvc.method.aotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:853) ~[spring-webmvc-7.0.9.jar:7.0.9]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:86) ~[spring-webmvc-7.0.9.jar:7.0.9]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963) ~[spring-webmvc-7.0.9.jar:7.0.9]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:866) ~[spring-webmvc-7.0.9.jar:7.0.9]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1000) ~[spring-webmvc-7.0.9.jar:7.0.9]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:892) ~[spring-webmvc-7.0.9.jar:7.0.9]
Are there any examples on how to use Hibernate StatelessSesion with spring.jpa.open-in-view? Simply calling HibernateTransactionManager.setHibernateManagedSession(true) does not fix the issue.
Spring Boot version: 4.1.0
برچسب:
نویسنده: استخدام کار