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     }

    执行结果:

  • 相关阅读:
    lrzsz on linux
    ASP.Net Core 运行在Linux(CentOS)
    ASP.Net Core 运行在Linux(Ubuntu)
    .Net程序跑在Linux上
    通过GitHub部署网站到Azure WebSite
    kubernetes报错
    第4篇创建harbor私有镜像库
    第1篇Kubernetes介绍
    第2篇Kubernetes架构
    第3篇K8S集群部署
  • 原文地址:https://www.cnblogs.com/ShawnYang/p/6769833.html
Copyright © 2011-2022 走看看