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.

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.

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.

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.

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