Hibernate Query Language | HQL | HQL Select Tutorials Point

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorial, we are going to learn about Hibernate Query Language (HQL). HQL is mainly used to perform the bulk operations in hibernate.
In our Hibernate Tutorials, so far, we have executed CURD operations on a single object at a time. If we want to execute a set of operations at a time, we can go with bulk operations.
To perform bulk operations, we can use one of the following techniques.
Hibernate Query Language (HQL)
Hibernate Criteria API
Native SQL
In this tutorials, we are mainly concentrate about HQL operations.
HQL is a Hibernate’s own query language.
HQL is a Database independent query language, that is., using this query language, we can develop data base independent queries.
HQL queries are easy to learn, because HQL looks like SQL only.
We can easily translate the SQL commands into HQL commands, by replacing the table columns with pojo class variable names, and table name with pojo class name with reference variable.
HQL queries are also called as Object Oriented form of SQL queries.
At runtime Hibernate will convert the HQL query into SQL query according to the database. So we no need to write a query according to the database.
In hibernate, we can divide the select operations into 2 types :
Reading Complete Entity
Reading Partial Entity
In Hibernate, selecting the all columns is called reading a complete entity.
Example :
SQL : select * from emp;
HQL : from Employee e;

HQL commands to read a complete entity or entities are directly begins with “from” keyword.
In Hibernate, reading a specific columns is called reading partial entity or entities.
Example :
SQL : select empno,sal from emp;
HQL : select e.employeeId, e.employeeSalary from Employee e;
In HQL, reading a partial entity is begins with “select” keyword.
In HQL we can pass the query parameters either in index parameters or named parameters.
Example :
SQL : select * from emp where deptno = ?;
HQL : from Employee e where e.deptNumber = ?
OR
HQL : from Employee e where e.deptNumber =:deptNo;
[box type=”info” align=”alignleft” class=”” width=”100%”]Named parameters are indicated with “:”[/box]
To perform the select operation, Hibernate given us Query object.
Query : A Query is a Object Orientation representation of Hibernate query. We can get the Query instance, by calling the session.createQuery();
We can also pass the parameters to the Hibernate Query. To do that, we can use query.setParameter(int pos, Object o)
And then call the list() on query object : query.list();
query.list() returns the query results in the form of java.util.List.
Example :
A simple HQL Query :
Query query = session.createQuery("from Employee e");

List qryResults = query.list();
HQL Query with parameters :
Query query = session.createQuery("from Employee e where e.deptNumber=?");

query.setParameter(0,300);

List qryResults = query.list();
If the HQL command is to read the complete entity or entities then the list contains the complete objects. That means, Hibernate internally does like below:
Hibernate gets the data from the database and it stores the records of table in ResultSet object.
Each record of the ResultSet will be set to a pojo class object.
Adds each object of the pojo class to the java.util.List and finally returns the List.
Iterator it =list.iterator();
while (it.hasNext()) {
Employee emp = (Employee) it.next();
System.out.println("Employee Name : " + emp.getEmployeeName()
+ " , Salary : " + emp.getSalary());
}
If HQL command is to read partial entity or entities, then the list contains the Objects array (Object[]). That is the hibernate does internally like below:
Hibernate reads partial entities from table and stores the result in ResultSet.
Hibernate will set the data of each record in ResultSet to an Object array.
Each object[] will be added to the list.
Finally it returns the list.
Iterator it = list.iterator();

while (it.hasNext()) {
Object[] object = (Object[]) it2.next();
System.out.println("Department Number : " + object[0]
+ " Salary : " + object[1]);
}
If the HQL command is to read partial entity with single column then the list contains Objects(Integer,String and etc.). That is., the Hibernate does internally like below:
Hibernate reads a single column of each record and stores in ResultSet.
Hibernate will set each record of ResultSet  into an Object of that property type (depends on the property type)
Adds objects to the list and finally returns the list.
Iterator it = list.iterator();
while (it.hasNext()) {
String salary = (String) it3.next();
System.out.println("Salary : " + salary);
}
This is how the Hibernate Select Query works. Download for complete example.
Happy learning 🙂


Hibernate Query Language Select Example

File size: 13 KB
Downloads: 840


HQL update, delete Query Example
Hibernate Native SQL Query Example
Hibernate groupby criteria HQL query Example
Hibernate Named Query with Example
Hibernate Criteria API with Example
Hibernate Projection with Example
Hibernate Restrictions with Example
JDBC Select Program Example
Calling Stored Procedures in Hibernate
Hibernate Left Join Example
Hibernate Filters Example Annotation
What is Hibernate
Top 10 Advantages of Hibernate
hibernate update query example
Hibernate Right Join Example

HQL update, delete Query Example
Hibernate Native SQL Query Example
Hibernate groupby criteria HQL query Example
Hibernate Named Query with Example
Hibernate Criteria API with Example
Hibernate Projection with Example
Hibernate Restrictions with Example
JDBC Select Program Example
Calling Stored Procedures in Hibernate
Hibernate Left Join Example
Hibernate Filters Example Annotation
What is Hibernate
Top 10 Advantages of Hibernate
hibernate update query example
Hibernate Right Join Example






Milana Travis
October 25, 2017 at 10:31 am - Reply 

Thank you very much for your blog.
I enjoyed reading this article.










Milana Travis
October 25, 2017 at 10:31 am - Reply 

Thank you very much for your blog.
I enjoyed reading this article.




Thank you very much for your blog.
I enjoyed reading this article.
 


Δ
 Hibernate – Introduction
 Hibernate – Advantages
 Hibernate – Download and Setup
 Hibernate – Sql Dialect list
 Hibernate – Helloworld – XML
 Hibernate – Install Tools in Eclipse
 Hibernate – Object States
 Hibernate – Helloworld – Annotations
 Hibernate – One to One Mapping – XML
 Hibernate – One to One Mapping foreign key – XML
 Hibernate – One To Many -XML
 Hibernate – One To Many – Annotations
 Hibernate – Many to Many Mapping – XML
 Hibernate – Many to One – XML
 Hibernate – Composite Key Mapping
 Hibernate – Named Query
 Hibernate – Native SQL Query
 Hibernate – load() vs get()
 Hibernate Criteria API with Example
 Hibernate – Restrictions
 Hibernate – Projection
 Hibernate – Query Language (HQL)
 Hibernate – Groupby Criteria HQL
 Hibernate – Orderby Criteria
 Hibernate – HQLSelect Operation
 Hibernate – HQL Update, Delete
 Hibernate – Update Query
 Hibernate – Update vs Merge
 Hibernate – Right Join
 Hibernate – Left Join
 Hibernate – Pagination
 Hibernate – Generator Classes
 Hibernate – Custom Generator
 Hibernate – Inheritance Mappings
 Hibernate – Table per Class
 Hibernate – Table per Sub Class
 Hibernate – Table per Concrete Class
 Hibernate – Table per Class  Annotations
 Hibernate – Stored Procedures
 Hibernate – @Formula Annotation
 Hibernate – Singleton SessionFactory
 Hibernate – Interceptor
 hbm2ddl.auto Example in Hibernate XML Config
 Hibernate – First Level Cache