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

5

我使用示例查询(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);

你有研究过 QueryDsl 吗? - Konstantinos
2个回答

1

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

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

在您的示例中,您想要按一个是Set<> (offer2productSet) 的属性进行搜索,这是一个PluralAttribute-无法通过此字段进行搜索。可以在此处看到,构建查询时会忽略它:

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


0

实际上,人们可以做到这一点。请按照以下示例操作,其中需求具有标签列表。我现在没有时间解释所有内容,但很快我会更新此帖子。

@Repository
@RequiredArgsConstructor
public class DemandaFilterRepository {

    private final EntityManager entityManager;

    public Page<Demanda> findAll(DemandaFilterDTO demandaFilter, Pageable pageable) {
        String query = "select d from Demanda d where 1=1";
        Map<String, Object> parameters = new HashMap<>();
        if (demandaFilter.getId() != null) {
            query += " and d.id = :id";
            parameters.put("id", demandaFilter.getId());
        }
        if (!StringUtils.isEmpty(demandaFilter.getTenant())) {
            query += " and upper(d.tenant) = upper(:tenant)";
            parameters.put("tenant", demandaFilter.getTenant());
        }
        if (!StringUtils.isEmpty(demandaFilter.getAssunto())) {
            query += " and upper(d.assunto) like  upper(:assunto)";
            parameters.put("assunto", "%" + demandaFilter.getAssunto() + "%");
        }
        if (!StringUtils.isEmpty(demandaFilter.getDescricao())) {
            query += " and upper(d.descricao) like upper(:descricao)";
            parameters.put("descricao", "%" + demandaFilter.getDescricao() + "%");
        }
        if (!StringUtils.isEmpty(demandaFilter.getEtiqueta())) {
            query = query.replace("Demanda d", "Demanda d inner join d.etiquetas etiqueta");
            query += " and upper(etiqueta.descricao) = upper(:etiqueta)";
            parameters.put("etiqueta", demandaFilter.getEtiqueta());
        }
        if (!StringUtils.isEmpty(demandaFilter.getNomeDemandante())) {
            query += " and upper(d.demandante.nome) like upper(:nomeDemandante)";
            parameters.put("nomeDemandante", "%" + demandaFilter.getNomeDemandante() + "%" );
        }
        if (!StringUtils.isEmpty(demandaFilter.getDtInclusao())) {
            query += " d.dtInclusao like :dtInclusao";
            parameters.put("dtInclusao", "%" + demandaFilter.getDtInclusao() + "%");
        }
        query += "  ORDER BY d.id DESC ";
        TypedQuery<Demanda> typedQuery = entityManager.createQuery(query, Demanda.class)
                .setMaxResults(pageable.getPageSize())
                .setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
        parameters.forEach(typedQuery::setParameter);
        return new PageImpl<>(typedQuery.getResultList());
    }
}

这是我正在开发的小项目中的一个类...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接