将休眠查询的结果转换为类DTO时遇到问题。我的数据库,代码和错误在这里: 数据库
我计算并总结所有错误和成就,并将结果保存到jsp文件中 Table name: record, type 0 is mistake, type 1 is achievement
phpmyadmin中的查询结果
|---------------------------------------------------|
|StaffId | achievement | mistake | total |
| 1111 | 0 | 2 | -2 |
|---------------------------------------------------|
存储库
@Override
public List<RecordFormHomePage> selectt() {
Session session = this.sessionFactory.getCurrentSession();
List<RecordFormHomePage> recordsList = (List<RecordFormHomePage>)session.createSQLQuery(
"select `StaffId`,sum(case when `Type`=1 then 1 else 0 end) as achievement, sum(case when `Type`=0 then 1 else 0 end) as mistake, sum(case when `Type`=1 then 1 else 0 end)-sum(case when `Type`=0 then 1 else 0 end) as total from Records group by `StaffId`\n"
+ "").setResultTransformer(Transformers.aliasToBean(RecordFormHomePage.class)).list();
System.out.println(recordsList.size());
return recordsList;
}
服务
@Override
public List<RecordFormHomePage> list() {
List<RecordFormHomePage> list = recordRepository.selectt();
return list;
}
控制器
@GetMapping
public String index(Model model) {
model.addAttribute("TopStaff", recordService.list());
return "/homepage/index";
}
DTO
public class RecordFormHomePage implements Serializable{
int staffId;
int achievement;
int mistake;
int total;
错误
Message Request processing failed; nested exception is IllegalArgumentException occurred while calling setter for property [vn.sof302.trunglqph04966.record.form.RecordFormHomePage.achievement (expected type = int)]; target = [RecordFormHomePage [StaffId=111111, Achievement=0, mistake=0, total=0]], property value = [0]
答案 0 :(得分:0)
您可以尝试的是显式分配别名列的类型
session.createSQLQuery("select ...")
.addScalar("achievement", IntegerType.INSTANCE)
.addScalar("mistake", IntegerType.INSTANCE)
.addScalar("total", IntegerType.INSTANCE)
.setResultTransformer(Transformers.aliasToBean(RecordFormHomePage.class))
.list();
但是请确保*Type.INSTANCE
和类字段与数据库表的列类型匹配。