Java Reference
In-Depth Information
Testing HQL Queries
HQL is a powerful way to interact with the database; but traditionally, debugging errors in
these queries has been a long-winded process. Typically, a developer would end up creating a
unit test for the query and then spending considerable time tweaking it into working correctly.
Hibernate Tools now provides a query editor that allows you to prototype your HQL queries
and run them against the database without needing to write any code.
To run an HQL query, select HQL Scratchpad from the context menu for the project in the
configuration view (by default, this is to the left of the main window in the Hibernate Console
perspective).
A new editor window will be created and labeled with HQL and the name of the project—
in our example, it's “HQL: ToolsExample.” This is an HQL editor with some context sensitivity.
Context sensitivity is activated with the Ctrl+Space key combination, as is usual for Eclipse
editors. For example, enter the following query in the scratch pad editor and press Ctrl+Space:
from Note
This will display a context menu offering the two class names that are legal at this point in
the query (as shown in Figure B-17).
Figure B-17. The context-sensitive scratch pad
n Caution At the time of writing, there are various bugs related to saving scratch pads to physical files,
and to running HQL queries loaded-in directly from text files. As a workaround, it is possible to copy and
paste between a scratch pad window and a normal text editor view in order to run queries. It is likely that
these problems will have been addressed by the time you read this.
As you create the query, the corresponding SQL query against the underlying relational
database is updated in the Hibernate Dynamic SQL Preview window shown in Figure B-18.
Figure B-18. The Hibernate Dynamic SQL Preview window
Search WWH ::




Custom Search