Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

Slow SQL Server? These SentryOne resources share tips and tricks for not only troubleshooting SQL Server performance issues, but also preventing them before they hit your production environment.

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.

Database monitoring tools letting you down? See how SentryOne empowers Enterprises to go faster.

Topics:
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 }}