Java Reference
In-Depth Information
The generated SQL will be visible whenever the HQL query is syntactically valid, allowing
you to confirm the basic correctness of the HQL and confirm the sanity of the underlying query
at the same time.
Once you have a satisfactory HQL query, it can be executed against the database to vali-
date the returned results. Enter the following query in the scratch pad window, and then click
the green play button above the editor window.
select note as Note_Entry
from Note
where id > 10 and id < 20
The view shown in Figure B-19 will list the results of the query. Note that the results have
been given the correct column heading.
Figure B-19. The output from the query
Query Parameters
While the scratch pad alone is adequate for running stand-alone queries, much of the HQL
that you will use in practice in your applications will be parameterized. Consider the following
typical query:
select owner from Notepad where owner = :username
This query uses a named parameter, username , as a placeholder for the actual value. When
converted to SQL, this will become a parameter in a prepared statement, and will not be spec-
ified until the application is actually running.
The Hibernate Console provides the novel feature of allowing you to specify the values of
parameters in Hibernate queries of this type. 2 This means that you can cut and paste an HQL
query from your code into the scratch pad, and test it there without having to edit any of the
parameters themselves.
If you paste the preceding query into a scratch pad window and try to run it as is, the con-
sole will log an appropriate error, warning you that the query parameters have not all been set
(currently this appears in the Hibernate Query Result view). On the right-hand side of the per-
spective's default layout, there is a view called Query Parameters. Adjust the layout so that you
can see the three columns within this view, labeled Name, Type, and Value.
2. If you've ever encountered problems debugging complex SQL queries, in which the process of manu-
ally substituting in parameters disguised the cause of the problem during testing, you will understand
why we are extremely enthusiastic about this ingenious feature!
Search WWH ::




Custom Search