java.lang.ClassCastException: com.sun.proxy.$Proxy62 无法转换为 org.hibernate.engine.spi.SessionImplementor

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

我在使用 Spring 和 Hibernate Search 时遇到问题。这是我的代码:

@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);
        }
    }
}

我得到:

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

这是我的 POM(属性)的版本(以防版本冲突)

<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>

这是 entityManager 的 bean:

<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>

所以我知道 Spring 正在注入代理,那么我应该如何获取 FullTextEntityManager,解包似乎让我遇到同样的错误?谢谢!

这通常是使用 Hibernate Search 5.11.5.Final 及以下 Spring boot 2.4 / Spring 5.3 时发生的情况。参见 https://github.com/spring-projects/spring-framework/issues/26090

鉴于 ClassCastException 出现在 FullTextSessionImpl.java:62,我怀疑您实际上并没有使用 Hibernate Search 5.11.7.Final:在 Hibernate Search 5.11.7.Final 中,这一行只是 instanceof测试。在 5.11.5.Final 及以下,此行实际上是强制转换。

我看到您将 属性 hibernate.search.version 设置为 5.11.7.Final,但是您是否在 POM 的任何地方使用了这个 属性? Spring Boot 不管理对 Hibernate Search 的依赖,因此您需要在 <dependency> 标记中自行指定版本。