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 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/**
*
* Urgent LEFT outer connection: The characteristic is: if the left table has not satisfied the condition, also returns the left table does not satisfy the condition
* 1. The left JOIN FETCH keyword indicates an urgent right-side connection retrieval strategy.
* 2. The list () method returns a reference to the collection that holds the entity object, and the Employee collection associated with each Department object is initialized.
* The entity object that holds all the associated Employee.
* 3. Duplicate elements may be included in query results, and duplicate elements can be filtered through a HashSet
*
* To Weight:
* Method One: Use 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 connection:
* 1. The LEFT JOIN keyword indicates an out-of-the-field connection query.
* 2. The collection returned by the list () method holds the object array type
* 3. Determines the retrieval strategy for the Employee collection based on the configuration file.
* 4. If you want the list () method to return a collection that contains only the Department object,
* The SELECT keyword can be used in HQL query statements
*
* The result of such a statement query is duplicated:
* String hql = "from Department D left JOIN d.emps";
* Query query = session.createquery (HQL);
*
* list<object[]> results = query.list ();
* SYSTEM.OUT.PRINTLN (Results.size ());
*
* To Weight:
* Only use the distinct method 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 ());
}
}
/**
* Urgent Internal connection: The feature is: Do not return to the left table does not meet the conditions
* The INNER join FETCH keyword indicates an urgent connection, or you can omit the INNER keyword
* the list () method returns a reference to the collection that holds the Department object, each Department
* The employee collection of the object is initialized to hold all associated employee objects
*
* Internal connection:
* The INNER join keyword indicates an inner join, or you can omit the INNER keyword
* Each element in the collection of the list () method corresponds to a record of the query result, each element is an object array type
* If you want the returned collection of the list () method to contain only the Department object, 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 a multi-pair 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 ());
}
}
}
Hibernate the difference between an urgent connection and a normal connection