Spring Boot 3.0.9升级后JPA findOne+EntityGraph无法完整加载OneToMany集合
Spring Boot 2.7.4迁移至3.0.9后EntityGraphJpaSpecificationExecutor.findOne的OneToMany集合加载问题
将Spring Boot从2.7.4版本迁移到3.0.9版本后,发现EntityGraphJpaSpecificationExecutor.findOne(Specification<T> spec, EntityGraph entityGraph)方法无法完整解析OneToMany关联集合。在2.7.4版本中该功能正常工作,但官方迁移指南里未提及相关问题。
该问题可在小型演示项目中复现:调用findOne(with(authorId), entityGraph)时,作者对应的所有书籍并未被加载,导致测试失败。预期所有延迟加载的OneToMany关联详情都能被完整解析。
查看生成的SQL日志可见,left join被与fetch first ? rows only限制结合使用,这一逻辑并不合理——因为EntityGraph支持动态配置,关联详情必须完整加载。生成的SQL如下:
select a1_0.id,b1_0.author_id,b1_0.id,b1_0.genre,b1_0.isbn,b1_0.title,a1_0.name from author a1_0 left join book b1_0 on a1_0.id=b1_0.author_id where a1_0.id=? fetch first ? rows only
环境信息
- 本地机器:MacBook Pro,macOS Ventura 13.4.1
- Java版本:17,使用
temurin-17.jdk - Spring Boot版本:3.0.9
- 依赖:
com.cosium.spring.data:spring-data-jpa-entity-graph:3.0.1 - 数据库:Docker镜像中的Postgres 13-alpine
内容的提问来源于stack exchange,提问作者danielv
相关产品推荐
相关产品推荐

