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 feature is: if The left table has not satisfied the condition, also returns the left table does not meet 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, holding the entity object for all associated employee. * 3. The query results may contain duplicate elements that can be filtered by a HashSet to filter the repeating elements * * deduplication: * 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 LEF
T 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 list () method returns a collection that holds the object array type * 3. UnderConfiguration file to determine the retrieval policy for the Employee collection. * 4. If you want the list () method to return only the collection that contains the Department object, * You can use the SELECT keyword in the HQL query statement * * The result of 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 ()); * * Go to Weight: * Can only use 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 * INNER Join FETCH keyword indicates an urgent internal connection, you can also omit the INNER keyword * List () method returned by the collection of storage depart ment object references, each Department * object's employee collection is initialized to hold all associated employee objects * Within the connection: * The INNER join keyword indicates an inner join, or you can omit INNER keyword * List () The collection of methods for each element that 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 Department objects, you can use them in HQL query statements The SELECT keyword * * */@Test public void Testinnerjoinfetch () {//string hql = "Select DISTINCT d from Departm
Ent 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 multipleFor 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 ()); }
}
}