String springquerysql = "select c.id , c.name, ps.name, sm.id ,sum(case when p.Browse_Number=0 then 1 else 0 end) from Children c " +
"left join Parents ps on(c.Parents_ID=ps.id) " +
"left join Set_Meal sm on(sm.ID=c.Set_Meal_ID) " +
"left join Picture p on(p.Children_ID=c.id) " +
"where ps.id=? GROUP by c.id";
//SQLQuery
SQLQuery sqlQuery = getSession().createSQLQuery(springquerysql);//
sqlQuery.setParameter(0, 1);
List list = sqlQuery.list();
Object[] obj = list.toArray();
for (int i = 0; i < obj.length; i++) {
Object[] objects = (Object[])obj[i];
for (Object object : objects) {
System.out.print(object);
}
System.out.println();
}
这段代码会出现一个问题,就是查询的字段是一样的话,数据也会一样的,这该怎么解决呢?
query
------解决方案--------------------
creatSqlQuery(sql)如果存在相同的字段,比如r.id,c.id那个hibernate将用后面的那个c.id覆盖r.id,也就是查询不出r.id,这是hibernate存在的bug,各位同学如果遇到要注意啊,最好用嵌套查询,可以避免...我已经试过了可以的
------解决方案--------------------
为什么不用储存过程呢