【发布时间】:2020-01-11 04:23:30
【问题描述】:
在我的Spring boot 项目中,我需要使用paging、sorting 和specification 的能力查询一个名为XrayVulnerabilityEntity 的表entity。
Paging 和 sorting 的实现似乎没问题。但是当我添加Specification 时,throws an error 是这样的:
引起: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);
}
在实现Specification 时我做错了吗?
编辑:
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()));
}
}
【问题讨论】:
-
是的,如果您在 @Query 中编写自己的 HQL 查询,那么规范没有用处。
-
有没有更简单的方法将表与规范连接起来,然后编写额外的查询?
-
你能粘贴你的模型类吗
-
添加了实体类
标签: java sql spring spring-data-jpa jpql