The previous article discussed in detail the HQL query in the Entity object query, and at the end of the simple introduction of SQL Native Query, HQL is the object query, so in the Query method and SQL may not be the same, in learning to use HQL only need to understand the different points, so you can quickly get started using HQL. Next, we discuss the connection query and the external named query in HQL's basic query.
One, connection query
In SQL often use the connection query to get a collection of multiple objects, which are often used in the inner join, left joins, right joins, and so on, respectively, refers to the intra-generation connection query, the outer join query, and the outer join query, the content they return in the query is the Cartesian product between the entities, The contents of the query and some contents of the left table, query content and some contents of the right table, the function of query is powerful. The HQL connection Query method and the SQL connection query are the same on the query results, but slightly different on the query statement.
1.1 Internal Connection
HQL INNER JOIN queries can be queried using either the INNER JOIN statement or the JOIN statement, and the resulting set is a Cartesian product. Similar to SQL internal connection query, HQL Join query is divided into explicit and implicit two, the query is displayed refers to the query string has the JOIN keyword, implicit query in the string does not need to add a join.
Inner connection @suppresswarnings ({"Unchecked", "rawtypes"}) public void Testquery () {Session session=null;try{session= Hibernateutils.getsession (); session.begintransaction ();//Returns the result set attribute list, the element type and the attribute type in the entity class are consistent list students= Session.createquery ("Select S.name,c.name from Student s joins s.classes C"). List (); for (Iterator Ite=students.iterator ( ); Ite.hasnext ();) {object[] obj= (object[]) ite.next (); System.out.println (Obj[0]);} Session.gettransaction (). commit ();} catch (Exception e) {e.printstacktrace (); Session.gettransaction (). rollback (); Finally{hibernateutils.closesession (session);}}
1.2 External Connection
Outer joins are divided into the left outer connection and the right outer connection query, the query method is similar, but the query result set is different, they in the query result and the SQL outside joins the same, different is the writing, the concrete use code as follows:
@SuppressWarnings ({"Unchecked", "rawtypes"}) public void Testquery () {Session session=null;try{session= Hibernateutils.getsession (); session.begintransaction ();//Returns the result set attribute list, the element type and the attribute type in the entity class are consistent list students= Session.createquery ("Select S.name,c.name from Student s left joins s.classes C"). List (); for (Iterator ite= Students.iterator (); Ite.hasnext ();) {object[] obj= (object[]) ite.next (); System.out.println (Obj[0]);} Session.gettransaction (). commit ();} catch (Exception e) {e.printstacktrace (); Session.gettransaction (). rollback (); Finally{hibernateutils.closesession (session);}}
The above code uses the LEFT OUTER JOIN query statement, the corresponding right outer connection query method and the left outer join similar to the same, will be converted to "." The queried data is saved to the list object, which can be used to get the contents of the query through list.
second, external named query
An external named query refers to writing a query statement into a mapping file, using <query> tags in the mapping file to define the HQL statement, so that the defined HQL statement can implement the function configuration function, if there is a problem only need to modify the configuration. If you want to use this SQL statement, you can use the Session.getnamedquery () method in your program to get the HQL query string, as in the following example.
2.1 External Query Statements The following example demonstrates the application of an external query statement, adds a <query> tag to the mapping file, adds a Name property to the label, and adds a string to the <! [cdata[]]>, this allows the corresponding query string to be obtained in the program according to the Name property of query.
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">
The external named query put the query into the mapping file, so it can be considered a common query string, in the program file can query the use of the string, which is its advantage, so that other program files can be used, and as a common string added to the convenience of modification.
2.2 Program Application
After defining the external query statement to be used in the program, HQL provides the Getnamequery method to get an external query string, which needs to add an external cursor name, HQL based on the cursor name query to obtain the corresponding SQL statement block, such as the following program code:
External named query @suppresswarnings ({"Unchecked", "rawtypes"}) public void Testquery () {Session session=null;try{session= Hibernateutils.getsession (); Session.begintransaction (); List Students=session.getnamedquery ("Querystudent"). Setparameter (0, ten). List (); for (Iterator Ite=students.iterator (); Ite.hasnext ();) {Student obj= (Student) ite.next (); System.out.println (Obj.getname ());} Session.gettransaction (). commit ();} catch (Exception e) {e.printstacktrace (); Session.gettransaction (). rollback (); Finally{hibernateutils.closesession (session);}}
Conclusion
The HQL connection Query method and the external named Query method are discussed, the HQL connection Query method is similar to the SQL connection query, it can be considered as a kind of SQL wrapper, the query is quite simple, and the external named query can be seen as defining a common query string, This string can be used by program files.