最近在做项目中使用到hibernate 的new map 的用法,这里记录一下:
//HQL-Map 语法demo
String hql = " select new map(usr.name as userName, usr.password as password) from User usr";
Query query = session.createQuery(hql);
List list = query.list();
Map goods =(Map)list.get(0);
// 实例:
public Object findByCid(String cid){
StringBuffer sbString = new StringBuffer();
sbString.append( " select new map( cid as cid , count(cid) as count)from Registration group by cid");
List list = baseService.find(sbString.toString());
System.out.println("**********");
for(Iterator ite = list.iterator();ite.hasNext();){
HashMap map = (HashMap)ite.next();
String cid2 = (String)map.get("cid");
Long count = (Long)map.get("count");
System.out.println("**********cid="+cid2);
System.out.println("**********count="+count);
}
return 1;
}
注意:模型model中 无需在添加自动,但是 Registration 必须是类名称 而不是表名称,查找的语句 必须有别名,才能获取的到值。
from 前面没有空格
参考:http://hi.baidu.com/tianfu_xue/blog/item/e16b1e1fdc8b620d314e15e4.html
和
http://hi.baidu.com/suofang/blog/item/0947291f754a4cd2a78669c0.html的介绍
//HQL-Map 语法demo
String hql = " select new map(usr.name as userName, usr.password as password) from User usr";
Query query = session.createQuery(hql);
List list = query.list();
Map goods =(Map)list.get(0);
// 实例:
public Object findByCid(String cid){
StringBuffer sbString = new StringBuffer();
sbString.append( " select new map( cid as cid , count(cid) as count)from Registration group by cid");
List list = baseService.find(sbString.toString());
System.out.println("**********");
for(Iterator ite = list.iterator();ite.hasNext();){
HashMap map = (HashMap)ite.next();
String cid2 = (String)map.get("cid");
Long count = (Long)map.get("count");
System.out.println("**********cid="+cid2);
System.out.println("**********count="+count);
}
return 1;
}
注意:模型model中 无需在添加自动,但是 Registration 必须是类名称 而不是表名称,查找的语句 必须有别名,才能获取的到值。
from 前面没有空格
参考:http://hi.baidu.com/tianfu_xue/blog/item/e16b1e1fdc8b620d314e15e4.html
和
http://hi.baidu.com/suofang/blog/item/0947291f754a4cd2a78669c0.html的介绍