为什么冬眠中的AddScalar不起作用?
我试图使用本机标语来创建查询,但是当我尝试添加到DTO中时。它不认识DTO。
public List<AbsenceResponse> findAllByUserId1(Long id) {
List<AbsenceResponse> res = null;
StringBuilder sql = new StringBuilder();
sql.append(
"SELECT a.absence_id, a.create_date, a.end_date,a.date_off,a.title,a.description,a.status,a.type,a.enable_date, u.full_name as fullNameOfDepartment");
sql.append(" FROM absence a ");
sql.append(" INNER JOIN user u ON u.user_id = a.absence_by ");
sql.append(" WHERE a.user_id = "+id);
res = entityManager.unwrap(org.hibernate.Session.class).createNativeQuery(sql.toString())
.addScalar("absenceId", LongType.INSTANCE)
.addScalar("createDate", DateType.INSTANCE)
.addScalar("end_date", DateType.INSTANCE)
.addScalar("dateOff", IntegerType.INSTANCE)
.addScalar("title", StringType.INSTANCE)
.addScalar("description", StringType.INSTANCE)
.addScalar("status", IntegerType.INSTANCE)
.addScalar("type", IntegerType.INSTANCE)
.addScalar("enable_date", DateType.INSTANCE)
.addScalar("fullNameOfDepartment", StringType.INSTANCE)
.setResultTransformer(Transformers.aliasToBean(AbsenceResponse.class))
.list();
return res;
}
这是我的缺陷(DTO),
public class AbsenceResponse {
private Long absenceId;
private Date startDate;
private Date endDate;
private String title;
private String description;
private int status;
private int type;
private Date enableDate;
private String fullNameOfDepartment
}
轮到这个错误了,
java.sql.SQLException: Column 'absenceId' not found.
这是我第一次使用本地标语。如果我有不良或缺少的东西,请在下面发表评论。您的评论对我有很多帮助。玩的很开心
Im trying to create a query using NativeQuery but when i'm trying to add into a DTO. It's not recognize DTO.
public List<AbsenceResponse> findAllByUserId1(Long id) {
List<AbsenceResponse> res = null;
StringBuilder sql = new StringBuilder();
sql.append(
"SELECT a.absence_id, a.create_date, a.end_date,a.date_off,a.title,a.description,a.status,a.type,a.enable_date, u.full_name as fullNameOfDepartment");
sql.append(" FROM absence a ");
sql.append(" INNER JOIN user u ON u.user_id = a.absence_by ");
sql.append(" WHERE a.user_id = "+id);
res = entityManager.unwrap(org.hibernate.Session.class).createNativeQuery(sql.toString())
.addScalar("absenceId", LongType.INSTANCE)
.addScalar("createDate", DateType.INSTANCE)
.addScalar("end_date", DateType.INSTANCE)
.addScalar("dateOff", IntegerType.INSTANCE)
.addScalar("title", StringType.INSTANCE)
.addScalar("description", StringType.INSTANCE)
.addScalar("status", IntegerType.INSTANCE)
.addScalar("type", IntegerType.INSTANCE)
.addScalar("enable_date", DateType.INSTANCE)
.addScalar("fullNameOfDepartment", StringType.INSTANCE)
.setResultTransformer(Transformers.aliasToBean(AbsenceResponse.class))
.list();
return res;
}
And this is my AbsenceResponse(DTO)
public class AbsenceResponse {
private Long absenceId;
private Date startDate;
private Date endDate;
private String title;
private String description;
private int status;
private int type;
private Date enableDate;
private String fullNameOfDepartment
}
it's turn this error
java.sql.SQLException: Column 'absenceId' not found.
This is the first time i used NativeQuery. If i have something bad or missing, please comment below. Your comment is helped me alot. Have a good time
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Select-Query返回的列被命名为
haberence_id
(而不是habenceid
)。对于所有其他与不匹配的其他列的错误 -创建
例如(应该为create_date
)。它是本机查询,因此您需要使用本机SQL查询返回的名称(而不是类中属性的名称)。
您还可以将查询更改为:
这样,查询结果将与BEAN中的属性匹配,并应将问题与结果变压器解决。
The column returned by the select-query is named
absence_id
(and notabsenceId
). You will have a similar error for all the other columns not matching -createDate
for example (it should becreate_date
).It's a native query, so you need to use the named returned by the native SQL query (and not the name of the attribute in the class).
You can also change the query to:
This way the result of the query will match the attributes in the bean and should fix the issue with the result transformer.