Hibernate 迫切连接和普通连接的区别
相关的介绍和解释在代码中已注释,大家可以参考。
package com.baidu.test;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.baidu.leftJoin.Department;
import com.baidu.leftJoin.Employee;
public class TestHQL_LeftJoin {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
@Before
public void init(){
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
.applySettings(configuration.getProperties())
.buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
session = sessionFactory.openSession();
transaction = session.beginTransaction();
}
@After
public void destroy(){
transaction.commit();
session.close();
sessionFactory.close();
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~下面的例子是 从 1 对 多 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/**
*
* 迫切左外连接: 特点是:如果左表有不满足条件的,也返回左表不满足条件
* 1. LEFT JOIN FETCH 关键字表示迫切左外连接检索策略.
* 2. list() 方法返回的集合中存放实体对象的引用, 每个 Department 对象关联的 Employee 集合都被初始化,
* 存放所有关联的 Employee 的实体对象.
* 3. 查询结果中可能会包含重复元素, 可以通过一个 HashSet 来过滤重复元素
*
* 去重:
* 方法一:使用 distinct
* String hql = "SELECT DISTINCT d FROM Department d LEFT JOIN FETCH d.emps ";
* Query query = session.createQuery(hql);
*
* List depts = query.list();
* System.out.println(depts.size());
*
* 方法二
* String hql = "FROM Department d LEFT JOIN FETCH d.emps ";
* Query query = session.createQuery(hql);
*
* List depts = query.list();
*
* depts = new ArrayList<>(new LinkedHashSet(depts));
* System.out.println(depts.size());
*
* for(Department dept:depts){
* System.out.println(dept.getName() + "--" + dept.getEmps().size() );
* }
*
*
*/
@Test
public void testLeftJoinFetch(){
// String hql = "SELECT DISTINCT d FROM Department d LEFT JOIN FETCH d.emps ";
// Query query = session.createQuery(hql);
//
// List depts = query.list();
// System.out.println(depts.size());
//
String hql = "FROM Department d LEFT JOIN FETCH d.emps ";
Query query = session.createQuery(hql);
List depts = query.list();
System.out.println(depts.size());
depts = new ArrayList<>(new LinkedHashSet(depts));
System.out.println(depts.size());
for (Department dept:depts){
System.out.println(dept.getName() + "--" + dept.getEmps().size() );
}
}
/**
* 左外连接:
* 1. LEFT JOIN 关键字表示左外连接查询.
* 2. list() 方法返回的集合中存放的是对象数组类型
* 3. 根据配置文件来决定 Employee 集合的检索策略.
* 4. 如果希望 list() 方法返回的集合中仅包含 Department 对象,
* 可以在HQL 查询语句中使用 SELECT 关键字
*
* 这样的语句查询的结果有重复:
* String hql = "FROM Department d LEFT JOIN d.emps";
* Query query = session.createQuery(hql);
*
* List