Hibernate查询返回结果
Hibernate查询单个字段,Hibernate把每个id做成一个元素的一维数组,List存放的是一维数组。
Query query = s.createQuery("select u.id from User as u");
List list = query.list();
for (i=0; i< list.size(); i++) {
Long id = (Long) list.get(i);
System.out.println(id);
}
Hibernate查询多个字段,Hibernate把每个id和name做成一个2个元素的一维数组,List存放的是一维数组。
Query query = s.createQuery("select u.id,u.name from User as u");
List list = qurey.list();
for (int i=0; i< list.size(); i++) {
Object[] row = (Object[])list.get(i);
Long id = (Long) row[0];
String name = (String) row[1];
System.out.println(“id = ”+id+“ name = ”+name);
}
Hibernate查询一个字段和User对象,Hibernate把每个id和User对象做成一个2个元素的一维数组,List存放的是一维数组。
Query query = s.createQuery("select u.id , u from User as u");
List list = query.list();
for (int i=0; i< list.size(); i++) {
Object[] row = (Object[])list.get(i);
Long id = (Long) row[0];
User user = (User) row[1];
System.out.println(“id = ”+id+“ name = ”+name+“ User = ”+user);
}