HIBERNATE-HQL Query
HQL is the acronym for Hibernate Query Language, an object-oriented query language and the most common query language in hibernate.
(i) Basic grammar of HQL
|
|
Select "Property name" from "Object" where "condition" Group BY "attribute name" having "grouping condition" Order By "Property name" Desc/asc |
|
Precautions
- Case sensitive
- Try to avoid using DML statements to manipulate data
Example
Select *from User u wher u.id>10 order u.id desc;//The mind must have the concept of object
(ii) entity objects and dynamically instantiated object queries
1) entity object Query
String ExecuteSQL = "from User";
Note: You do not need to add the SELECT keyword
2) Dynamic Instantiation of object queries
Requirement: What can we do if we don't need to query all the field properties of an object in the actual case?
Select ID, name from user;
Analysis:
The HQL statement above only queries the id,name of the user object for the two fields,
But in Hibernate, the second statement returns an array of object, and he loses the original state of the objects,
Solution Solutions
Select new User (id,name) from user;
Conditional queries and the use of others
Implementing conditional queries in HQL with a WHERE clause
From User where id = 1;
HQL provides methods for object aliases
From User u whre u.id = 1;
From User as u wher u.id = 1;//Both methods are possible, and I personally feel the use of the first kind.
(iii) Dynamic Query of HQL statements
1, in JDBC Programming, PreparedStatement objects are convenient for development, not only to dynamically assign SQL statements but also to avoid SQL injection attacks. SQL caching technology is also used to improve the efficiency of SQL execution.
2, HQL also uses a similar approach.
1) "?" Delegate parameters
This approach is very similar to preparestatement, and is assigned through the method Setparameter () of the query object.
Case:
String hql = "from User u where u.id=?"; Query query = session.createquery (HQL); Query. = Query.setparameter (0,3); List = Query.list (); |
|
Custom parameter names
This method is also assigned through the Setparameter method of the query object, but the dynamic parameters in the HQL statement can be customized by using the ":" Number and the method of defining the parameter combination.
String hql = "from User u where u.id =:userid"; Query query = Session.createquery (); Qery = Query.setparameter ("UserId", 3); List = Query.list (); |
|
(d) object navigation query
The relational model establishes the relationship between two tables through the primary foreign key, while the object-oriented model is a reference
Such as
public class Student {
Privateteacher teacher = null;
}
HQL queries conform to the habit of Java programmers, and when an object has a dependency on another object, it can be passed "." Symbol to navigate.
Session = Hibernateutil.getsession (); Transaction = Session.begintransaction (); String hql = "from Medicine m where m.category.name=?"; Query query = session.createquery (HQL). Setparameter (0, "cold"); list<medicine> list = Query.list (); for (Medicine m:list) { System.out.println ("Cold goods" +m.getname ()); } Transaction.commit (); |
|
(v) sort Query
From the User u order by ID ASC;//Ascending From the User u order by id desc;//Descending |
|
(vi)
(vii) aggregation function
Select count () from User u;
From the User u order by ID ASC;//Ascending From the User u order by id desc;//Descending |
|
Select Max (u.id) from User u;
(eight) grouping Operations
Grouping by using the GROUP BY clause in HQL
|
|
Session = Hibernateutil.getsession (); Session.begintransaction (); String hql = "Select U.dept, COUNT (*) from the User u group by u.dept"; Query query = session.createquery (HQL); list<object> list = Query.list (); For (object[] obj;list) { System.out.println ..... } Transaction.commit (); |
|
(ix) sub- query
|
|
Session = Hibernateutil.getsession (); Transaction = Session.begintransaction (); String hql = "Select Med.name from Medicine med where med.price" (select Max, from Medicine) "; list<string> list = Session.createquery (HQL). List (); for (String name:list) { System.out.println ("The most expensive medicine is" +name); } Transaction.commit (); |
|
HIBERNATE-HQL Query