在 JPA CriteriaBuilder 连接查询中只获取一个 child

fetch just one child in a JPA CriteriaBuilder Join Query

我有 2 个实体:

public class Tyre {

 @Id
 @Column(name = "tyreid")
 @GeneratedValue
 private int tyreid;
 @Column(name = "brand")
 private String brand;
 @Column(name = "tyretype")
 private String tyretype;
 @OneToMany(targetEntity = TyreAuto.class, cascade = CascadeType.ALL)
 @JoinColumn(name = "tyreid",referencedColumnName = "tyreid")
 private List<TyreAuto> tyreAutos;
}

和:

public class TyreAuto {
 @Id
 @Column(name = "tyreautoid")
 @GeneratedValue
 private int tyreautoid;
 @Column(name = "serie")
 private String serie;
 @Column(name = "tyreid")
 private int tyreid;
}

我有以下代码 returns 一个 json 响应:

    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Tyre> criteriaQuery = criteriaBuilder.createQuery(Tyre.class);
    Metamodel m = entityManager.getMetamodel();
    EntityType<Tyre> Tyre_ = m.entity(Tyre.class);
    Root<Tyre> tyreRoot = criteriaQuery.from(Tyre_);
    Join<Tyre,TyreAuto> tyreAutos = tyreRoot.join("tyreAutos",JoinType.INNER);

    Predicate predicateForBrand
            = criteriaBuilder.equal(tyreRoot.get("brand"), "Firestone");
    Predicate predicateForSerie
            = criteriaBuilder.equal(tyreAutos.get("serie"),"XYZ");
    Predicate predicateForBrandAndSerie
            = criteriaBuilder.and(predicateForSerie,predicateForBrand);
    criteriaQuery.where(predicateForBrandAndSerie);
    List<Tyre> items = entityManager.createQuery(criteriaQuery).getResultList();

    if (!items.equals(null)) {
        return new ResponseEntity<>(items, HttpStatus.OK);
    } else {
        return new ResponseEntity(ResponseEntity.notFound().build(),HttpStatus.NOT_FOUND);
    }

这个returns下面Json:

[
   {
    "tyreid": 1,
    "brand": "Firestone",
    "tyretype": "Big",
    "tyreAutos": [
        {
            "tyreautoid": 38,
            "serie": "XYZ",
            "tyreid": 13
        },
        {
            "tyreautoid": 39,
            "serie": "ABC",
            "tyreid": 13
        },
        {
            "tyreautoid": 40,
            "serie": "JKL",
            "tyreid": 13
        }
      ]
    }
 ]

我的问题是:

我怎样才能以这样的方式过滤响应:

[
   {
    "tyreid": 1,
    "brand": "Firestone",
    "tyretype": "Big",
    "tyreAutos": [
        {
            "tyreautoid": 38,
            "serie": "XYZ",
            "tyreid": 13
        }
      ]
    }
 ]

Why I am getting more than one child register if I am filtering for Serie: "XYZ"?

你得到整个 Tyre 声明的对象 criteriaBuilder.createQuery(Tyre.class) 其中 brand = "Firestone"tyreAutos 包含 tyreAutoserie = "XYZ".

How can I get just one child element instead of all 3?

如果只得到一个 TyreAuto 你可以像这样使用反向查询 criteriaBuilder.createQuery(TyreAuto.class) 以这种方式改变你的实体

public class TyreAuto {
    @Id
    @Column(name = "tyreautoid")
    @GeneratedValue
    private int tyreautoid;

    @Column(name = "serie")
    private String serie;

    @ManyToOne
    @JoinColumn(name = "tyreid")
    private Tyre tyre;
}

现在您可以从 TyreAuto

访问 tyre
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<TyreAuto> criteriaQuery = criteriaBuilder.createQuery(TyreAuto.class);
Metamodel m = entityManager.getMetamodel();
EntityType<TyreAuto> TyreAuto_ = m.entity(TyreAuto.class);
Root<TyreAuto> tyreAutoRoot = criteriaQuery.from(TyreAuto_);
Join<TyreAuto, Tyre> tyre = tyreAutoRoot.join("tyre",JoinType.INNER);

Predicate predicateForBrand
        = criteriaBuilder.equal(tyre.get("brand"), "Firestone");
Predicate predicateForSerie
        = criteriaBuilder.equal(tyreAutoRoot.get("serie"),"XYZ");

criteriaQuery.select(tyreAutoRoot).where(predicateForSerie, predicateForBrand);
List<TyreAuto> items = entityManager.createQuery(criteriaQuery).getResultList();

另一种选择是使用dto class作为查询结果。您可以根据需要配置它。 Here 您可以获得更多信息。阅读条件查询中的 DTO 投影

一章