请问如下两种方法,哪种较好: 1. Query q = session.createQuery("from Cat as c"); List l = q.list(); for (int i=0; i< l.size(); i++) { Cat c = (Cat)l.get(i); int id = c.getId(); String name = c.getName(); System.out.println(id+" "+name); }
2. Query q = session.createQuery("select c.id, c.name from Cat as c"); List l = q.list(); Object[] row = new Object[2]; for (int i=0; i< l.size(); i++) { row = l.get(i); Integer id = (Integer) row[0]; String name = (String) row[1]; System.out.println(id+" "+name); }
第一次查询 Begin time is 0 Hibernate: select post0_.id as x0_0_ from post post0_ Hibernate: select post0_.id as id, post0_.userid as userid from post post0_ where post0_.id=? End time is 2281
第二次查询 Begin time is 0 Hibernate: select post0_.id as x0_0_ from post post0_ End time is 2625
第三次查询 Begin time is 0 Hibernate: select post0_.id as x0_0_ from post post0_ End time is 1766
附读函数 public void readDB(Session session){ String sql = "select post from com.test.my.Post as post"; long begintime = System.currentTimeMillis(); System.out.println("Begin time is 0 "); try{ Query query = session.createQuery(sql); //List myList=query.list(); //据说用List Jcs无效 Iterator myIter=query.iterate(); }catch (HibernateException ee){ ee.printStackTrace(); } long endtime=System.currentTimeMillis(); System.out.println("End time is "+(endtime-begintime)); }