问题描述
您好,我遇到问题,但找不到以下解决方案 我有一个动态查询的方法,我不能为字段加上别名
@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构造函数,因为它找不到实例化结果的方法。