gpt4 book ai didi

Spring Data JPA Query by Example,可以访问嵌套的对象属性

转载 作者:行者123 更新时间:2023-12-04 21:30:40 34 4
gpt4 key购买 nike

我使用 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);

最佳答案

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

Currently, only SingularAttribute properties can be used for property matching.



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

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

关于Spring Data JPA Query by Example,可以访问嵌套的对象属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52871967/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com