Hibernate Query Language (HQL)

HQL is an object-oriented query language
  • Syntax has similarities to SQL
  • Not working against tables and columns, but objects
Understands object-oriented concepts like inheritance and Reduces the size of queries.
Has advanced features like:
  • Associations and joins
  • Polymorphic queries
  • Subqueries
  • Expressions

The From Clause:
Simplest possible query, qualified class name auto-imported, will return all Person instances: 
Example:    from Employee
 
Convenient to assign an alias to refer to in other parts of the query:        Example: from Employee as e 

Multiple classes may be desired. The alias keyword is optional:        Example:  from Employee e, dept d

The Where Clause:


Allows you to narrow the returned list, properties can be referred to by name:

Example
: from Person where firstName=’John’  


HQL Example:

public Collection<Person> getPersonsByAge( int age, int maxResults )
{
  Session session = // retrieve a session instance somehow
  String hql = "from Person where age = :age";
  Query query = session.createQuery( hql );
  query.setInteger( "ageage", ageage );
  Query.setMaxResults( maxResults );
  return query.list();
}

 

Search:

Recent Posts