Hibernate- 连接查询

01.搭建开发环境

02.连接查询

package com.gordon.test;

import java.util.Arrays;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

import com.gordon.domain.Book;
import com.gordon.domain.Publisher;
import com.gordon.utils.HibernateUtil;

/**
 * 连接查询
 * @author Administrator
 */
public class TestDemo5 {
	
	/**
	 * 连接查询-右外连接
	 * 查询结果
		Hibernate: 
		    select
		        book0_.id as id1_0_0_,
		        publisher1_.id as id1_1_1_,
		        book0_.name as name2_0_0_,
		        book0_.price as price3_0_0_,
		        book0_.publisher_id as publishe4_0_0_,
		        publisher1_.name as name2_1_1_ 
		    from
		        t_book book0_ 
		    left outer join
		        t_publisher publisher1_ 
		            on book0_.publisher_id=publisher1_.id
		云计算技术及性能优化
		---电子工业出版社
		C语言程序设计
		---电子工业出版社
		中国冰雪梦
		---电子工业出版社
		Photoshop图形图像处理
		---北京大学出版社
		VisualBasic2015实践教程 
		---北京大学出版社
		生产微服务 
		---人民邮电出版社
		架构探险:轻量级微服务架构(下册)
		---人民邮电出版社
	 */
	@Test
	public void run4() {
		Session session = HibernateUtil.getCurrentSession();
		Transaction transaction = session.beginTransaction();
		
		String hql = "from Book b right join b.publisher p";
		Query query = session.createQuery(hql);
		
		
		Book b = null;
		Publisher p = null;
		
		List<Object[]> list = query.list();
		for (Object[] book : list) {
			
			b = (Book) book[0];
			p = (Publisher) book[1];
			
			System.out.println(b.getName());
			System.out.println("---" + p.getName());
		}
		
		transaction.commit();
	}
	
	/**
	 * 连接查询-左外连接
	 * 查询结果
		Hibernate: 
		    select
		        book0_.id as id1_0_0_,
		        publisher1_.id as id1_1_1_,
		        book0_.name as name2_0_0_,
		        book0_.price as price3_0_0_,
		        book0_.publisher_id as publishe4_0_0_,
		        publisher1_.name as name2_1_1_ 
		    from
		        t_book book0_ 
		    left outer join
		        t_publisher publisher1_ 
		            on book0_.publisher_id=publisher1_.id
		云计算技术及性能优化
		---电子工业出版社
		C语言程序设计
		---电子工业出版社
		中国冰雪梦
		---电子工业出版社
		Photoshop图形图像处理
		---北京大学出版社
		VisualBasic2015实践教程 
		---北京大学出版社
		生产微服务 
		---人民邮电出版社
		架构探险:轻量级微服务架构(下册)
		---人民邮电出版社
	 */
	@Test
	public void run3() {
		Session session = HibernateUtil.getCurrentSession();
		Transaction transaction = session.beginTransaction();
		
		String hql = "from Book b left join b.publisher p";
		Query query = session.createQuery(hql);
		
		
		Book b = null;
		Publisher p = null;
		
		List<Object[]> list = query.list();
		for (Object[] book : list) {
			
			b = (Book) book[0];
			p = (Publisher) book[1];
			
			System.out.println(b.getName());
			System.out.println("---" + p.getName());
		}
		
		transaction.commit();
	}
	
	/**
	 * 连接查询-隐式内连接
	 * 查询结果
		Hibernate: 
		    select
		        book0_.id as id1_0_0_,
		        publisher1_.id as id1_1_1_,
		        book0_.name as name2_0_0_,
		        book0_.price as price3_0_0_,
		        book0_.publisher_id as publishe4_0_0_,
		        publisher1_.name as name2_1_1_ 
		    from
		        t_book book0_ cross 
		    join
		        t_publisher publisher1_ 
		    where
		        book0_.publisher_id=publisher1_.id
		云计算技术及性能优化
		---电子工业出版社
		C语言程序设计
		---电子工业出版社
		中国冰雪梦
		---电子工业出版社
		Photoshop图形图像处理
		---北京大学出版社
		VisualBasic2015实践教程 
		---北京大学出版社
		生产微服务 
		---人民邮电出版社
		架构探险:轻量级微服务架构(下册)
		---人民邮电出版社
	 */
	@Test
	public void run2() {
		Session session = HibernateUtil.getCurrentSession();
		Transaction transaction = session.beginTransaction();
		
		String hql = "from Book b, Publisher p where b.publisher = p";
		Query query = session.createQuery(hql);
		
		
		Book b = null;
		Publisher p = null;
		
		List<Object[]> list = query.list();
		for (Object[] book : list) {
			
			b = (Book) book[0];
			p = (Publisher) book[1];
			
			System.out.println(b.getName());
			System.out.println("---" + p.getName());
		}
		
		transaction.commit();
	}
	
	/**
	 * 连接查询-显示内连接
	 * 查询结果
		Hibernate: 
		    select
		        book0_.id as id1_0_0_,
		        publisher1_.id as id1_1_1_,
		        book0_.name as name2_0_0_,
		        book0_.price as price3_0_0_,
		        book0_.publisher_id as publishe4_0_0_,
		        publisher1_.name as name2_1_1_ 
		    from
		        t_book book0_ 
		    inner join
		        t_publisher publisher1_ 
		            on book0_.publisher_id=publisher1_.id
		云计算技术及性能优化
		---电子工业出版社
		C语言程序设计
		---电子工业出版社
		中国冰雪梦
		---电子工业出版社
		Photoshop图形图像处理
		---北京大学出版社
		VisualBasic2015实践教程 
		---北京大学出版社
		生产微服务 
		---人民邮电出版社
		架构探险:轻量级微服务架构(下册)
		---人民邮电出版社
	 */
	@Test
	public void run1() {
		Session session = HibernateUtil.getCurrentSession();
		Transaction transaction = session.beginTransaction();
	
		String hql = "from Book b inner join b.publisher";
		Query query = session.createQuery(hql);
		
		
		Book b = null;
		Publisher p = null;
		
		List<Object[]> list = query.list();
		for (Object[] book : list) {
			
			b = (Book) book[0];
			p = (Publisher) book[1];
			
			System.out.println(b.getName());
			System.out.println("---" + p.getName());
		}
		
		transaction.commit();
	}
}

 

posted @ 2017-08-24 09:43  GordonDicaprio  阅读(327)  评论(0编辑  收藏  举报