Hibernate query Statement--HQL

Source: Internet
Author: User
Tags mathematical functions null null

1. From
1.1 Single Table Query
From Eg.cat as Cat. Among them, cat is only an alias, in order to use other child statements when writing simple
1.2 Multi-Table query
From eg. Cat,eg. Dog
From eg. Cat as Cat,eg. Dog as Dog

2. Join related
(inner) Join
Left (outer) join
Right (outer) join
Full Join
HQL also supports these features in SQL
Here is a small topic, about the characteristics of the above, I have not how to use, today, since I said here, I would like to use a few of the characteristics of the above, but also to their own a supplement:
Let's say there are two tables: departments, employees, and some data:
Employee (Employee):
ID Name Depno
001 Jplateau 01
002 Jony 01
003 Camel 02
Department (Department):
ID Name
01 Research and Development Department
02 Marketing Department
We manipulate objects in the hibernate, so we manipulate departmental and employee classes.
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 (Note that the conditional statement I use on does not use where)
So what is the result of the execution?
ID1 name1 Id2 name2
++++++++++++++++++++++++++++++++++++++
001 Jplateau 01 Research and Development Department
002 Jony 01 Research and Development Department

2). Left (outer) join
Select Employee.id as Id1,employee. Name as name1,department.id as id2,department. Name as name2 from employee as employee left join Department as Department on employee. Depno=department.id
So what is the result of the execution?
ID1 name1 Id2 name2
++++++++++++++++++++++++++++++++++++++
001 Jplateau 01 Research and Development Department
002 Jony 01 Research and Development Department
003 Camel NULL NULL
That means I want to have the record number of the first table and then fill in null when there is no corresponding 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 result of the execution?
ID1 name1 Id2 name2
++++++++++++++++++++++++++++++++++++++
001 Jplateau 01 Research and Development Department
002 Jony 01 Research and Development Department
NUL null 02 Marketing Department
So I'm going to have to have the number of records in the second table, which is filled with NULL when there is no record in the first table.

3. Case sensitive

4. SELECT statement
is to determine which objects or properties you want to return from the query. Let's write a few examples:
Select Employee Form Employee as employee
Select Employee Form employee as employee where 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.) Look at the description)
Wait a minute

5. Mathematical functions
JDO currently does not seem to support this type of feature.
AVG (...), sum (...), min (...), Max (...)
COUNT (*)
Count (...), COUNT (distinct ...), count (all ...)
The usage is essentially the same as SQL:
Select distinct Employee.Name from employee as employee
Select COUNT (Distinct employee.name), count (employee) from employee as employee

6. Polymorphism (don't know how to explain for a while.) )
From Com.test.Animal as Animal
Not only do you get all the animal instances, but you can get all the animal subclasses (if we define a subclass cat)
A more extreme example.
From Java.lang.Object as O
Can get instances of all persistent classes

7. WHERE statement
Define the condition of the query statement, for 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 "=" You can compare objects, such as:
Select Animal from Com.test.Animal as animal where Animal.name=dog

8. An expression
Most expressions in an 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 for the solution)
SQL literals ' foo ', 69, ' 1970-01-01 10:00:01.0 '
Java public static final constants eg. Color.tabby
Other than to explain, here I just want to query the parameters of the problem description:
We know that in the SQL to carry out the parameters of the query, we usually use PreparedStatement, in the statement to write a lot of ". ”,
This method can also be used in hql, such as:
List mates = Sess.find (
"Select Employee.Name from Employee as employee" +
"Where employee." Name=? ",
Name
Hibernate.string
);
Note: Above using the Find method in the session, in the Hibernate API session overloaded a lot of find methods, it can satisfy you a variety of forms of query. The top is the case of a parameter, which is followed by the introduction of the parameter and the type of the defining parameter, and when the other find method is invoked for multiple arguments, the latter two arguments are in the form of an array.
There is another way to solve the problem above, JDO also has such a method, but and hibernate's performance is different, but their two bones are the same, such as:
Query q = Sess.createquery ("Select Employee.Name from employee as employee where employee.") Name=:name ");
Q.setstring ("name", "Jplateau");
When you have multiple parameters, define one at a time
Iterator employees = q.iterate ();

9. Order statement
and SQL statements are no different, such as:
Select Employee.Name from employee as employee where employee. Name like ' j% ' ORDER by employee.id Desc (or ASC)

10. GROUP BY statement
There is no difference from the 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, even on databases with no subselects.} Who can help me explain the above two sentences, thank you.

11. Child query
Hibernate also supports subqueries, writing several examples:
From eg. Cat as FatCat where Fatcat.weight > (select AVG (cat.weight) from eg. Domesticcat cat)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.