Java Reference
In-Depth Information
A bit more complicated than the previous example is the native SQL that returns a result
set of objects. In this case, we will need to map an entity to the SQL query. The entity consists
of the alias we used for the object in the SQL query and its class. For this example, we used our
Supplier class:
String sql = "select {supplier.*} from Supplier supplier";
SQLQuery query = session.createSQLQuery(sql);
query.addEntity("supplier", Supplier.class);
List results = query.list();
Hibernate modifies the SQL and executes the following command against the database:
select Supplier.id as id0_, Supplier.name as name2_0_ from Supplier supplier
The special aliases allow Hibernate to map the database columns back to the object
properties.
Summary
HQL is a powerful object-oriented query language that provides the power of SQL while tak-
ing advantage of Hibernate's object-relational mapping and caching. If you are porting an
existing application to Hibernate, you can use Hibernate's native SQL facilities to execute
SQL against the database. The SQL functionality is also useful for executing SQL statements
that are specific to a given database and have no equivalents in HQL.
You may turn on SQL logging for Hibernate, and Hibernate will log the generated SQL
that it executes against the database. If you add a comment to your HQL query object, Hiber-
nate will display a comment in the log next to the SQL statement—this helps with tracing
SQL statements back to HQL in your application.
Search WWH ::




Custom Search