hibernate left join fetch 使用方法解决重复数据

package com.demo.mode;

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

/**
 * User generated by MyEclipse Persistence Tools
 */
@SuppressWarnings("serial")
public class User implements java.io.Serializable {

	// Fields

	private Integer id;

	private String name;

	private Set userBooks = new HashSet(0);

	// Constructors

	/** default constructor */
	public User() {
	}

	/** minimal constructor */
	public User(String name) {
		this.name = name;
	}

	/** full constructor */
	public User(String name, Set userBooks) {
		this.name = name;
		this.userBooks = userBooks;
	}

	// Property accessors

	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Set getUserBooks() {
		return this.userBooks;
	}

	public void setUserBooks(Set userBooks) {
		this.userBooks = userBooks;
	}

}


 

package com.demo.mode;

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

/**
 * Books generated by MyEclipse Persistence Tools
 */

@SuppressWarnings("serial")
public class Books implements java.io.Serializable {

	// Fields

	private Integer id;

	private String name;

	private Set userBooks = new HashSet(0);

	// Constructors

	/** default constructor */
	public Books() {
	}

	/** minimal constructor */
	public Books(String name) {
		this.name = name;
	}

	/** full constructor */
	public Books(String name, Set userBooks) {
		this.name = name;
		this.userBooks = userBooks;
	}

	// Property accessors

	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Set getUserBooks() {
		return this.userBooks;
	}

	public void setUserBooks(Set userBooks) {
		this.userBooks = userBooks;
	}

}


 

package com.demo.mode;

/**
 * UserBook generated by MyEclipse Persistence Tools
 */
@SuppressWarnings("serial")
public class UserBook implements java.io.Serializable {

	// Fields

	private Integer id;

	private User user;

	private Books books;

	// Constructors

	/** default constructor */
	public UserBook() {
	}

	/** full constructor */
	public UserBook(User user, Books books) {
		this.user = user;
		this.books = books;
	}

	// Property accessors

	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public User getUser() {
		return this.user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Books getBooks() {
		return this.books;
	}

	public void setBooks(Books books) {
		this.books = books;
	}

}


 

User 和 Books是多对多关系,一般情况需要尽量避免多对多关系的存在,所以拆分为双向一对多 加一个中间表user_book

如果我想知道ID为1的User所有的book信息如果做呢?

关闭延迟加载?那么如果book有1000条数据,就会出现1+1000条数据库查询,效率极其让人蛋疼!

left join fetch 来实现可以将当前对象的子集 一次加载进去!并返回当前对象!

 

package com.demo.test;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.demo.dao.HibernateDao;
import com.demo.mode.User;
import com.demo.mode.UserBook;

public class HibernateTest {

	/**
	 * @param args
	 */
	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		
		ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
		HibernateDao dao = (HibernateDao) app.getBean("hibernateDao");
		
		List<User> users = (List<User>) dao.find("select distinct u from User u left join fetch u.userBooks as b left join fetch b.books where u.id=?", new Object[]{1});
		for(User user : users){
                         System.out.println("用户名:"+user.getName);
                         Iterator<Books> it = user.getUserBooks. iterator();
              while(it.hasNext()){
                 Books book = it.next();
                 System.out.println("当前用户所以书:"+book.getName());
              }
                  }		
              }
	}

}


打印结果

log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
Hibernate: select user0_.id as id1_0_, userbooks1_.id as id2_1_, books2_.id as id0_2_, user0_.name as name1_0_, userbooks1_.user as user2_1_, userbooks1_.book as book2_1_, userbooks1_.user as user0__, userbooks1_.id as id0__, books2_.name as name0_2_ from 800best.user user0_ left outer join 800best.user_book userbooks1_ on user0_.id=userbooks1_.user left outer join 800best.books books2_ on userbooks1_.book=books2_.id where user0_.id=?
用户名:xiaomaha
当前用户所以书:spring
当前用户所以书:struts
当前用户所以书:j2ee
当前用户所以书:hibernate
当前用户所以书:j2se


 

我做测试 user id为1的book有5条记录 如果用懒加载方式至少生成6条SQL

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值