zoukankan      html  css  js  c++  java
  • list 和 iterate

    list 和 iterate 不同之处

      a) list取所有;
      b) iterate 先取ID,等到用到的时候再根据ID来取对象;
      c) session 中 list 第二次发出,仍会到数据库查询;
      d) iterate 第二次,首先找 session 级缓存。

    测试1:

     1     @Test
     2     public void testQueryList(){
     3         Session session = sf.getCurrentSession();
     4         session.beginTransaction();
     5         
     6         List<Category> li = (List<Category>)session.createQuery("from Category").list();
     7         
     8         for(Category c : li){
     9             System.out.println(c.getId() + "-" + c.getName() );
    10         }
    11         
    12         List<Category> li2 = (List<Category>)session.createQuery("from Category").list();
    13         for(Category c : li2){
    14             System.out.println(c.getId() + "-" + c.getName() );
    15         }
    16         
    17         session.getTransaction().commit();
    18     }

    测试2:

     1     @Test
     2     public void testQueryIterate(){
     3         Session session = sf.getCurrentSession();
     4         session.beginTransaction();
     5         
     6         Iterator<Category> it = (Iterator<Category>)session.createQuery("from Category c where c.id < 4").iterate();
     7         
     8         while(it.hasNext()){
     9             Category c = it.next();
    10             System.out.println(c.getId() + "-" + c.getName());
    11         }
    12         
    13         Iterator<Category> it2 = (Iterator<Category>)session.createQuery("from Category c where c.id < 4").iterate();
    14         while(it2.hasNext()){
    15             Category c = it2.next();
    16             System.out.println(c.getId() + "-" + c.getName());
    17         }
    18         session.getTransaction().commit();
    19     }

    执行结果:

  • 相关阅读:
    leetcode[45]Jump Game II
    leetcode[46]Permutations
    leetcode[47]Permutations II
    leetcode[48]Rotate Image
    手把手一起玩perl安装
    List the Modules in Your System
    oracle之recyclebin
    10g 11g新特性
    RMAN相关操作命令
    手把手一起安装RAC+DataGuard
  • 原文地址:https://www.cnblogs.com/ShawnYang/p/6769833.html
Copyright © 2011-2022 走看看