Search code examples
springhibernatehibernate-search

java.lang.ClassCastException: com.sun.proxy.$Proxy62 cannot be cast to org.hibernate.engine.spi.SessionImplementor


I'm having an issue with Spring and Hibernate Search. Here is my code:

@Slf4j
@Repository
public class DefaultIndexBuilderDao implements IndexBuilderDao {

    @PersistenceContext
    @Getter
    @Setter
    private EntityManager entityManager;

    @Override
    public void rebuildIndex() {
        try {
            log.debug("Starting the reindex process...");
            FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(getEntityManager());
            fullTextEntityManager.createIndexer().startAndWait();
            log.debug("Reindex complete.");
        } catch( InterruptedException e ) {
            log.warn("Error rebuilding index: {}", e.getMessage(), e);
        }
    }
}

I'm getting:

Caused by: java.lang.ClassCastException: com.sun.proxy.$Proxy62 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 com.domainwww.dao.DefaultIndexBuilderDao.rebuildIndex(DefaultIndexBuilderDao.java:38)
    at com.domainwww.service.DefaultIndexBuilderService.rebuildIndex(DefaultIndexBuilderService.java:30)
    at com.domainwww.beans.admin.IndexBean.reindex(IndexBean.java:29)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.el.parser.AstValue.invoke(AstValue.java:247)
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:267)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:107)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
    ... 57 more

Here are the versions from my POM (properties) (in case this is a version conflict)

<spring.version>5.3.1</spring.version>
<hibernate.version>5.4.24.Final</hibernate.version>
<hibernate.search.version>5.11.7.Final</hibernate.search.version>

Here is by bean for entityManager:

<bean id="entityManager" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="net.xxxx,com.xxxx.dbmanager3.settings" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="hibernate.dialect.storage_engine">innodb</prop>
                <prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.connection.autocommit">false</prop>
                <prop key="hibernate.search.default.directory_provider">filesystem</prop>
                <prop key="hibernate.search.default.indexBase">/opt/xxx/lucene/indexes</prop>
            </props>
        </property>
    </bean>

So I get that Spring is injecting a Proxy, so how should I be obtaining the FullTextEntityManager, unwrapping seems to leave me with the same error? Thanks!


Solution

  • This is typically what happens when using Hibernate Search 5.11.5.Final and below with Spring boot 2.4 / Spring 5.3. See https://github.com/spring-projects/spring-framework/issues/26090

    Given the ClassCastException occurs at FullTextSessionImpl.java:62, I suspect you are not actually using Hibernate Search 5.11.7.Final: in Hibernate Search 5.11.7.Final, this line is just an instanceof test. In 5.11.5.Final and below, this line actually is a cast.

    I see you set property hibernate.search.version to 5.11.7.Final, but did you use this property anywhere in your POM? Spring Boot doesn't manage the dependency to Hibernate Search, so you need to specify the version yourself in your <dependency> markup.