在JPA CriteriaBuilder联接查询中仅获取一个孩子

时间:2020-03-10 15:23:45

标签: jpa entity criteria entitymanager hibernate-criteria

我有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;
}

我有以下代码返回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);
    }

这将返回以下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
        }
      ]
    }
 ]

我的问题是:

  • 如果我要过滤以下内容,为什么会得到一个以上的孩子注册表 意甲:“ XYZ”?
  • 我如何只获得一个子元素而不是全部三个?

我该如何过滤以使响应类似于以下内容:

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

1 个答案:

答案 0 :(得分:1)

如果我要过滤Serie:“ XYZ”,为什么我得到的子登记不止一个?

您获得了声明为Tyre的整个criteriaBuilder.createQuery(Tyre.class)对象,其中brand = "Firestone"tyreAutos包含tyreAutoserie = "XYZ"

我如何只获得一个子元素而不是全部三个?

要只获得一个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;
}

现在您可以从tyre来访问TyreAuto

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类作为查询结果。您可以根据需要进行配置。 Here,您可以获得更多信息。阅读标准查询中的 DTO预测

一章