hibernate list和iterate

劍握在手發表於2013-12-20

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();
    }
}

相關文章