More Books
Hibernate: A J2EE Developer's Guide
Hibernate: A J2EE™ Developer's Guide
Table of Contents
Copyright
Acknowledgments
About the Author
Preface
Required Skills
Roadmap
Chapter 1. Overview
Why Object/Relational Mapping?
What Is Hibernate?
Comparing JDBC to Hibernate
Hibernate's Mapping System
Other Java/Database Integration Solutions
How to Obtain and Install
Supported Databases
Chapter 2. Getting Oriented
Application Architecture
Mapping Files
Generating Java Source
Application Configuration
Web Application
JSP Interface
Chapter 3. Starting from Java
Java Object Model
Generated Mapping Files
Generated Schema
Working with Artifacts and Owners
Chapter 4. Starting from an Existing Schema
Initial Schema
Using Middlegen
Generated Mapping Files
Generated Java
Working with the Database
Chapter 5. Mapping Files
Basic Structure
Mapping File Reference
Chapter 6. Persistent Objects
Sessions
Objects and Identity
Life-Cycle Methods
Chapter 7. Relationships
Database Relationships
Java Collection Relationships
Java Class Relationships
Any-Based Relationships
Bi-directional Relationships
Chapter 8. Queries
HQL
HQL Reference
Select
From
Where
Group By
Having
Order By
Criteria Queries
Native SQL Queries
Chapter 9. Transactions
Introduction to Transactions
Optimistic and Pessimistic Locking
Chapter 10. Performance
Finding and Solving Problems
Queries
Inserts
Connection Pooling
Caching
Chapter 11. Schema Management
Updating an Existing Schema
Generating Update and Drop Scripts
Chapter 12. Best Practices, Style Guide, Tips and Tricks
Reducing Code with Inversion of Control
Reducing Session Creation Impact with ThreadLocal
Using Hibernate as an EJB BMP Solution
Integrating with Other Technologies
Applications That Use Hibernate
Strategies for Getting Started
Chapter 13. Future Directions
Hibernate 3.0
EJB 3.0
Here and Now
Index
SYMBOL
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X

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 Symbols

Notation

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).