hibernate 本地查询 字段别名 映射到 DTO时注意事项

本地原生SQL:

SELECT  pole.pId as poleId FROM lamppole pole LEFT JOIN grouppole gp ON pole.pId = gp.pId

 

Query:

Query q = getSession().createSQLQuery(sql2.toString()).addScalar("poleId")
		.setResultTransformer(Transformers.aliasToBean(TestDto.class));

 

如果SQL有别名,那么,query就一定要加

addScalar("别名")

 

不然抛此异常:

could not read column value from result set column 'pId' not found

 

 

你可能感兴趣的:(Hibernate)