Hibernateの中の1+N問題


Hibernateの1+N問題
1、Category類
package com.edu.hpu;

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

@Entity
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;
	}
}

2、Topic類
package com.edu.hpu;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

@Entity
public class Topic {

	private int id;
	private String title;
	private Date date;
	private Category category;
	
	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
	
	@ManyToOne
	@JoinColumn(name="category_ID")
	public Category getCategory() {
		return category;
	}
	public void setCategory(Category category) {
		this.category = category;
	}
}

3、テストクラス
package com.edu.hpu;

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

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class TestClass {

	private static SessionFactory sf = null;
	
	@BeforeClass
	public static void beforeClass() {
		Configuration conf = new Configuration().configure();
		ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(conf.getProperties()).buildServiceRegistry();
		sf = conf.buildSessionFactory(sr);
	}
	
	@Test
	public void testExport() {
		new SchemaExport(new Configuration().configure()).create(true ,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.setTitle("t" + i);
			t.setDate(new Date());
			t.setCategory(c);
			session.save(c);
			session.save(t);
		}
		
		session.getTransaction().commit();
		session.close();
	}
	
	@Test
	public void testQL_1() {
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		List<Topic> ts = (List<Topic>)s.createQuery("from Topic").list();
		for(Topic t : ts) {
			System.out.println(t.getId() + " " + t.getTitle());
			//System.out.println(t.getCategory().getId() + t.getCategory().getName());
		}
		s.getTransaction().commit();
	}
	
	@AfterClass
	public static void afterClass() {
		sf.close();
	}
}

CategoryのFetchTypeはEAGERなので、Topicを1つ取るたびに該当するCategoryを取り出してTopic数に等しいSQL文を発行し、実行速度に影響します.
4、解決方法
  • 第1種:TopicのFetchTypeをLAZYに変更し、1つを使用するたびにSQL文
  • を発行する
  • 第2種:Categoryクラスに注記@BatchSize(size=5)を追加し、5本のCategoryレコード
  • を一度に取り出す
  • 第3種:join fetch
  • を使用
    @Test
    public void testQL_2() {
    	Session s = sf.getCurrentSession();
    	s.beginTransaction();
    	List<Topic> ts = (List<Topic>)s.createQuery("from Topic t left outer join fetch t.category c").list();
    	for(Topic t : ts) {
    		System.out.println(t.getId() + " " + t.getTitle());
    	}
    	s.getTransaction().commit();
    }