Hql: Hibernate query language (1)

Source: Internet
Author: User
Hql: Hibernate query language Hibernate is equipped with a very powerful Query Language, which looks like SQL. But do not be confused by syntax-structure similarity. hql is consciously designed as a fully Object-Oriented Query That can understand concepts such as inheritance, polymorphism, and association. 15.1. Case sensitivity
Except for the names of Java classes and attributes, query statements are not case sensitive. So select is the same as select and select, but org. hibernate. eg. foo is not equivalent to org. hibernate. eg. Foo and foo. barset is not equivalent to foo. barset.
The hql keywords in this manual use lower-case letters. many users find that the use of fully capitalized keywords makes the query statement more readable, but we find that it is difficult to use uppercase keywords when embedding the query statement into a Java statement. 15.2. From Clause
The simplest query statement in Hibernate is as follows:
Code content
From eg. Cat
This clause simply returns all instances of the EG. Cat class. Generally, we do not need to use a fully qualified class name, because Auto-import (auto-import) is the default. Therefore, we only use the following simple Syntax:
Code content
From cat
In most cases, you need to specify an alias because you may need to reference cat in other parts of the query statement.
Code content
From cat as Cat
This statement specifies the alias cat to the cat-like instance, so that we can use this alias in subsequent queries. The keyword as is optional. We can also write it like this:
Code content
From Cat
Multiple classes can appear in the clause at the same time. The query result is a Cartesian product or a cross-table join.
From formula, parameter
From formula as form, parameter as Param
The lower-case characters at the beginning of an alias in a query statement are considered a good habit in practice. This is consistent with the Naming Standard for Java variables (for example, domesticcat ). 15.3. Association (Association) And Connection (Join)
We can also specify an alias for the associated object or even all the elements in a set. In this case, we need to use the keyword join.
Code content
From cat as Cat
Inner join cat. mate as mate
Left Outer Join cat. Kittens as kitten
From cat as cat left join cat. mate. Kittens as kittens
From formula form full join form. Parameter Param
The supported connection types are used for reference in ansi SQL.
Inner join)
Left Outer Join (left Outer Join)
Right Outer Join (right Outer Join)
Full join (full join, not commonly used)
Statement inner join, left Outer Join, and right Outer Join can be abbreviated.
Code content
From cat as Cat
Join cat. mate as mate
Left join cat. Kittens as kitten
In addition, a "Fetch" connection allows only one selection statement to initialize the associated object or a set of values with the initialization of their parent object, this method is particularly useful when a set is used. For associations and sets, it effectively replaces the Outer Join and delay Declaration (lazy declarations) in the ing file ). view section 20.1 "fetching strategy" to obtain more information.
Code content
From cat as Cat
Inner join fetch cat. Mate
Left join fetch cat. Kittens
A fetch connection usually does not need to be specified as an alias, because the associated object should not be used in the WHERE clause (or any other clause. At the same time, the associated objects are not directly returned in the query results, but they can be accessed through their parent objects.
Note that fetch constructor variables cannot be used in queries using the scroll () or iterate () function. Finally, it is meaningless to use full join fetch and right join fetch.
If you use the property-level latency acquisition (lazy fetching) (this is achieved by re-coding bytecode ), you can use fetch all properties to force hibernate to immediately obtain the attributes that originally require delayed loading (in the first query ).
Code content
From document fetch all properties order by name
From document DOC fetch all properties where lower (Doc. Name) like '% cats %'
 

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.