spring data jpa filter children in @OneToMany

fnvucqvd  于 11个月前  发布在  Spring
关注(0)|答案(1)|浏览(172)

我有一个测试测试实体是父实体,FunGroupInfo实体是子实体,这两个实体都是通过测试ID**Map的,我需要一种方法来过滤掉符合搜索条件的子实体,这样结果就只包含父实体和子实体(满足要求)。
测试类:

@Entity(name = "EmployeeTbl")
public class EmployeeTest{
 @Id
 @Column(name = "emp_id")
 private String employeeId;

 @OneToMany(mappedBy= "employeeId", fetch =FetchType.Eager)
 private Set<FunGroupInfo> funGroupInfo;
}

字符串
FunGroupInfo类:

@Entity(name = "FunGroupTbl")
public class FunGroupInfo{
 @Id
 @Column(name = "group_id")
 private String groupId;

 @Column(name = "emp_id")
 private String employeeId;

 @Column(name = "type_id")
 private String typeId;

 @Column(name = "active")
 private String activeFlag;

}


EmpRepository接口:

@Repository
public interface EmpRepository extends JpaRepository<EmployeeTest, String>{
List<EmployeeTest> findByFunGroupInfoTypeId(String typeId)
}

//inside by test method

@Autowired 
private EmpRepository  empRepository;

List<EmployeeTest> empList = empRepository.findByFunGroupInfoTypeId("2");


上面的一行返回我的列表与FunGroupInfo其中typeId是在1,2,3,4,5,但我需要得到唯一的匹配FunGroupInfo与typeId 2信息
结果,我现在得到

,但我实际上只需要一个突出显示的沿着与父

jjjwad0x

jjjwad0x1#

如果你使用的是 Hibernate,你可以使用注解 @Where 来过滤 OneToMany 关系中的元素。
范例:

import org.hibernate.annotations.Where;
  ...
  @OneToMany(fetch = FetchType.EAGER, mappedBy = "employeeId", cascade = CascadeType.ALL)
  @Where(clause = "type_id = '2'")
  private Set<FunGroupInfo> funGroupInfo;
  ...

字符串
更多信息可以咨询here

相关问题