1、 省略as,指定实体类的别名
示例:
public void testUseAlias(){
Session session = null;
session = HibernateUtils.getCurrentSession();
//使用别名
String hql = "select gb.name as name from Guestbook gb";
Query query = session.createQuery(hql);
List<String> list = query.list();
for(String name : list){
System.out.println(name);
}
}
2、别名与属性值组成key/value对
如果属性中也使用别名,则查询持久化对象多个属性的第3种方式可以修改为如下代码:
public void testUseAlias(){
Session session = null;
session = HibernateUtils.getCurrentSession();
String hql = "select new map(gb.id as id,gb.name as name,gb.email as email) from Guestbook gb";
Query query = session.createQuery(hql);
List<Map> list = query.list();
System.out.println("ID"+"\t"+"Name"+"\t"+"Email");
for(Map map : list){
System.out.println(map.get("id")+"\t"+map.get("name")+"\t"+map.get("email"));
}
}