HQL Reference
[select] from [where] [group by] [having] [order by];
This section serves as a road map for understanding the HQL syntax. It can be used to diagram a HQL statement, much as you diagram a sentence in English (circling the noun, the verb, etc.).
A HQL command is composed of several clauses, as shown above. The from clause is the only portion of a query that is mandatory. For example, the simple command from com.cascadetg.ch03.Owner will return all of the owner table records as Owner objects.
The select clause is used to limit the returned data to specific properties or to control the format for the returned data. It will be used to return either an aggregate value (e.g., the number of records returned) or a subset of the data (e.g., just the first name of the student objects).
The from clause is used to widen the returned data to additional classes. The tables used are as determined by the class mappings (defined by your *.hbm.xml files). HQL requires at least one class to form the basis of the query. If you wish to use more than one table, you may have to use join statements (described below) to control how the tables are returned.
The where, group by, and having clauses are used to cull the data returned. You can use a combination of logical expressions and Boolean expressions to compose your where clause. In addition, you can use collection commands to work with collections in your where statements.
The order by clause is used to sort the returned data.
Notation Reference
The clauses given below are expected to be formatted with a particular set of tokens. The format of this HQL guide is based on the notation shown in Table 8.1.
Table 8.1. HQL Grammar Notation[ ... ] | Indicates an optional term. | ( ... ) | Indicates a grouping of terms. | ... | ... | Indicates that one side or the other of the | is expected. | * | Indicates that zero, one, or many terms are expected. | ? | Indicates that zero or one terms expected. | UPPERCASE | Used to indicate an expected token. | lowercase | Used to indicate additional text, as specified by another clause or some other text. |
This reference uses uppercase to indicate HQL terms, but in actual use HQL terms are case-insensitive.
Obviously, HQL uses some of the symbols shown in Table 8.1. Parentheses, for example, are used to indicate precedence in HQL queries as well as in the notation. Therefore, the tokens in Table 8.2 should be translated to these terms when writing actual HQL queries.
Table 8.2. HQL Grammar SymbolsNotation | Symbol |
|---|
COMMA | , | OPEN | ( | CLOSE | ) | OPEN_BRACKET | [ | CLOSE_BRACKET | ] | CONCAT | || | PLUS | + | MINUS | - | DIV | / | STAR | * | path | An identifier to a particular class or property |
Note that the path elements are case-sensitive. For example, select owner from Owner as owner and SELECT owner FROM Owner AS owner are equivalent, whereas SELECT FROM OWNER is invalid (because OWNER doesn't match the case of the name Owner given in the mapping file).
|