Spring Data JPA按示例查询,可以访问嵌套的对象属性

Mar*_*tin 5 spring nested object spring-data-jpa spring-boot

我使用Query by Example并想知道如何在嵌套对象中找到具有某些属性的对象.

有人计划吗?

这是我的示例代码:

    ExampleMatcher matcher = ExampleMatcher.matching()
      .withMatcher("offer2product.id.productId", match -> match.exact()
              );

    Offer2ProductId id = new Offer2ProductId();
    id.setProductId(1337L);

    Offer2Product offer2Product = new Offer2Product();
    offer2Product.setId(id);

    Set<Offer2Product> offer2productSet = new HashSet<>();
    offer2productSet.add(offer2Product);

    Offer probe = new Offer();
    probe.setOffer2productSet(offer2productSet);

    Example<Offer> example = Example.of(probe, matcher);
    List<Offer> offerList = offerRepository.findAll(example);
Run Code Online (Sandbox Code Playgroud)

小智 1

引用Spring数据文档:https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#query-by-example

目前,只有 SingularAttribute 属性可用于属性匹配。

offer2productSet在您的示例中,您希望通过 Set<> ( )属性进行搜索,该属性是PluralAttribute- 无法通过此字段进行搜索。构建查询时它将被忽略,如下所示:

https://github.com/spring-projects/spring-data-jpa/blob/master/src/main/java/org/springframework/data/jpa/convert/QueryByExamplePredicateBuilder.java#L112