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

The first thing you should know about HQL is that it is exclusively a query language. Unlike SQL, which offers statements of different forms for SELECT, UPDATE, DELETE, and INSERT, HQL is designed exclusively for retrieval. As a result, the syntax is most similar to the SELECT statement. There is no such thing as HQL for updating or inserting data, and the exact same HQL can be used for both SELECT and DELETE operations.

Some of the examples in Chapter 3 and Chapter 4 give an introduction to the use of HQL, and the methods for executing HQL are shown in Chapter 6. Nonetheless, HQL is most easily explored in the context of the Hibern8 IDE, a simple tool for executing HQL statements on the fly in a graphical environment.

Using Hibern8 IDE

The Hibern8 IDE is distributed with the Hibernate Extensions package. To use it, simply launch it using the command java net.sf.hibern8ide.Hibern8IDE (found in the hibern8ide.jar), with the class path set to include the various required Hibernate and Hibernate Extensions libraries as well as the persistent classes used by your application. Launching Hibern8 IDE will display the interface shown in Figure 8.1.

Figure 8.1. Starting Hibern8 IDE


To establish the Hibernate configuration, click on the ... button next to either the Properties field (to select a hibernate.properties file) or the Config File field (to select a hibernate.cfg.xml file).

Click the Add button to add one or more *.hbm.xml mapping files. After adding all of the needed files, click the Apply button.

Using the hibernate.properties and *.hbm.xml files and classes from Chapter 3, Hibern8 IDE will look as shown in Figure 8.2.

Figure 8.2. Hibern8 IDE Configured


Clicking on the HQL Commander tab, you can drag and drop objects or object properties from the left-side object display to the HQL entry field so that Hibern8 IDE can automatically generate HQL for you. Pressing control-enter will cause Hibern8 IDE to execute the HQL and display the results. Figure 8.3 shows an example of retrieving the artifact.id values.

Figure 8.3. Artifact ID Values


Hibern8 IDE allows you to inspect the values of returned results, tunneling in through the returned object hierarchy. Figure 8.4 shows an example of this, allowing you to see in detail the objects returned by a request.

Figure 8.4. Inspecting Hibern8 IDE Results


As described in Chapter 9, you can use Hibern8 in conjunction with a tool like IronTrack SQL to easily monitor the SQL generated by a given HQL statement. This can be very useful as you experiment with HQL using Hibern8.

It is easy to explore the capabilities of HQL using Hibern8, but to truly take advantage of Hibernate you need to understand the structure of HQL in more detail. You are encouraged to work through this reference in conjunction with Hibern8 and the various mapping files in the rest of the book to fully understand HQL.