hibernate 1 + N 問題解決

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

1+N就是在hibernate中第一次查一個所需要的表的內容,他會把別的相關表的內容也查詢一遍。

 

解決辦法有三種:

1,設定LAZY。

2,借鑑createCriteria的查詢語句,from Topic t left join fetch t.category c,通過join fetch來遮蔽多於查詢。

3,將多次查詢整成一次查詢。給多於表加上BatchSize註解。這種方法並沒有解決問題,只是相對來說優化了一點。

 

解決辦法3的寫法:

package com.bjsxt.hibernate;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

import org.hibernate.annotations.BatchSize;

@Entity
//@BatchSize(size=5)//加上BachtSize註解,如此,一次會查詢五條相關內容
public class Category {
    private int id;
    private String name;
    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

 

 

 

測試類:

import java.util.Date;
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();
    }
   
    //N+1
    @Test
    public void testQuery1() {
        Session session = sf.openSession();
        session.beginTransaction();
        //List<Topic> topics = (List<Topic>)session.createCriteria(Topic.class).list();
        List<Topic> topics = (List<Topic>)session.createQuery("from Topic").list();
                    
       
        for(Topic t : topics) {
            System.out.println(t.getId() + "-" + t.getTitle());
        }
        session.getTransaction().commit();
        session.close();
       
    }
   
    @Test
    public void testQuery2() {
        Session session = sf.openSession();
        session.beginTransaction();
        //List<Topic> topics = (List<Topic>)session.createCriteria(Topic.class).list();//關聯關係不設lazy也不會全部查詢,因為使用的是關聯表方式
        List<Topic> topics = (List<Topic>)session.createQuery("from Topic").list();//關聯關係不設lazy會全部查詢
                    
       
        for(Topic t : topics) {
            System.out.println(t.getId() + "-" + t.getTitle());
            System.out.println(t.getCategory().getName());
        }
        session.getTransaction().commit();
        session.close();
       
    }
   
    //@BatchSize
    @Test
    public void testQuery3() {
        Session session = sf.openSession();
        session.beginTransaction();
        //List<Topic> topics = (List<Topic>)session.createCriteria(Topic.class).list();
        List<Topic> topics = (List<Topic>)session.createQuery("from Topic").list();
       
        for(Topic t : topics) {
            System.out.println(t.getId() + "-" + t.getTitle());
            System.out.println(t.getCategory().getName());
        }
        session.getTransaction().commit();
        session.close();
       
    }
    //join fetch
    @Test
    public void testQuery4() {
        Session session = sf.openSession();
        session.beginTransaction();
        //List<Topic> topics = (List<Topic>)session.createCriteria(Topic.class).list();
        List<Topic> topics = (List<Topic>)session.createQuery("from Topic t left join fetch t.category c").list();
       
        for(Topic t : topics) {
            System.out.println(t.getId() + "-" + t.getTitle());
            System.out.println(t.getCategory().getName());
        }
        session.getTransaction().commit();
        session.close();
       
    }
    public static void main(String[] args) {
        beforeClass();
    }
}

相關文章