为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕的地6264312 2023-07-19 16:12:41
在我的Spring boot项目中,我需要查询一个entity具有,和XrayVulnerabilityEntity功能的表。pagingsortingspecificationPaging并且sorting实施似乎还可以。但是当我添加时Specification,它throws an error是这样的:导致:org.springframework.beans.factory.BeanCreationException:创建名称为“xrayVulnerabilityRepository”的bean时出错:调用init方法失败;嵌套异常是 java.lang.IllegalArgumentException:要么对除一次输入的 Pageable 和 Sort 之外的所有参数使用 @Param,要么根本不使用 @Param!我使用的存储库:@Repositorypublic 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?
查看完整描述

3 回答

?
开满天机

TA贡献1786条经验 获得超12个赞

方法声明:


public List<XrayVulnerabilityEntity> findAll(Specification<XrayVulnerabilityEntity> spec, Pageable pageable);

执行:


Specification specification = new Specification<XrayVulnerabilityEntity>() {

    @Override

    public Predicate toPredicate(Root<XrayVulnerabilityEntity> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {

    List<Predicate> predicates = new ArrayList<>();

    predicates.add(criteriaBuilder.equal(

        root.join("domainArtifactId").get("id"), root.get("id")));

    predicates.add(criteriaBuilder.and(

       root.get("domainArtifactId").get("domainOrgName"), "domainOrgNameString"));


    return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));

    }

};


Pageable page = PageRequest.of(0, 5, new Sort(Sort.Direction.ASC, "id") );



List<XrayVulnerabilityEntity> xrayVulnerabilityEntitylists = xrayVulnerabilityEntityRepo.findAll(specification, page);



查看完整回答
反对 回复 2023-07-19
?
四季花海

TA贡献1811条经验 获得超5个赞

我像这里一样解决了这个问题;


@PostMapping(value = "/search")

public List<Customer> search(@RequestBody List<SearchCriteriaRequest> criteriaRequest, Pageable page) {

    if (criteriaRequest == null || criteriaRequest.size() == 0) {

        return null;

    }

    SpecificationsBuilder builder = new SpecificationsBuilder();

    for (SearchCriteriaRequest criteria : criteriaRequest) {

        builder.with(criteria);

    }

    Specification<Customer> spec = builder.build();

    return customerRepository.findAll(spec, page).getContent();

}


查看完整回答
反对 回复 2023-07-19
?
犯罪嫌疑人X

TA贡献2080条经验 获得超4个赞

在此查询中:


@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,您应该在那里定义所有内容并避免传递额外的参数。


查看完整回答
反对 回复 2023-07-19
  • 3 回答
  • 0 关注
  • 133 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信