Spring JPA findAll() 在 where 子句中添加附加列

问题描述 投票:0回答:1

我是 Spring Boot 和 JPA 的新手。 我有一个实体类作为 Deal,它有几个字段,如 dealId、name、status 等,并且它扩展了一个具有 id、createdBy、lastModifiedBy 等通用字段的基类。

我正在使用示例匹配器执行 findAll(),我需要查找带有 dealId 或带有 dealId 和状态的所有交易。

DealEntity dealEntitySearchRequest = new DealEntity();
dealEntitySearchRequest.setDealId(dealId);
dealEntitySearchRequest.setStatus(status.getValue());

Example<DealEntity> example = Example.of(dealEntitySearchRequest);
List<DealEntity> deals =  dealDAO.findAll(example);

由于输入参数会发生变化并且需要 AND 条件,因此我使用示例,以便它将动态生成查询。

就像如果我在请求中只传递 dealId,findAll 将在 where 子句中使用 dealId 完成。 如果我传递 dealId AND status,那么 findAll 将使用 dealId AND status 完成。

这效果很好,符合我的目的。

但是,为什么它还在where子句中添加createdBy和lastModifiedBy,我不明白。

知道为什么即使我没有提到要在 where 子句中添加它们,它也会添加它们吗?

希望问题很清楚。 谢谢!

spring-boot spring-data-jpa query-by-example
1个回答
0
投票

在阅读您的问题之前,我什至不知道

findAll()
有一个接受实体作为输入的重载版本。 如果您只想在 JPA 存储库中按 2 列/字段进行搜索,规范的方法是使用适当的工厂方法:

List<DealEntity> findByDealIdAndStatus(String dealId, String status);
© www.soinside.com 2019 - 2024. All rights reserved.