Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jpa JPQL筛选子级_Jpa_Spring Data Jpa - Fatal编程技术网

Jpa JPQL筛选子级

Jpa JPQL筛选子级,jpa,spring-data-jpa,Jpa,Spring Data Jpa,我使用的是spring数据jpa: 过滤父对象上的子对象的最佳方法是什么。 下面是我的例子 我想要有活动子对象的父对象,也想要只有活动子对象作为具有父对象的列表 @Query(select distinct p from Parent p inner join p.child c where c.active=:active) Page<Parent> getAllPArents(@Param("active") int active); @Entity

我使用的是spring数据jpa: 过滤父对象上的子对象的最佳方法是什么。 下面是我的例子 我想要有活动子对象的父对象,也想要只有活动子对象作为具有父对象的列表

  @Query(select distinct p from Parent p inner join p.child c where c.active=:active)

    Page<Parent> getAllPArents(@Param("active") int active);

    @Entity
    Parent{  
      @OneToMany(fetch=FetchType.LAZY)
      List<Child> child;
    }

    @Entity
    Child{
      @ManyToOne
      Parent parent;
    }

在花了很多时间之后,我找不到任何解决方案来在查询父对象时过滤子对象。
我决定更改数据库结构,以便能够查询我想要的子对象。添加了另一个可以将子项放入不同类别的字段/属性。当使用唯一的条件查询孩子时,只会给我需要的孩子,也会给我需要的父母。

我遇到了完全相同的问题,我花了一段时间才发现这是如何工作的。当您在加入后添加FETCH时,子列表将被过滤,如下所示:

SELECT p FROM Parent p JOIN FETCH p.child c WHERE c.active=:active

我找到了这样做的方法: 您可以使用@Where注释过滤一对多或多对多关系的子项

public class Parent {

    @OneToMany(mappedBy="Parent")
    @Where(clause = "active = 1") //In case of a boolean but it can be a LIKE, a comparator...
    private Set<Child> childs; //This gets filled with pets with a name starting with N

    //getters & setters
}