[英]How to fetch only the specific child entity along with its Parent entities with specific value using Spring JPA DATA
[英]Not able to fetch child entities using inner join in spring data jpa
嗨,我正在尝试获取一个父类部门,在那里我也有一个雇员条款。
在Employee表中,我有一个指标is_active。 我需要找到一个拥有多个员工的部门,其中is_active不是'N'。
我尝试使用存储库。
@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);
上面的查询给了我父数据,但是当试图遍历子实体时,它给了我Employee LazyInitializationException,无法初始化代理-没有会话
我已在OneToMany映射中将fetchType指定为lazy。
等效的sql查询如下。
select t1.*, t2.* from Department t1, Employee t2 where t2.dept_Id = 423 and t1.dept_name='HR' and t1.is_active != 'N'
您可以尝试获取联接以热切地获取子实体。 查询应该看下:
@Query("select t1 from Department t1 inner join fetch t1.employee t2 where t1.deptHead = :deptHead and t1.departmentId = :deptId and t2.isActive != 'N')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.