我有一个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";
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;
}
}
@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作为值并删除列表时,它正在工作。
答案 0 :(得分:0)
SearchDTO 类中有几处错误:
private Integer ID
中缺少分号应为private Integer ID;
this.incidentId = incidentId;
,但您尚未定义变量。由于我不知道您是否错过了它,或者您想用变量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;
}
}