Hibernate The difference between urgent connection and ordinary connection
The relevant introduction and explanation have been commented in the code, so you can refer to it.
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();}// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The following example is from 1 To many~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~/** * * Erection left outer join: Features are: If the left table does not meet the conditions, the left table does not meet the conditions* 1. The LEFT JOIN FETCH keyword represents the urgent left outer join retrieval strategy. * 2. The list() method stores references to entity objects in the set returned. The Employee collection associated with each Department object is initialized, * Stores all the associated entity objects. * 3. The query result may contain duplicate elements, and duplicate elements can be filtered through a HashSet* * Deduplication: * Method 1: Use distinct * String hql = "SELECT DISTINCT d FROM Department d LEFT JOIN FETCH d.emps "; * Query query = session.createQuery(hql); * * List<Department> depths = query.list(); * System.out.println(depts.size()); * * Method 2* String hql = "FROM Department d LEFT JOIN FETCH d.emps "; * Query query = session.createQuery(hql); * * List<Department> 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<Department> depts = query.list();// System.out.println(depts.size());// String hql = "FROM Department d LEFT JOIN FETCH d.emps ";Query query = session.createQuery(hql);List<Department> 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() );}}/** * Left outer join: * 1. The LEFT JOIN keyword represents a left outer join query. * 2. The object array type is stored in the collection returned by the list() method * 3. Determine the search strategy of the Employee collection based on the configuration file. * 4. If you want the collection returned by the list() method to contain only Department objects, * You can use the SELECT keyword * * in HQL query statements to query the results of such statements: * String hql = "FROM Department d LEFT JOIN d.emps"; * Query query = session.createQuery(hql); * * List<Object[]> results = query.list(); * System.out.println(results.size()); * * Deduplication: * Only use distinct method to remove duplication* * String hql = "SELECT DISTINCT d FROM Department d LEFT JOIN d.emps"; * Query query = session.createQuery(hql); * * List<Department> depts = query.list(); * System.out.println(depts.size()); * * for(Department dept:depts){ * System.out.println(dept.getName() + dept.getEmps().size()); * } * */@Test public void testLeftJoin(){String hql = "SELECT DISTINCT d FROM Department d LEFT JOIN d.emps";Query query = session.createQuery(hql);List<Department> depts = query.list();System.out.println(depts.size());for (Department dept:depts){System.out.println(dept.getName() + dept.getEmps().size());}}/** * Impulsive internal connection: Features are: Do not return the left table but does not meet the conditions* INNER JOIN FETCH keyword represents an urgent internal connection, and the INNER keyword can also be omitted * The list() method stores Department in the collection returned by the collection Reference to objects, each Department * The Employee collection of the object is initialized, storing all associated Employee objects* * Inner connections: * INNER JOIN keyword represents an internal connection, and the INNER keyword can also be omitted. A record of the query result of each element stored in the list() method set is the object array type. Each element is the object array type* If you want the collection returned by the list() method to only contain Department objects, you can use the SELECT keyword in the HQL query statement* * * */@Test public void testInnerJoinFetch(){//String hql = "SELECT DISTINCT d FROM Department d LEFT JOIN FETCH d.emps ";String hql = "FROM Department d INNER JOIN FETCH d.emps ";Query query = session.createQuery(hql);List<Department> 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 testLeftJoinFetch2(){String hql = "FROM Employee e LEFT JOIN FETCH e.dept";Query query = session.createQuery(hql);List<Employee> emps = query.list();System.out.println(emps.size());for (Employee emp:emps){System.out.println(emp + " -- " + emp.getDept());}}}Summarize
The above is the full content of this article on the difference between Hibernate urgent connection and ordinary connection. I hope it will be helpful to everyone. Interested friends can continue to refer to this site:
A brief discussion on the urgent loading problem of hibernate (multiple foreign key association)
Detailed explanation of the operation code for adding, deleting, modifying and searching in Hibernate
If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!