JavaBeat
Hibernate Home Articles Resources Tutorials Hibernate Interview Questions & FAQs JSF QnA Code Junction

1. What is Hibernate?

2. Hibernate Architecture

3. Writing First Hibernate Code

4. Understanding Hibernate O/R Mapping

5. Understanding Hibernate <generator> element

6. Using Hibernate <generator> to generate id incrementally

7. Hibernate Query Language

8. Preparing table for HQL Examples

9. Writing ORM for Insurance table

10. HQL from clause Example

11. Hibernate Select Clause

12. HQL Where Clause Example

13. HQL Group By Clause Example

14. HQL Order By Example

15. Hibernate Criteria Query Example

16. Criteria Query Examples

17. Hibernate Native SQL Example

Hibernate Query Language


Hibernate Query Language or HQL for short is extremely powerful query language. HQL is much like SQL  and are case-insensitive, except for the names of the Java Classes and properties. Hibernate Query Language is used to execute queries against database. Hibernate automatically generates the sql query and execute it against underlying database if HQL is used in the application. HQL is based on the relational object models and makes the SQL object oriented. Hibernate Query Language uses Classes and properties instead of tables and columns. Hibernate Query Language is extremely powerful and it supports Polymorphism, Associations, Much less verbose than SQL.

There are other options that can be used while using Hibernate. These are Query By Criteria (QBC) and Query BY Example (QBE) using Criteria API and the Native SQL queries. In this lesson we will understand HQL in detail.


Why to use HQL?

  • Full support for relational operations: HQL allows representing SQL queries in the form of objects. Hibernate Query Language uses Classes and properties instead of tables and columns.
       
  • Return result as Object: The HQL queries return the query result(s) in the form of object(s), which is easy to use. This elemenates the need of creating the object and populate the data from result set.
       
  • Polymorphic Queries: HQL fully supports polymorphic queries. Polymorphic queries results the query results along with all the child objects if any.

  • Easy to Learn: Hibernate Queries are easy to learn and it can be easily implemented in the applications.
       
  • Support for Advance features: HQL contains many advance features such as pagination, fetch join with dynamic profiling, Inner/outer/full joins, Cartesian products. It also supports Projection, Aggregation (max, avg) and grouping, Ordering, Sub queries and SQL function calls.
      
  • Database independent: Queries written in HQL are database independent (If database supports the underlying feature).

 

Understanding HQL Syntax
Any Hibernate Query Language may consist of following elements:

  • Clauses
  • Aggregate functions
  • Subqueries

Clauses in the HQL are:

  • from
  • select
  • where
  • order by
  • group by

Aggregate functions are:

  • avg(...), sum(...), min(...), max(...) 
  • count(*)
  • count(...), count(distinct ...), count(all...)

Subqueries
Subqueries are nothing but its a query within another query. Hibernate supports Subqueries if the underlying database supports it.





Sponsors
Webmaster Hosting Forum
Java Jobs
MyVideoLib
India News
Internet Advances
Latest QnA
Describe the lifecycle of a receiver application in order to receive a message?
Messages are not successful until they have been acknowledged. What are the types of acknowledgments?
What happens to messages if a transaction is rolled back?
What is the Role of the JMS Provider?
What is JMS administered object ?

JavaBeat Media (2004-2008), India
javabeat | planetoss | links directory | advertise
Copyright (2004 - 2008), JavaBeat