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

    list方法会一次查出所有内容,放在list里和缓存中。再次查询同一内容仍然会去数据库重新查一遍,并刷新缓存。

    iterate方法会一次查出所有内容的ID,等用到某个ID对应的内容时又会去根据主键查询内容,并放在缓存中,当再次查询相同的内容时不再查询数据库,直接调用session级缓存。

     

    import java.util.Date;
    import java.util.Iterator;
    import java.util.List;

    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.AnnotationConfiguration;
    import org.hibernate.tool.hbm2ddl.SchemaExport;
    import org.junit.AfterClass;
    import org.junit.BeforeClass;
    import org.junit.Test;

    public class HibernateQLTest {
        private static SessionFactory sf;
       
        @BeforeClass
        public static void beforeClass() {
            sf = new AnnotationConfiguration().configure().buildSessionFactory();
        }
        @AfterClass
        public static void afterClass() {
            sf.close();
        }
       
        @Test
        public void testSchemaExport() {
            new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);
        }
       
        @Test
        public void testSave() {
            Session session = sf.openSession();
            session.beginTransaction();
           
            for(int i=0; i<10; i++) {
                Category c = new Category();
                c.setName("c" + i);
                Topic t = new Topic();
                t.setCategory(c);
                t.setTitle("t" + i);
                t.setCreateDate(new Date());
                session.save(c);
                session.save(t);
            }
               
            session.getTransaction().commit();
            session.close();
        }
       
        //join fetch
        @Test
        public void testQueryList() {
            Session session = sf.openSession();
            session.beginTransaction();
            //List<Topic> topics = (List<Topic>)session.createCriteria(Topic.class).list();
            List<Category> categories = (List<Category>)session.createQuery("from Category").list();
           
            for(Category c : categories) {
                System.out.println(c.getName());
            }
           
            List<Category> categories2 = (List<Category>)session.createQuery("from Category").list();
            for(Category c : categories2) {
                System.out.println(c.getName());
            }
            session.getTransaction().commit();
            session.close();
           
        }
       
        @Test
        public void testQueryIterate() {
            Session session = sf.openSession();
            session.beginTransaction();
            //List<Topic> topics = (List<Topic>)session.createCriteria(Topic.class).list();
            Iterator<Category> categories = (Iterator<Category>)session.createQuery("from Category").iterate();
           
           
            while(categories.hasNext()) {
                Category c = categories.next();
                System.out.println(c.getName());
            }
           
            Iterator<Category> categories2 = (Iterator<Category>)session.createQuery("from Category").iterate();
           
            while(categories2.hasNext()) {
                Category c = categories2.next();
                System.out.println(c.getName());
            }
            session.getTransaction().commit();
            session.close();
           
        }
        public static void main(String[] args) {
            beforeClass();
        }
    }

  • 相关阅读:
    来自ebay内部的「软件测试」学习资料,覆盖GUI、API自动化、代码级测试及性能测试等,Python等,拿走不谢!...
    2000字谏言,给那些想学Python的人,建议收藏后细看!
    月薪20k+的测试工程师都会这项技能!
    Umbraco中的Member登录时的Lock out功能
    ASP.NET MVC中Session以及处理方式
    ASP.NET MVC 前端(View)向后端(Controller)中传值
    ASP.NET MVC程序传值方式:ViewData,ViewBag,TempData和Session
    Google中rel="canonical"的相关解释和用法
    虚拟攻防系统 HoneyPot
    JS一定要放在Body的最底部么? 聊聊浏览器的渲染机制
  • 原文地址:https://www.cnblogs.com/flying607/p/3483294.html
Copyright © 2011-2022 走看看