Hibernate Query Language (HQL)

Hibernate Query Language (HQL) is same as SQL (Structured Query Language) however it doesn't relies upon the table of the database. Rather than table name, we use class name in HQL. So it is database free inquiry language.

Advantage of HQL

There are numerous focal points of HQL. They are as per the following:

  • database independent
  • supports polymorphic queries
  • easy to learn for Java Programmer

Query Interface

It is an article situated portrayal of Hibernate Query. The object of Query can be gotten by calling the createQuery() technique Session interface.

The inquiry interface gives numerous techniques. There is given ordinarily utilized methods:

  1. public int executeUpdate() is utilized to execute the update or erase query.
  2. public List list() restores the consequence of the ralation as a list.
  3. public Query setFirstResult(int rowno) indicates the line number from where record will be retrieved.
  4. public Query setMaxResult(int rowno) indicates the no. of records to be recovered from the connection (table).
  5. public Query setParameter(int position, Object value) it sets the incentive to the JDBC style inquiry parameter.
  6. public Query setParameter(String name, Object value) it sets the incentive to a named question parameter.

Example of HQL to get all the records

  1. Query query=session.createQuery("from Emp");//here persistent class name is Emp  
  2. List list=query.list();  

Example of HQL to get records with pagination

  1. Query query=session.createQuery("from Emp");  
  2. query.setFirstResult(5);  
  3. query.setMaxResult(10);  
  4. List list=query.list();//will return the records from 5 to 10th number  

Example of HQL update query

  1. Transaction tx=session.beginTransaction();  
  2. Query q=session.createQuery("update User set name=:n where id=:i");  
  3. q.setParameter("n","Udit Kumar");  
  4. q.setParameter("i",111);  
  6. int status=q.executeUpdate();  
  7. System.out.println(status);  
  8. tx.commit();  

Example of HQL delete query

  1. Query query=session.createQuery("delete from Emp where id=100");  
  2. //specifying class name (Emp) not tablename  
  3. query.executeUpdate();  

HQL with Aggregate functions

You may call avg(), min(), max() and so forth total capacities by HQL. How about we see some regular models: xample to get total salary of all the employees
  1. Query q=session.createQuery("select sum(salary) from Emp");  
  2. List<Integer> list=q.list();  
  3. System.out.println(list.get(0));  

Example to get maximum salary of employee

  1. Query q=session.createQuery("select max(salary) from Emp");  

Example to get minimum salary of employee

  1. Query q=session.createQuery("select min(salary) from Emp");  

Example to count total number of employee ID

  1. Query q=session.createQuery("select count(id) from Emp");  

Example to get average salary of each employees

  1. Query q=session.createQuery("select avg(salary) from Emp");  

javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition