Java Reference
In-Depth Information
Use this to identify the SQL output in your application's logs if SQL logging is enabled. For
instance, if we add a comment to this example, the Java code would look like this:
String hql = "from Supplier";
Query query = session.createQuery(hql);
query.setComment("My HQL: " + hql);
List results = query.list();
The output in your application's log will have the comment in a Java-style comment
before the SQL:
Hibernate: /*My HQL: from Supplier*/ select supplier0_.id as id, supplier0_.name å
as name2_ from Supplier supplier0_
We have found this useful for identifying SQL in our logs, especially because the gener-
ated SQL is a little difficult to follow when you are scanning large quantities of it in logs.
The from Clause and Aliases
We have already discussed the basics of the from clause in HQL in the earlier section, “The
First Example with HQL.” The most important feature to note is the alias . Hibernate allows
you to assign aliases to the classes in your query with the as clause. Use the aliases to refer
back to the class inside the query. For instance, our previous simple example would be the
following:
from Product as p
or the following:
from Product as product
You'll see either alias-naming convention in applications. The as keyword is optional—
you can also specify the alias directly after the class name, as follows:
from Product product
If you need to fully qualify a class name in HQL, just specify the package and class name.
Hibernate will take care of most of this behind the scenes, so you only really need this if you
have classes with duplicate names in your application. If you need to do this in Hibernate,
use syntax such as the following:
from com.hibernatebook.criteria.Product
The from clause is very basic and useful for working directly with objects. However, if you
want to work with the object's properties without loading the full objects into memory, you
must use the select clause.
The select Clause and Projection
The select clause provides more control over the result set than the from clause. If you want to
obtain the properties of objects in the result set, use the select clause. For instance, we could
Search WWH ::




Custom Search