String hql
=
"
select u.name,u.password from User u
"
;
Query query = session.createQuery(hql);
Iterator iter = query.iterator();
while (iter.next()) {
Object obj[] = (Object[]iter.next());
System.out.println(obj[ 0 ] + obj[ 1 ]);
}
Query query = session.createQuery(hql);
Iterator iter = query.iterator();
while (iter.next()) {
Object obj[] = (Object[]iter.next());
System.out.println(obj[ 0 ] + obj[ 1 ]);
}
以上代码需要逐一去访问结果集,非常不方便
Hibernate的HQL语言提供了 动态构造实例对象的方法
String hql
=
"
select new User(u.name,u.password) from User u
"
;
Query query = session.createQuery(hql);
Iterator iter = query.iterator();
while (iter.next())... {
User u = (User)it.next();
System.out.println(u.getName() + u.getPassword());
}
Query query = session.createQuery(hql);
Iterator iter = query.iterator();
while (iter.next())... {
User u = (User)it.next();
System.out.println(u.getName() + u.getPassword());
}
特别需要注意的是,对应实体对象中使用指定属性值的构造函数一定要存在