HiBernate Qusery Language, if you are already familiar with it, you will find it very similar to SQL. However, you don't be confused by the face, HQL is an object-oriented (OO, looking at every object with life, they are so fresh). If you have some understanding of Java and SQL statements, then HQL is easier to make you feel like you, you can use the time on the bus to master it.
Slowly slowly in depth from several aspects:
1. Sensitive
Everyone knows Query is not sensitive to case, but in HQL (previously mentioned it is OO), then the name and attribute of the object class are sensitive (in accordance with the Java programming syntax).
Such as: select cat.name from cat as cat and select cat.name from cat as cat is the same
but:
Select cat.name from cat as cat and select catch.name from cat as cat is really different.
2. From statement
simplest:
From eg.cat
It is just simply returns an instance of all EG.cat
Usually we will be an individual name for Eg.cat at this time, because in the rest of Query may be used (see the upper side for uppercase
Examples of sensitiveity), such as:
From eg.cat as cat here, AS can be omitted.
The upper side is just a single table query, and the situation of the multi-table is written as follows:
From eg.cat, eg.dog
From eg.cat as cat, eg.dog as dog
3. Join related
(Inner) Join
LEFT (OUTER) JOIN
Right (Outer) Join
Full Join
HQL also supports these features in SQL
In the following, a small topic is inserted, and I haven't used it all the features, I haven't used it, since I said here, I want to
Take the usage of the few features of the upper side, but also a supplement to yourself:
Suppose there are two tables: department, employee, listed below:
EMPLOYEE:
ID Name Depno
001 JPLATEAU 01
002 JONY 01
003 Camel 02
Department:
ID Name
01 R & D Department
02 Marketing Department
In Hibernate, we manipulate the object, so we manipulate the department and employee
1). (Inner) Join
Select Employee.id as ID1, Employee.Name As Name1, Department.id as ID2, Department.Name
AS Name2 from Employee As Employee Join Department As Department on Employee.Depno =
Department.id (notice that the conditional statement I have not used WHERE)
So what is the execution result?
ID1 Name1 ID2 Name2
001 JPLATEAU 01 R & D Department
002 JONY 01 R & D department
2) .left (outside) Join
Select Employee.id As ID1, Employee.Name As Name1, Department.id as ID2, Department.NameAs Name2 from Employee As Employee Left Join Department As Department on Employee.Depno =
Department.id
So what is the execution result?
ID1 Name1 ID2 Name2
001 JPLATEAU 01 R & D Department
002 JONY 01 R & D department
003 Camel Null Null
{That is, I have to have the first record of the first table, and the NULL} is filled when there is no record in the second table.
3). Right (Outer) Join
Select Employee.id as ID1, Employee.Name As Name1, Department.id as ID2, Department.Name
AS Name2 from Employee As Employee Right Join Department As Department on Employee.Depno =
Department.id
So what is the execution result?
ID1 Name1 ID2 Name2
001 JPLATEAU 01 R & D Department
002 JONY 01 R & D department
NULL NULL 02 Marketing Department
{That is, I have to have the second table of records, and fill null} when there is no record in the first table.
4. SELECT statement
That is to determine which objects you have to return from the query or which objects. Write a few examples:
SELECT Employee Form Employee As Employee
Select Employee Form Employee As Employee Employee.name Like 'J%'
SELECT Employee.name form Employee as Employee Where Employee.name Like 'J%'
Select Employee.id as ID1, Employee.Name As Name1, Department.id as ID2, Department.Name
AS Name2 from Employee As Employee Right Join Department As Department on Employee.Depno =
Department.id
SELECT Elements (Employee.name) from Employee As Employee
(I don't understand what Elements is doing it? I hope to explain it)
and many more
5. Mathematical function
JDO does not seem to support this type of feature.
AVG (...), SUM (...), min (...), max (...)
count (*)
Count (...), count (distance ...), count (all ...)
Its usage and SQL basically
Select Distinct Employee.name from Employee As Employee
Select Count (Distinct Employee.Name, Count (Employee) from Employee As Employee
6. Polymorphism (I don't know how to explain it for the time being?)
From com.test.animal as animal
Not only gets all Animal instances, and you can get all animal subclasses (if we define a subclass CAT)
A comparative extreme example
From java.lang.object as o
Examples of all persistent classes can be obtained
7. WHERE statement
Define the conditions for query statements, and take a few examples:
From Employee as Employee WHERE Employee.name = 'JPLATEAU'
From Employee As Employee WHERE Employee.name Like 'J%'
From Employee As Employee WHERE Employee.name Like '% U'
In the where statement, "=" is not light, can compare the properties of the object, or the object, such as:
SELECT Animal from com.test.animal as animal where animal.name = DOG
8. expression
Most of the expressions in the SQL statement can be used in HQL:
Mathematical Operators , -, *, /
Binary Comparison Operators =,> =, <=, <>,! =, LIKE
Logical Operations and, OR, NOT
String concatenation ||
SQL Scalar Functions Like Upper () And Lower ()
Parentheses () Indicate Grouping
IN, Between, IS NULL
JDBC in parameters?
Named Parameters: Name,: start_date,: x1 (this should be another "?" workaround solution)
SQL LITERALS 'FOO', 69, '1970-01-01 10: 00: 01.0'
Java Public Static Final constants eg.color.tabby
Others don't have to explain, here I just want to explain the parameters in the query:
Everyone knows when the passing parameters in SQL are inquiry, we usually use prepaaredStatement, write a lot of "?" In the statement,
This method can also be used in HQL, such as:
List mats = sess.find
"SELECT EMPLOYE.NAME FROM EMPLOYEE AS EMPLOYEE"
"WHERE EMPLOYE.NAME =?",
Name,
Hibernate.string
);
(Description: The above uses the find method in the session, heavy is overloaded in many Find methods in Hibernate's API session, which can meet a variety of forms of queries)
The upper side is the case of a parameter. In this case, the introduction of parameters and definition parameters are introduced. When multiple parameters, another Find method is called, and the latter two parameters are in the form of an array.
There is another method to solve the problem of the upper side, and JDO has such a method, but there is a difference in the form of Hibernate, but they are in both bones.
The same, such as:
Query Q = Sess.createQuery ("Select Employee.name from Employee As Employee Employee.Name =: Name");
Q.setString ("Name", "JPLATEAU");
// When there are multiple parameters, this is a sense of one
Iterator Employees = Q.Istrate ();
9. ORDER statement
There is no difference in the SQL statement, such as:
SELECT Employee.name from Employee As Employee WHERE EMPLOYE.NAME LIKE 'J%' Order by Employee.ID DESC (or ASC)
10. Group by statement
There is no difference in the same and SQL statement, such as:
Select Employee.name, Employee.Depno from Employee As Employee Group by Employee.Depno
Select Foo.ID, AVG (Elements (Foo.Names)), Max (INDICES (Foo.Names)) from eg.foo foo group by foo.id
{NOTE: You May Use the elements and indices constructs inside a select clause, evening databases with no subselects.
Who helped me explain the two sentences, thank you!
11. Subquerice
Hibernate also supports subqueries, writing a few examples:
From eg.cat as fatcat where fatcat.weight> (SELECT AVG (Cat.Weight) from eg.domesticcat cat
Section:
In fact, HQL and SQL are very similar, when you write, you only want to think of the concept of objects, then you can write HQL with SQL's idea.
Reference Resources: Hibernate Reference Manual Seventh chapter and Chapter 9. It is recommended to study the manual carefully by the time of time and to use the Hibernate development project.
Ezerg Programming