Over a million developers have joined DZone.

Hibernate Query Language

DZone's Guide to

Hibernate Query Language

In this tutorial, we will discuss the Hibernate Query Language, which is an object-oriented query language.

· Database Zone ·
Free Resource

Built by the engineers behind Netezza and the technology behind Amazon Redshift, AnzoGraph is a native, Massively Parallel Processing (MPP) distributed Graph OLAP (GOLAP) database that executes queries more than 100x faster than other vendors.  

In this tutorial, we will discuss the Hibernate Query Language. HQL is an object-oriented query language. Hibernate Query Language(HQL) is same as Structured Query language(SQL), except that in HQL, we use entity class name instead of table name.

HQL provides its own syntax and grammar.

Ex.  From Employee emp.

Where Employee is an entity class which is associated with employee table and emp is the alias name of Employee class.

It is an alternative to SQL query Select* from employee.

All hibernate queries are translated by hibernate into structured query for further processing. Hibernate also provides a way to use a structured query in hibernate. HQL syntax is not case sensitive, but class name and variable name are case sensitive.

Query Interface: It is n object-oriented representation of a Hibernate query. We can get Query instance by Session.createQuery(). 

This interface provides the various method:

  1. public int  executeUpdate() — Execute the update or delete statement.
  2. public String getNamedParameters() — It returns the names of all named parameters of the query.
  3. public String[] getQueryString() — Get the query string.
  4. public List list() —  It returns the query results as a List.
  5. public Query setMaxResults(int maxResults) — It is used to set the maximum number of rows to retrieve.
  6. public Object uniqueResult() — It is used to return a single instance that matches the query, or null if the query returns no results.

Let’s take a look at CRUD operations using HQL.

1. Select Query: Retrieve an employee detail where employee ID is 255.

Query query = session.createQuery("From Employee where employeeId= :id ");
query.setParameter("id", "255");
List list = query.list();
  1. Update Query: Update Employee name where ID is 255.
Query query = session.createQuery("Update Employee set employeeName =:eName" +  " where employeeId= :id");
query.setParameter("employeeName ", "AKASH");
query.setParameter("id", "255");
int result = query.executeUpdate();

3.DELETE Query: Delete employee where ID is 255.

Query query = session.createQuery("Delete Employee where employeeName = :id");
query.setParameter(":id", "255");
int result = query.executeUpdate();
  1. Insert Query: In HQL, we can only insert values from another table.
Query query = session.createQuery("insert into Employee (employee_code, employee_name)" +"select employee_code, employee_name from employee_info");
int result = query.executeUpdate();

Note: The query.executeUpdate() will return how many number of record has been inserted, updated, or deleted.

Download AnzoGraph now and find out for yourself why it is acknowledged as the most complete all-in-one data warehouse for BI style and graph analytics.  

hql ,database ,query interface ,query ,crud operations ,hibernate ,query language

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}