无法在类上找到合适的构造函数

时间:2018-04-03 08:02:47

标签: spring-mvc spring-data-jpa jpql

我有一个JPQL查询,它只需要获取两个字段,一个整数和一个列表值。在这里,我创建了一个Dto,并提到了需要获取的字段。

//定义了所有变量

 query = new StringBuilder();
 query.append(ReposJPQL.GET_INCIDENTS);
 q = em.createQuery(query.toString());
 result = q.getResultList();

其中查询,

GET_INCIDENTS = "SELECT DISTINCT searchDto ("
            + "ih.Id,  ih.logs )"
            + "FROM Hdr ih left join ih.logs sl";

searchDto

public class SearchDto implements Serializable {

    private static final long serialVersionUID = 1L;

    private Integer ID
    private List<IncidentStatusLogDto> statusLogs;

    public SearchDto () {
    }

    public SearchDto (Long incidentId, List<IncidentStatusLogDto> statusLogs) {
        super();
        this.incidentId = incidentId;
        this.statusLogs = statusLogs;
    }

}

HDR实体

@Entity
@Table(name="TB_HDR")
public class IncidentHdr implements Serializable {
    private static final long serialVersionUID = 1L;


    private Id;
    @OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE}, fetch = FetchType.LAZY,  mappedBy="incidentHdr")
    @JsonManagedReference
    private List<Log> logs;

//getters and setters

}

这里我根据需要在searchDto中添加了构造函数,但仍抛出异常。

no appropriate constructor in class: SearchDto]
org.hibernate.hql.internal.ast.DetailedSemanticException: Unable to locate appropriate constructor on class SearchDto]. Expected arguments are: long, java.util.Collection

当我尝试仅将Id作为值并删除列表时,它正在工作。

1 个答案:

答案 0 :(得分:0)

SearchDTO 类中有几处错误:

  1. private Integer ID中缺少分号应为private Integer ID;
  2. 您正尝试在构造函数中设置this.incidentId = incidentId;,但您尚未定义变量。
  3. 由于我不知道您是否错过了它,或者您想用变量ID替换它,我的解决方案是:

    public class SearchDto implements Serializable {
    
        private static final long serialVersionUID = 1L;
    
        private Integer ID;
        private Long incidentId;
        private List<IncidentStatusLogDto> statusLogs;
    
        public SearchDto () {
        }
    
        public SearchDto (Long incidentId, List<IncidentStatusLogDto> statusLogs) {
            super();
            this.incidentId = incidentId;
            this.statusLogs = statusLogs;
        }
    
    }