Rabbit Eight Brother Note 14 (1): Hibernate Query Language (HQL)
Mailbox: ltf_ty@163.net
This article translated from Hibernate documents Chapter 10 "Hibernate Query Language".
Hibernate has a very powerful query language, which is intentionally very similar to SQL, which is convenient for developers. But don't be confused by HQL's syntax surface, HQL is completely object-oriented, can be used to process polymorphism, inheritance, association and other relationships.
10.1 Case Sensitivity Used Java class names and properties are sensitive, and other keywords are unoccupied. So "select" equivalent to "SELECT", is also equivalent to "SELECT" because it is not a Java class name, nor is the property name of the Java class. However, Java class net.sf.hibernate.eg.foo is not equivalent to net.sf.hibernate.eg.foo, and the same foo.barset is not equivalent to foo.barset.
In this manual, keywords in HQL use lowercase, some users may find that HQL's keywords are more readily read, but we also found that when these HQL embedded into the Java code, it appeared ugly.
10.2FROM clause (the from clause)
The easiest from Hibernate may be:
From eg.cat
Just simply returns all instances of the Eg.cat class.
Many times you might need to set alias (alias) because you may need to reference Cat in other parts of the query.
From eg.cat as cat
Keyword AS is optional, we can also write:
From eg.cat cat
Multiple classes can appear, then return a "Cartesi" or cross-connection:
From Formula As Form, Parameter As PARAM
Alias in HQL uses lowercase letters to be a good habit, complying with naming specifications for Java local variables.
10.3 Association and connection (Associations and Joint)
We use an alias associated entity, even with a collection of a collection of values with Join.
From eg.cat as cat
Inner Join Cat.mate As Mate
LEFT OUTER JOIN Cat.kittens As Kitten
From eg.cat as cat left join cat.mate.kittens As kittens
From Formula Form Full Join Form.Parameter Param
Supported connection types learn from ANSI SQL:
· Inner Join
· Left outer Join
Right Outer Join
· Full Join (Not common)
Inner Join, Left Outer Join and Right Outer Join can be skilled.
From eg.cat as cat
Join Cat.mate As Mate
Left Join Cat.kittens As Kitten
Additionally, a "fetch" connection allows you to use a single connection to associate a set of values, allowing them to initialize with the parent object. This is especially useful in the case of using Collection.
From eg.cat as cat
Inner Join Fetch Cat.mateleft Join Fetch Cat.kittens
Fetch Join usually does not need to set an alias because the object being associated should not be used in the WHERE clause, and cannot be used in any other clause.
The associated object cannot be returned directly in the query result, they can access through the parent object.
Please note: In the current implementation, only one collection can be returned in the query. Also note that Fetch may not be in a query called by scroll () and iTerator (). Finally, it is important to note that Full Join Fetch and Right Join Fetch are meaningless.
10.4 SELECT clause (The Select Clause)
The SELECT slave is used to select the objects and properties returned in the result set:
Select cat.mate from eg.cat cat
The above query returns all cats of the spouse.
You can also use the Elements function to return a collection of elements. The following query will return all kittens for any cat (Kitten).
Select Elements (cat.kittens) from eg.cat cat
The inquiry can also return the properties of any value type (including properties of the Component type):
Select cat.name from eg.domesticcat cat
Where cat.name like 'fri%'
Select custom.name.firstname from customer as cup
The query can return multiple objects or return an attribute of an array as an Object [] type.
SELECT Mother, Offspr, Mate.Name
From eg.domesticcat as mother
Inner Join Mother.mate As Mate
Left outer Join Mother.kittens As Offspr
Or as an actual Java object:
SELECT New Family (Mother, Mate, Offspr)
From eg.domesticcat as mother
Join Mother.mate As Mate
Left join mother.kittens As offspr
The above query statement assumes that the Family class has an appropriate constructor.
10.5 Gathering Function (AGGREGATE FUNCTIONS)
The query can use the aggregation function of the attribute:
SELECT AVG (Cat.Weight), Sum (Cat.Weight), Max (Cat.Weight), Count (cat) from eg.cat cat
A collection of Select clauses can occur in the aggregation function of the Sentence:
Select Cat, Count (Elements (Cat.kittens)) from eg.cat cat group by cat
Supported aggregates are:
· AVG (...), SUM (...), min (...), max (...)
· COUNT (*)
COUNT (...), Count ...), Count (all ...)
The meaning of the Distinct and all keywords is the same as the usage and SQL:
Select distinct cat.name from eg.cat cat
Select Count (Distinct Cat.Name), Count (cat) from eg.cat cat
10.6 Polymorphism) A query: from eg.cat as cat, it is not just CAT, there is a subclass such as Domesticcat. Hibernate can specify any Java classes and interfaces in the FROM clause, and the query returns an instance of all persistent classes inherited from the class and implements the interface. The following query will return all persistent objects:
From java.lang.Object O
The specified interface can be implemented by multiple different persistent classes:
From eg.named n, eg.named m where n.name = m.name
Note that the last 2 queries will take more than 1 SQL SELECT, which means that the entire result set cannot be arranged in the arrangement of the clause. This also means that you can't use query.scroll () to call these queries.