You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

修复$Proxy100转SessionImplementor报错 实现Hibernate全文搜索

问题描述

测试全文搜索接口时抛出如下异常:

java.lang.ClassCastException: class jdk.proxy4.$Proxy100 cannot be cast to class org.hibernate.engine.spi.SessionImplementor

问题报错截图

复现路径:调用接口 localhost:8081/post/search?searchKey=1 时,代码第36行抛出上述异常,对应功能为Hibernate全文搜索。

根因分析

异常本质是JDK动态代理对象强转失败:Spring容器管理的EntityManager/Session对象默认是JDK动态代理生成的代理类实例(也就是报错里的jdk.proxy4.$Proxy100),不是Hibernate原生的SessionImplementor实现类,直接强制类型转换必然抛出类转换异常。

修复步骤
  • 替换直接强转代理对象的错误写法,使用JPA/Hibernate提供的unwrap方法解包获取原生实例,示例代码如下:
    // 移除原有直接强转的错误代码
    // SessionImplementor session = (SessionImplementor) 注入的entityManager;
    
    // 正确写法:先解包获取原生Hibernate Session
    Session nativeSession = entityManager.unwrap(Session.class);
    // 后续全文搜索逻辑基于获取到的原生Session执行即可
    FullTextSession fullTextSession = Search.getFullTextSession(nativeSession);
    
  • 版本校验:确认引入的Hibernate Search版本和当前项目的Hibernate Core、Spring Boot版本匹配,避免版本不兼容导致的代理识别异常,优先使用Spring Boot依赖管理中默认对齐的版本,不要随意混用跨大版本的依赖。

修改完成后重启服务,再调用搜索接口即可正常执行Hibernate全文检索逻辑。

内容的提问来源于stack exchange,提问作者Nguyen The Toan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 15:36:25