Chapter 7. Hibernate Query Language

Hibernate introduced its own query language, called the Hibernate Query Language (HQL), specially designed for querying Java object models. It is a simple query language, similar to SQL. We will be using HQL for working on our objects often, so this chapter introduces the features of this query language in detail.

We use Structured Query Language (SQL) to query the relational tables. SQL is the de facto standard when it comes to working with relational databases. However, Hibernate’s intent was to create a simple and easy-to-use language to work with object models seamlessly. Hence, in line with SQL, Hibernate created the Hibernate Query Language for querying the Java Object graph. HQL is a simple but powerful feature of the toolkit.

Leveraging developers’ familiarity with SQL, the Hibernate team made HQL similar to SQL so the learning curve would be easy. For example, while in SQL we use SELECT * FROM MOVIES to fetch all the records in the MOVIES table, in HQL we’d use FROM Movie. As we are dealing with objects in HQL, we must provide the entity class name that represents our table—in this case, Movie is our persistent Java entity mapped to the MOVIES table. Ideally, we should provide the fully qualified name (FQN) of the Movie class, like: FROM com.madhusudhan.jh.hql.Movie. The SELECT statement in HQL is optional if you are querying the entire table’s data. However, you must use it when you are working with one or more columns individually, usually ...

Get Just Hibernate now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.