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

升级Spring Boot 3.2.x(Hibernate 6)后实体查询出现重复父实体问题

解决Spring Boot 3(Hibernate 6)多关联查询的笛卡尔积重复问题

场景说明

我定义了User实体,它与Authority是多对多关系,与UserGroup、Company为多对一关系。之前在Spring Boot 3版本中,使用UserDto投影接口,通过指定attributePaths为authorities、company、userGroup的Entity Graph查询所有用户时,能自动返回去重的User实体。

遇到的问题

升级到Spring Boot 3(对应Hibernate 6)后,同样的查询逻辑出现了笛卡尔积,导致父实体User被重复返回。

试过的无效方案

  • 手写包含join fetch和distinct的JPA查询,直接返回User实体而非投影,重复问题依旧存在。

最终解决办法

Hibernate 6仅对Set类型的返回结果做自动去重,只需将Repository方法的返回类型从List改为Set,就能解决重复问题。

内容的提问来源于stack exchange,提问作者aakash singh

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 22:14:50