English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The difference between eager connection and ordinary connection in Hibernate
The relevant introductions and explanations are commented in the code, please refer to them.
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 On multiple ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /** * * Eager left outer join: The feature is: if there are unsatisfactory conditions in the left table, it also returns the left table that does not meet the conditions * 1. The LEFT JOIN FETCH keyword represents an eager left outer join retrieval strategy. * 2. The list() method returns a collection of entity object references, each Department object's associated Employee collection is initialized, * . Stores entity objects of all associated Employees. * 3. Duplicate elements may be included in the query results, which can be filtered out by a HashSet * * De-duplication: * Method one: using distinct * 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()); * * Method two * 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 collection returned by the list() method stores object arrays. * 3. The retrieval strategy for the Employee collection is determined by the configuration file. * 4. If you want the list() method to return a collection that only contains Department objects, * You can use the SELECT keyword in HQL query statements. * * The results of such a statement query may have duplicates: }} * String hql = "FROM Department d LEFT JOIN d.emps"; * Query query = session.createQuery(hql); * * List<Object[]> results = query.list(); * System.out.println(results.size()); * * De-duplication: * Only the distinct method can be used to remove duplicates. * * 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()); } } /** * Eager inner join: The characteristic is that it does not return the left table if it does not meet the conditions. * The INNER JOIN FETCH keyword represents an eager inner join and can also omit the INNER keyword. * The collection returned by the list() method stores references to Department objects, each Department * The Employee collections of the objects are all initialized and store all the associated Employee objects. * * Inner join: * The INNER JOIN keyword represents an inner join and can also omit the INNER keyword. * Each element in the collection of the list() method corresponds to a record in the query result, and each element is of 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() ); } } // ~~~~~~~~~~~~~~~~~~~~~~~~~~The following example is from many to 1 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @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()); } } }
Summary
That is all the detailed explanation of the difference between eager connection and normal connection in Hibernate in this article. I hope it will be helpful to everyone. Interested friends can continue to refer to this site:
A brief discussion on the eager loading problem of Hibernate (multiple foreign key associations)
Code Explanation of Session Create, Delete, Update, and Query Operations in Hibernate
If there are any deficiencies, please leave a message to point them out. Thank you for friends' support to this site!
Statement: The content of this article is from the network, the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)