Section 20.5. HQL (Hibernate Query Language)


20.5. HQL (Hibernate Query Language)

Database applications, no matter how object-oriented, need to have direct access to the underlying database from time to time. We've already seen some reasons why (filtering data by parameterized query). The need for queries breaks down into the following categories:

  • Retrieve a collection of objects from the database all at once

  • Retrieve a sorted collection of objects from the database all at once

  • Retrieve nonobject values from the database

  • Retrieve summary information from the database

All four of these categories involve either collecting persistent objects or avoiding them altogether, making the direct object/relational mapping strategy an obstacle.

Hibernate provides Hibernate Query Language (HQL) and the Query interface to allow you to execute statements that meet these goals. Hibernate Query Language looks an awful lot like SQL. HQL queries can have a SELECT clause, a FROM clause, and a WHERE clause. You can, if you choose, author direct SQL statements as HQL queries; as long as the database can perform the query, the query will execute. On the other hand, you can use object-oriented notation instead of data-oriented notation in your HQL queries. This means, instead of using table and field names, you can use class and property names, or any mix of the two.

20.5.1. FROM

The FROM clause of an HQL query tells Hibernate where to look for the requested values. You can use either the name of one or more persistent classes, the name of one or more tables in the database, or any combination thereof. When all you want is for Hibernate to return one or more first-class persistent objects, you can issue an HQL query consisting of nothing but the FROM clause:

 List professors = session.createQuery("FROM Professor").list(  ); 

Hibernate doesn't require a SELECT clause because, by default, results will include every field in the table that is needed to create a Professor instance. Without a WHERE clause, the query returns every row in the table.

20.5.2. SELECT

You can use the SELECT clause to provide aliases to the first-class objects being returned or to return values other than first-class persistent types. For example, if you wanted to return just the last names of all professors, you could use the following query:

 List profLastNames =     session.createQuery("SELECT lastName from Professor").list(  ); 

The result is a List of String values, one for each Professor, containing just the last-name field. Asking for multiple values returns a list of arrays containing the values in order:

 Iterator profNames =     session.createQuery("SELECT firstName, lastName from Professor").list(  ).     iterator(  ); while(profNames.hasNext(  )) {     Object[] names = profNames.next(  );     // firstname == names[0]     // lastname == names[1] } 

20.5.3. WHERE

The WHERE clause can contain any specific kind of filtering or grouping criteria that your database vendor supports. If you use a database that allows subqueries, HQL will happily allow you to use subqueries. Standard equality, comparison, and join notations are all available. If you attempt to use a SQL feature that is unavailable on your database platform, Hibernate will throw a SQL exception when it tries to execute the command on the database.



Java Enterprise in a Nutshell
Java Enterprise in a Nutshell (In a Nutshell (OReilly))
ISBN: 0596101422
EAN: 2147483647
Year: 2004
Pages: 269

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net