【问题标题】:Unable to locate appropriate constructor on class无法在类上找到适当的构造函数
【发布时间】:2018-09-12 11:40:54
【问题描述】:

我有一个 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 作为值并删除它工作的列表时。

【问题讨论】:

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


    【解决方案1】:

    您在 SearchDTO 类中有几个错误:

    1. private Integer ID 中缺少分号应该是private Integer ID;
    2. 您试图在构造函数中设置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;
        }
    
    }
    

    【讨论】:

    • 感谢您纠正拼写错误。但我的实际问题是关于运行时错误。
    猜你喜欢
    • 2020-12-17
    • 2021-10-25
    • 2021-01-01
    • 2015-05-27
    • 1970-01-01
    • 2022-08-15
    • 1970-01-01
    • 2020-09-05
    • 2022-08-20
    相关资源
    最近更新 更多