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

Most upvoted comments

@jhoeller Thanks for confirming this.

For the record, here are the relevant PRs in Hibernate Search:

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! EventSource is even worse since it’s too internal for HibernateJpaVendorAdapter to use it as a default EntityManager interface. I’ll rather change our strategy then, reverting to plain Session as default EntityManager interface but handling unwrap calls 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 to SessionImplementor. It should really rather call EntityManager.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 native Session and could therefore be cast to the implementor interface at any point. Maybe we should simply use SessionImplementor as our EntityManager interface in HibernateJpaVendorAdapter…

@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 entityManagerInterface has changed from org.hibernate.jpa.HibernateEntityManager to org.hibernate.Session. This has a knock-on effect when Hibernate Search attempts to unwrap the EntityManager as a Session:

private static Session getSession(EntityManager em) {
    try {
        return em.unwrap( Session.class );
    }
    catch (PersistenceException e) {
        throw new SearchException(
                "Trying to use Hibernate Search with a non-Hibernate EntityManager", e
        );
    }
}

The EntityManager (a Spring Framework created proxy) now implements Session so the proxy is returned as-is. The proxy does not implement SessionImplementor so the subsequent cast fails. In Spring Framework 5.2 the proxy wasn’t an instance of Session so a different code path was taken.

We’ll transfer this to the Spring Framework team so that they can take a look.