简体   繁体   English

在Spring Data JPA中获取具有子级筛选集合的父级实体列表

[英]Fetching a list of parent entities with a filtered collection of children in Spring Data JPA

So I've been stuck on this problem for about half a day so I am wondering if I am just over-complicating things. 所以我在这个问题上停留了大约半天,所以我想知道我是否只是使事情变得过于复杂。

My application has three different Java object classes: Grandparent, Parent, and Child. 我的应用程序具有三种不同的Java对象类:祖父母,父母和孩子。 Each Grandparent contains a List of Parents, and each Parent contains a List of Children. 每个祖父母都包含父母名单,每个父母都包含孩子名单。 Child has an "isWellBehaved" property, which is a boolean. 子级具有“ isWellBehaved”属性,该属性是布尔值。

We are using Spring Data JPA and Hibernate in order to map the data to a database. 我们正在使用Spring Data JPA和Hibernate来将数据映射到数据库。 Our application contains a lot of nested entities and circular relationships and we are relying on projections to keep our request size down. 我们的应用程序包含许多嵌套实体和循环关系,并且我们依靠投影来减小请求大小。

The Problem: given a grandparent id, I want to return a list of all Parents (as projections). 问题:给定祖父母编号,我想返回所有父母的列表(作为投影)。 I want each of the Parents to contain a list of Child projections, but only if the Child is well behaved. 我希望每个父项都包含一个子项投影列表,但前提是该子项行为良好。 The rest of the children in the collection should be filtered out from the collection. 集合中的其余孩子应从集合中过滤掉。

What would be the simplest way to achieve this? 实现这一目标的最简单方法是什么? We are not using Hibernate filters at the moment and I am not keen on introducing them as we are not likely to need them anywhere else (either way, would it be suited for this purpose?). 目前我们不使用Hibernate过滤器 ,我也不愿意引入它们,因为我们不太可能在其他任何地方使用它们(无论哪种方式,都适合于此目的吗?)。 I have used JPA Criteria API predicates (very little) but find it difficult to adapt that to this particular scenario. 我使用了JPA Criteria API谓词 (很少),但发现很难使其适应这种特殊情况。 Is a native query the way to go? 本机查询吗? I've started going in that direction but am having some issues mapping all the fields to our Spring entity due to all the nested dependencies so just want to make sure I am even headed in the right direction before I continue. 我已经开始朝着这个方向前进,但是由于所有嵌套的依赖关系,在将所有字段映射到我们的Spring实体时遇到了一些问题,所以只想确保在继续之前我朝着正确的方向前进。

My (simplified) parent entity class looks like this: 我的(简化的)父实体类如下所示:

@Entity
@Table(name="parent"
public class Parent {

    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="parent_id")
    Integer id;

    Integer grandparentId; 

    @OneToMany(mappedBy = "parent")
    List<Child> children;
}

Child class: 子班:

@Entity
@Table(name="child"
public class Child {

    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="child_id")
    Integer id;

    @ManyToOne
    @JoinColumn(name="parent_id")
    Parent parent;

    boolean isWellBehaved; 
}

Parent repository interface: 父存储库接口:

@RepositoryRestResource(excerptProjection = ParentProjection.class)
public interface ParentRepository extends JpaProjectionRepository<Parent, Integer, ParentProjection> {

    List<ParentProjection> findAllByGrandparent_Id(Integer grandpaId);
}

As you have said: 如您所说:

I want each of the Parents to contain a list of Child projections, but only if the Child is well behaved. 我希望每个父项都包含一个子项投影列表,但前提是该子项行为良好。

List<childerns>allChilderns=parentsList.stream().map(parent>dao.findchildernByParentId()).collect(Collectors.List());

allChilderns.stream().filter(childern->childern.isWellBehaved()==true).collect(Collectors.toList());
  1. Get all the parents by GrandParentId--the one which you are doing. 通过GrandParentId(您正在做的那一个)来获取所有父母。
  2. Once you got all the parents,for each parent findchildernByParentId. 一旦获得所有父母,就可以为每个父母找到childBernParentId。
  3. and then filter out the childern on the basis of condition. 然后根据条件筛选出孩子。

Let me know:) 让我知道:)

You can use @Where annotation of hibernate on collection. 您可以在集合上使用休眠的@Where注释。 It will be something like 它会像

@Entity
@Table(name="parent"
public class Parent {

    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="parent_id")
    Integer id;

    Integer grandparentId; 

    @Where(clause = "isWellBehaved=true")
    @OneToMany(mappedBy = "parent")
    List<Children> children;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Data JPA + Hibernate 保存子实体而不先找到父实体 - Spring Data JPA + Hibernate save children entities without finding parent firstly Spring Data JPA-在显式删除子级后加载父级将返回具有已删除子级的子级的集合 - Spring Data JPA - loading parent after explicit child deletion returns collection of children with deleted child 如何使用 Lombok 在 Spring/JPA/Hibernate 中获取带有所有子实体和子实体的父实体 - How to get parent entity with all child entities and child entities of children in Spring/JPA/Hibernate with Lombok 春天在OneToMany JPA Relationship中使用子数据获取父数据 - Fetching parent data using Child Data in OneToMany JPA Relationship in spring 使用 spring jpa 获取数据 - fetching the data with spring jpa Spring Data Jpa OneToMany 同时保存子实体和父实体? - Spring Data Jpa OneToMany save child and parent entities at the same time? Spring Data jpa(hibernate)nullpointer添加实体列表时的异常 - Spring Data jpa (hibernate) nullpointer Exception when adding List of Entities JPA:通过给定列的值列表获取实体列表 - JPA: Fetching list of entities by list of values for a given column 使用 Spring 数据获取数据 JPA Stream - Data fetching using Spring Data JPA Stream 如何在spring-jpa中保存基于实体的从父到子表的数据 - How to save data-from-parent-to-child-tables-based-on-entities-in-spring-jpa
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM