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的时候就会取出相应的Category会发出与Topic数目相等的SQL语句,影响执行速度。
4、 解决方法
  • 第一种:将Topic的FetchType调成LAZY,每当要用到一个的时候就发出一条SQL语句
  • 第二种:在Category类上添加注解@BatchSize(size=5),一次性取出5条Category记录
  • 第三种:使用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();
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值