gpt4 book ai didi

java - 如何制作具有以下签名 '(x OR y) AND z'的spring data自定义查询方法

转载 作者:行者123 更新时间:2023-12-01 17:42:19 26 4
gpt4 key购买 nike

我有以下存储库,其中包含 2 种自定义查询方法:

@Repository
public interface CropVarietyNameDao extends JpaRepository<CropVarietyName, Long> {
//https://stackoverflow.com/questions/25362540/like-query-in-spring-jparepository
Set<CropVarietyName> findAllByNameIgnoreCaseContainingOrScientificNameIgnoreCaseContaining(String varietyName, String scientificName);
Set<CropVarietyName> findAllByNameIgnoreCaseStartsWithOrScientificNameIgnoreCaseStartsWith(String varietyName, String scientificName);
}

我需要添加一个附加条件,即父实体名为crop returned = false

如果我将方法更改为以下内容:

findAllByNameIgnoreCaseContainingOrScientificNameIgnoreCaseContainingAndCrop_deletedIsFalse(字符串品种名称,字符串科学名称);

那么它很可能会将查询解释为(名称包含 OR (scientificNameContaining AND Crop_deleted = false),但这不是我想要的。它需要是(NameContaining OR ScientificNameContaining) AND Crop_deleted = false)

我的猜测是我必须将 AND Crop_deleted 部分添加到这两个部分,但这似乎效率低下。我如何编写一个基本上是 (NameContaining OR ScientificNameContaining) AND Crop_deleted = false) 的方法,而不必使用 @Query

最佳答案

您可以尝试 JPA 规范 -

@Repository
public interface CropVarietyNameDao extends JpaRepository<CropVarietyName, Long>,
JpaSpecificationExecutor<CropVarietyName> {
}

public class CropVarietySpecs {

public static Specification<CropVarietyName> cropPredicate(String varietyName, String sciName, boolean cropStatus) {
return new Specification<CropVarietyName>() {
@Override
public Predicate toPredicate(Root<CropVarietyName> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
List<Predicate> predicates = new ArrayList<>();
Predicate varietyContainingIgnoreCasePredicate = criteriaBuilder.like(criteriaBuilder.lower(root.get("<column_name>")), varietyName.toLowerCase());
Predicate scientificContainingIgnoreCasePredicate = criteriaBuilder.like(criteriaBuilder.lower(root.get("<column_name>")), sciName.toLowerCase());
Predicate cropStatusPredicate = criteriaBuilder.equal(root.get("<column_name>"), cropStatus);
predicates.add(varietyContainingIgnoreCasePredicate);
predicates.add(scientificContainingIgnoreCasePredicate);
criteriaBuilder.or(predicates.toArray(new Predicate[predicates.size()]));
return criteriaBuilder.and(cropStatusPredicate);
}
};

}
}

然后你可以调用存储库的 findAll() 方法,例如 -

List<CropVarietyName> entities = cropVarietyNameDao.findAll(CropVarietySpecs.cropPredicate("varietyName", "sciName", false));

关于java - 如何制作具有以下签名 '(x OR y) AND z'的spring data自定义查询方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59547704/

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