微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

错误无法在SqlResultSetMapping中的类上找到合适的构造函数?

如何解决错误无法在SqlResultSetMapping中的类上找到合适的构造函数?

您好,我遇到问题,但找不到以下解决方我有一个动态查询方法,我不能为字段加上别名

@Override
    public RequestVo getDatoslocos(String query) {
        Query query = entityManager.createNativeQuery(query,"queryMapping");
    
        RequestVo requestVo = query.getSingleResult();
        return requestVo;
    }

当前正在运行的查询

"select id,event_id,body,body_notification from bulletins where id = 11"

这是我要返回的对象,如您所见,我有一列bodyNotification和eventId

public class RequestVo {
    
    private Long id;
    private Long eventId;
    private String title;
    private String body;
    private String bodyNotification;
    
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public Long getEventId() {
        return eventId;
    }
    public void setEventId(Long eventId) {
        this.eventId = eventId;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getBody() {
        return body;
    }
    public void setBody(String body) {
        this.body = body;
    }
    public String getBodyNotification() {
        return bodyNotification;
    }
    public void setBodyNotification(String bodyNotification) {
        this.bodyNotification = bodyNotification;
    }
    public RequestVo(Long id,Long eventId,String title,String body,String bodyNotification) {
        super();
        this.id = id;
        this.eventId = eventId;
        this.title = title;
        this.body = body;
        this.bodyNotification = bodyNotification;
    }
    
    public RequestVo() {
        
        super();
    }
}

我的实体外观

@sqlresultsetmapping(name="queryMapping",classes = {
        @ConstructorResult(targetClass = RequestVo.class,columns = {@ColumnResult(name="id"),@ColumnResult(name="event_id"),@ColumnResult(name="body"),@ColumnResult(name="body_notification")})
    })

但是出现以下错误无法在类上找到合适的构造函数

这是因为在我的构造函数中,我具有不同的列名

我该如何解决?没有为查询添加别名,因为如上所述,我无法移动查询

解决方法

在sqlResultSetMapping中,您仅定义了4个字段,而您的构造函数wn RequestVo类具有5个字段,如下定义构造函数,以匹配您在sqlResultSetMap中指示的参数:

public RequestVo(Long id,Long eventId,String body,String bodyNotification) {
    super();
    this.id = id;
    this.eventId = eventId;
    this.body = body;
    this.bodyNotification = bodyNotification;
}

在定义空构造函数和5参数构造函数时,还必须实现4构造函数,因为它找不到实例化结果的方法。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。