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:
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. FROMThe 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. SELECTYou 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. WHEREThe 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. |