在我的Spring boot项目中,我需要查询具有entityXrayVulnerabilityEntitypaging功能的表sorting

specificationPaging实现似乎还可以。但是当我添加sorting时,它像这样的Specification


  引起原因:org.springframework.beans.factory.BeanCreationException:
  创建名称为“ xrayVulnerabilityRepository”的bean时出错:
  调用init方法失败;嵌套异常为
  java.lang.IllegalArgumentException:在全部上使用@Param
  除Pageable和Sort之外的所有参数都键入一次,或者根本不输入!


我使用的存储库:

@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);

}


在实施throws an error时我做错什么了吗?

编辑:

XrayVulnerabilityEntity:

import java.util.Date;
import java.util.Objects;

import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

/**
 * The persistent class for the "xray_vulnerability" database table.
 *
 */
@Entity
@Table(name = "xray_vulnerability")
public class XrayVulnerabilityEntity extends BaseEntity {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private XrayVulnerabilityPK id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "domain_artifact_id", nullable = false, insertable = false, updatable = false)
    private DomainArtifactEntity domainArtifactId;

    @Column(name = "severity", nullable = true, length = 128)
    private String severity;

    @Column(name = "type", nullable = true, length = 128)
    private String type;

    @Column(name = "summary", nullable = true, length = 4000)
    private String summary;

    @Column(name = "infected_file", insertable = false, updatable = false, nullable = true, length=255)
    private String infectedFile;

    @Column(name = "full_path", insertable = false, updatable = false, nullable = true,length=255)
    private String fullPath;

    @Column(name = "created", nullable = true, length = 6)
    private Date created;

    public XrayVulnerabilityEntity() {
        super();
        // TODO Auto-generated constructor stub
        id = new XrayVulnerabilityPK();
    }

    public XrayVulnerabilityPK getId() {
        return id;
    }

    public void setId(XrayVulnerabilityPK id) {
        this.id = id;
    }

    public String getFullPath() {
        return fullPath;
    }

    public void setFullPath(String fullPath) {
        this.fullPath = fullPath;
    }

    public DomainArtifactEntity getDomainArtifactId() {
        return domainArtifactId;
    }

    public void setDomainArtifactId(DomainArtifactEntity domainArtifactId) {
        this.domainArtifactId = domainArtifactId;
    }

    public String getSeverity() {
        return severity;
    }

    public void setSeverity(String severity) {
        this.severity = severity;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getSummary() {
        return summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    public String getInfectedFile() {
        return infectedFile;
    }

    public void setInfectedFile(String infectedFile) {
        this.infectedFile = infectedFile;
    }


    public Date getCreated() {
        return created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }

    @Override
    public int hashCode() {
        // TODO Auto-generated method stub
        return Objects.hash(this.getId());
    }

    @Override
    public boolean equals(Object obj) {
        // TODO Auto-generated method stub
        if (this == obj) {
            return true;
        }

        if (!this.getClass().isInstance(obj)) {
            return false;
        }

        XrayVulnerabilityEntity other = (XrayVulnerabilityEntity) obj;

        return (this.getId().equals(other.getId()));
    }
}


DomainArtifactEntity:

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;


/**
 * The persistent class for the "domain_artifact_map" database table.
 *
 */
@Entity
@Table(name = "domain_artifact_map")
@NamedQuery(name = "DomainArtifactEntity.findEntityID", query = "SELECT d FROM DomainArtifactEntity d where d.domainOrgName=?1 and d.pathName=?2")
public class DomainArtifactEntity extends BaseEntity{

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "id", unique = true, nullable = false, precision = 10)
    protected Long id;

    @OneToMany(mappedBy= "domainArtifactId",cascade = CascadeType.ALL)
    private List<XrayVulnerabilityEntity> xrayVulnerabilityList;

    @Column(name = "path_name", nullable = false, length = 255)
    private String pathName;

    @Column(name = "domain_org_name", nullable = false, length = 255)
    private String domainOrgName;


    public DomainArtifactEntity() {
        super();
        // TODO Auto-generated constructor stub
        xrayVulnerabilityList= new ArrayList<XrayVulnerabilityEntity>();
    }

    public void addVulnerability(XrayVulnerabilityEntity vuln) {
        xrayVulnerabilityList.add(vuln);
        vuln.setDomainArtifactId(this);
    }

    public void removeVulnerability(XrayVulnerabilityEntity vuln) {
        xrayVulnerabilityList.remove(vuln);
        vuln.setDomainArtifactId(null);
    }

    public List<XrayVulnerabilityEntity> getXrayVulnerabilityList() {
        return xrayVulnerabilityList;
    }

    public void setXrayVulnerabilityList(List<XrayVulnerabilityEntity> xrayVulnerabilityList) {
        this.xrayVulnerabilityList = xrayVulnerabilityList;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getPathName() {
        return pathName;
    }

    public void setPathName(String pathName) {
        this.pathName = pathName;
    }

    public String getDomainOrgName() {
        return domainOrgName;
    }

    public void setDomainOrgName(String domainOrgName) {
        this.domainOrgName = domainOrgName;
    }

    @Override
    public int hashCode() {
        // TODO Auto-generated method stub
        return Objects.hash(this.getId());
    }

    @Override
    public boolean equals(Object obj) {
        // TODO Auto-generated method stub
        if (this == obj) {
            return true;
        }

        if (!this.getClass().isInstance(obj)) {
            return false;
        }

        DomainArtifactEntity other = (DomainArtifactEntity) obj;

        return (this.getId().equals(other.getId()));
    }


}

最佳答案

方法声明:

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);

09-11 20:32