Hibernate query language: hql

Source: Internet
Author: User
Tags mathematical functions null null
Hql: hibernate qusery language. If you are familiar with it, you will find that it is very similar to SQL. However, you should not be confused by the illusion that hql is object-oriented (OO, looking at every object with a life perspective, they are so vivid ). If you have some knowledge about Java and SQL statements, hql is easy for you. You can take full advantage of it on the bus.

The following is a step-by-step process from several aspects:

1. Sensitive

As you know, query is case-insensitive, but in hql (previously mentioned as OO), the names and attributes of object classes are indeed case-sensitive (in line with Java programming syntax ).

For example, select cat. name from cat as cat is the same as select cat. name from cat as cat.

However:

Select cat. name from cat as CAT and select cat. name from cat as CAT are indeed different.

2. From Statement

Simplest:

From eg. Cat

It simply returns all eg. Cat instances.

Usually we will use the alias for eg. Cat at this time, because it may be used in the rest of the query (see the preceding section about Case sensitivity.

Sensitive examples), such:

From eg. CAT as Cat can be omitted here.

The preceding statement is only for single-table queries. Multiple tables are written as follows:

From eg. Cat, eg. Dog

From eg. CAT as Cat, eg. Dog as dog

3. Join Problems

(Inner) join

Left (outer) join

Right (outer) join

Full join

Hql also supports these features in SQL

The following is a small topic. I have never used any of the above features. Now, I want

Let's take a look at the usage of the above features to supplement our own:

Assume there are two tables: department and employee. The following table lists some data:

Employee (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 all objects, So we manipulate department 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 condition statement I use on does not use 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 (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
what is the execution result?
id1 name1 Id2 name2
++ ++
001 jplateau 01 R & D department
002 Jony 01 R & D department
003 camel null
{That is to say, the number of records in the first table is correct, fill 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
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 to say, the number of records in the second table shall prevail, fill in null when there is no corresponding record in the first table}

4. Select statement

It is to determine which objects or attributes of which objects 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

(Do not understand what elements is? Hope to explain)

And so on.

5. Mathematical functions

Currently, JDO does not seem to support such features.

AVG (...), sum (...), min (...), max (...)

Count (*)

Count (...), count (distinct...), count (all ...)

The method and SQL are basically the same

Select distinct employee. name from employee as employee

Select count (distinct employee. Name), count (employee) from employee as employee

6. Polymorphism (do not know how to explain it for the moment ?)

From Com. Test. Animal as animal

Not only do we get all animal instances, but we can also get all animal subclasses (if we define a subclass cat)

An extreme example

From Java. Lang. object as O

You can obtain instances of all persistent classes.

7. Where statement

Define the query statement conditions. For example:

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, "=" can not only compare object attributes, but also compare objects, such:

Select animal from Com. Test. Animal as animal where animal. Name = dog

8. Expression

Most expressions in SQL statements 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 "? "Work und)

SQL literals 'foo', 69, '2017-01-01 10:00:01. 0'

Java public static final constants eg. color. Tabby

I don't have to explain anything else. Here I just want to explain the parameters in the query:

We know that when passing parameters in SQL for query, we usually use preparedstatement to write a lot of "?" In the statement,

This method can also be used in hql, for example:

List mates = sess. Find (

"Select employee. name from employee as employee" +

"Where employee. Name =? ",

Name,

Hibernate. String

);

(Note: The above uses the find method in the session to reload many find methods in the hibernate API session, which can meet your needs for multiple forms of queries)

The preceding is a parameter. In this case, the type of the parameter and the defined parameter are introduced immediately. When multiple parameters are called and another find method is called, the last two

Parameters are in the form of arrays.

There is another way to solve the above problem. JDO also has this method, but it is different from hibernate in its form, but they are actually

Same, such:

Query q = sess. createquery ("select employee. name from employee as employee where employee. Name =: Name ");

Q. setstring ("name", "jplateau ");

// Define multiple parameters here

Iterator employees = Q. iterate ();

9. Order statement

There is no difference with SQL statements, such:

Select employee. name from employee as employee where employee. name like 'J % 'order by employee. id desc (or ASC)

10. Group by statement

Similar to SQL statements, for example:

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 group by foo. ID

{Note: You may use the elements and indices constructs inside a select clause, even on databases with no subselects .}

Thank you!

11. Subquery

Hibernate also supports subqueries. Here are several examples:

From eg. CAT as fatcat where fatcat. weight> (select AVG (Cat. Weight) from eg. domesticcat)

Section:

In fact, hql is very similar to SQL. When writing, you only need to always think about the object concept, so you can use SQL to write hql.

Reference resources: refer to chapter 7 and chapter 9 of the hibernate reference manual. It is recommended that you carefully study the manual from time to time and use the hibernate development project.

Ezerg Programming Language
Author's blog:Http://blog.csdn.net/ezerg/RelatedArticle

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.