Posted on 2005-12-02 09:41
Terry的Blog 阅读(376)
评论(0) 编辑 收藏 所属分类:
java语言
取单个字段,会返回字段List:
java代码: |
Query q = s.createQuery("select c.id from Cat as c");
List l = q.list();
for (i=0; i< l.size(); i++) {
Long id = (Long) l.get(i);
System.out.println(id.longValue());
}
|
取多个字段,Hibernate把每个id和name做成一个2个元素的单维数组,List实际上是一个单维数组的集合。
java代码: |
Query q = s.createQuery("select Cat.id, Cat.name, from Cat ");
List l = q.list();
for (int i=0; i< l.size(); i++) {
Object[] row = (Object[]) l.get(i);
Long id = (Long) row[0];
String name = (String) row[1];
}
|
在这种情况下,HQL是不会去构造PO的,这一点很容易验证。JCS是对象Cache,如果写了这样的HQL,JCS里面是空的,说明Hibernate没有构造PO,如果再加上一个对象c本身,JCS里面就有数据了,如下:
java代码: |
Query q = s.createQuery("select c.id, c.name,c from Cat as c");
List l = q.list();
for (int i=0; i< l.size(); ; i++) {
Object[] row = (Object[]) l.get(i);
Long id = (Long) row[0];
String name = (String) row[1];
Cat c = (Cat) row[2];
}
|