spring-framework: Spring EntityManager proxy is incompatible with Hibernate Search <5.11.6 (FullTextEntityManager instantiation fails with ClassCastException)
After upgrading from 2.3.5.RELEASE to 2.4.0 instantiating a FullTextEntityManager (hibernate search) from an injected EntityManager is not possible anymore. The same code worked in the past and is derived from the official documentation or baeldung.
Code to reproduce: https://github.com/knoobie/spring-2-4-entitymanager
How to check:
- run the test; test fails on service#load
- you can verify that it worked, if you just change the version to 2.3.5.RELEASE again and start the test (it doesn’t fail)
Stacktrace:
java.lang.ClassCastException: com.sun.proxy.$Proxy98 cannot be cast to org.hibernate.engine.spi.SessionImplementor
at org.hibernate.search.impl.FullTextSessionImpl.<init>(FullTextSessionImpl.java:62)
at org.hibernate.search.impl.ImplementationFactory.createFullTextSession(ImplementationFactory.java:35)
at org.hibernate.search.Search.getFullTextSession(Search.java:45)
at org.hibernate.search.jpa.Search.getFullTextEntityManager(Search.java:49)
at com.example.demo.search.MyService.load(MyService.java:51)
at com.example.demo.search.MyService$$FastClassBySpringCGLIB$$195495fd.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:771)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:371)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:134)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:691)
at com.example.demo.search.MyService$$EnhancerBySpringCGLIB$$9e31f121.load(<generated>)
at com.example.demo.DemoApplicationTests.contextLoads(DemoApplicationTests.java:20)
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 21 (9 by maintainers)
Commits related to this issue
- Expose plain Hibernate Session(Factory) interface by default again See gh-26090 — committed to spring-projects/spring-framework by jhoeller 6 years ago
@jhoeller Thanks for confirming this.
For the record, here are the relevant PRs in Hibernate Search:
SearchSessiontargeting aSessionproxy can safely be used from multiple threads, which isn’t the case withFullTextSessionin Hibernate Search 5.I’ll try to release 5.11 early next week.
I can confirm it as well - my application is running with Hibernate Search 5.11.6 and Spring Boot 2.4.0 without any issues. Thanks for your help!
FYI I had experienced this issue and just updated to Hibernate Search 5.11.6. Happy to report that Spring and Hibernate Search are once again happy with each other. Thanks to all who worked on identifying and fixing the issue!
Argh, another unguarded cast there!
EventSourceis even worse since it’s too internal forHibernateJpaVendorAdapterto use it as a default EntityManager interface. I’ll rather change our strategy then, reverting to plainSessionas default EntityManager interface but handlingunwrapcalls differently, leaning towards returning the underlying native instance more eagerly.Thanks for the quick turnaround, in any case! I’ll try the other approach right away, will ping you once it’s ready in a shapshot.
This is arguably a mismatch in Hibernate Search where it calls
EntityManager.unwrap(Session.class)but subsequently casts the result toSessionImplementor. It should really rather callEntityManager.unwrap(SessionImplementor.class)upfront if that’s what it ultimately expects.That said, I do see the point that previous
unwrap(Session.class)calls did result in the nativeSessionand could therefore be cast to the implementor interface at any point. Maybe we should simply useSessionImplementoras our EntityManager interface inHibernateJpaVendorAdapter…@wilkinsona thanks for you detailed analysis!
Thanks for the sample. I believe this change in behaviour is due to this change in Spring Framework 5.3.
The
entityManagerInterfacehas changed fromorg.hibernate.jpa.HibernateEntityManagertoorg.hibernate.Session. This has a knock-on effect when Hibernate Search attempts to unwrap theEntityManageras aSession:The
EntityManager(a Spring Framework created proxy) now implementsSessionso the proxy is returned as-is. The proxy does not implementSessionImplementorso the subsequent cast fails. In Spring Framework 5.2 the proxy wasn’t an instance ofSessionso a different code path was taken.We’ll transfer this to the Spring Framework team so that they can take a look.