【发布时间】:2018-04-17 20:30:54
【问题描述】:
前段时间我在使用内部连接获取子实体时遇到问题。
Not able to fetch child entities using inner join in spring data jpa
我现在面临的问题有点不同。
@Query("select t1 from Department t1 inner join t1.employee t2 where t1.deptHead = :deptHead and t1.departmentId = :deptId and t2.isActive != 'N')
public Department fetchDepartmentByActiveEmployees(@Param(deptId) Long deptId, @Param(deptHead) String deptHead);
以上代码在使用@Fetch(FetchType.Join)后可以工作
@Query("select t1 from Department t1 inner join t1.employee t2 where t1.deptHead = :deptHead and t2.isActive != 'N')
public List<Department> fetchDepartmentByActiveEmployees(@Param(deptId) Long deptId, @Param(deptHead) String deptHead);
仅使用 deptHead 作为查询参数时,我得到重复记录。
我的一个解决方案是使用 Set 进行一对多映射而不是列表。
但如果我这样做,我必须进行很多我想避免的代码更改。 还有其他选择吗?
当我使用 dept_head 查询时,我应该得到一个大小为 2 的列表,而我得到的是大小为 8 的列表。
【问题讨论】:
-
请将您的代码添加到您的问题中
-
尝试使用
DISTINCT。@Query("select DISTINCT t1 from Department t1 inner join fetch t1.employee t2 where t1.deptHead = :deptHead and t1.departmentId = :deptId and t2.isActive != 'N') -
它可以作为不同问题的一部分,我在问题中提供的链接。
-
不过,请将您的代码也添加到此问题中。
-
如果您仍然遇到问题,请在您的问题中添加示例数据
标签: java hibernate spring-data-jpa