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();
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值