具有分页、排序和规范的 JPA

在我的Spring boot项目中,我需要查询一个entity具有,和XrayVulnerabilityEntity功能的表。pagingsortingspecification


Paging并且sorting实施似乎还可以。但是当我添加时Specification,它throws an error是这样的:


导致:org.springframework.beans.factory.BeanCreationException:创建名称为“xrayVulnerabilityRepository”的bean时出错:调用init方法失败;嵌套异常是 java.lang.IllegalArgumentException:要么对除一次输入的 Pageable 和 Sort 之外的所有参数使用 @Param,要么根本不使用 @Param!


我使用的存储库:


@Repository

public interface XrayVulnerabilityRepository extends PagingAndSortingRepository<XrayVulnerabilityEntity,XrayVulnerabilityPK> , JpaSpecificationExecutor<XrayVulnerabilityEntity>{


    @Query("SELECT x FROM XrayVulnerabilityEntity x,DomainArtifactEntity d WHERE d.domainOrgName=:domainOrgNameParam AND x.domainArtifactId=d")

    public Page<XrayVulnerabilityEntity> findAll(@Param(value = "domainOrgNameParam") String domainOrgName,Specification<XrayVulnerabilityEntity> spec, Pageable pageable);


    @Query("SELECT COUNT(x) FROM XrayVulnerabilityEntity x,DomainArtifactEntity d WHERE d.domainOrgName=:domainOrgNameParam AND x.domainArtifactId=d")

    public Long getCount(@Param(value = "domainOrgNameParam") String domainOrgName,Specification<XrayVulnerabilityEntity> spec);


}

我在实施时做错了什么吗Specification?


慕的地6264312
浏览 134回答 3
3回答

开满天机

方法声明:public List<XrayVulnerabilityEntity> findAll(Specification<XrayVulnerabilityEntity> spec, Pageable pageable);执行:Specification specification = new Specification<XrayVulnerabilityEntity>() {&nbsp; &nbsp; @Override&nbsp; &nbsp; public Predicate toPredicate(Root<XrayVulnerabilityEntity> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {&nbsp; &nbsp; List<Predicate> predicates = new ArrayList<>();&nbsp; &nbsp; predicates.add(criteriaBuilder.equal(&nbsp; &nbsp; &nbsp; &nbsp; root.join("domainArtifactId").get("id"), root.get("id")));&nbsp; &nbsp; predicates.add(criteriaBuilder.and(&nbsp; &nbsp; &nbsp; &nbsp;root.get("domainArtifactId").get("domainOrgName"), "domainOrgNameString"));&nbsp; &nbsp; return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));&nbsp; &nbsp; }};Pageable page = PageRequest.of(0, 5, new Sort(Sort.Direction.ASC, "id") );List<XrayVulnerabilityEntity> xrayVulnerabilityEntitylists = xrayVulnerabilityEntityRepo.findAll(specification, page);

四季花海

我像这里一样解决了这个问题;@PostMapping(value = "/search")public List<Customer> search(@RequestBody List<SearchCriteriaRequest> criteriaRequest, Pageable page) {&nbsp; &nbsp; if (criteriaRequest == null || criteriaRequest.size() == 0) {&nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; }&nbsp; &nbsp; SpecificationsBuilder builder = new SpecificationsBuilder();&nbsp; &nbsp; for (SearchCriteriaRequest criteria : criteriaRequest) {&nbsp; &nbsp; &nbsp; &nbsp; builder.with(criteria);&nbsp; &nbsp; }&nbsp; &nbsp; Specification<Customer> spec = builder.build();&nbsp; &nbsp; return customerRepository.findAll(spec, page).getContent();}

犯罪嫌疑人X

在此查询中:@Query("SELECT COUNT(x) FROM XrayVulnerabilityEntity x,DomainArtifactEntity d WHERE d.domainOrgName=:domainOrgNameParam AND x.domainArtifactId=d")public Long getCount(@Param(value = "domainOrgNameParam") String domainOrgName,Specification<XrayVulnerabilityEntity> spec);您正在引用一个参数:domainOrgNameParam,但在方法参数中找不到该参数。如果您使用Specifications,您应该在那里定义所有内容并避免传递额外的参数。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java