Hibernate Query Language
Hibernate Query Language
In this tutorial, we will discuss the Hibernate Query Language, which is an object-oriented query language.
Join the DZone community and get the full member experience.Join For Free
Compliant Database DevOps and the role of DevSecOps DevOps is becoming the new normal in application development, and DevSecOps is now entering the picture. By balancing the desire to release code faster with the need for the same code to be secure, it addresses increasing demands for data privacy. But what about the database? How can databases be included in both DevOps and DevSecOps? What additional measures should be considered to achieve truly compliant database DevOps? This whitepaper provides a valuable insight. Get the whitepaper
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:
- public int executeUpdate() — Execute the update or delete statement.
- public String getNamedParameters() — It returns the names of all named parameters of the query.
- public String getQueryString() — Get the query string.
- public List list() — It returns the query results as a List.
- public Query setMaxResults(int maxResults) — It is used to set the maximum number of rows to retrieve.
- 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();
- 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();
- 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.
Published at DZone with permission of Akash Tomar . See the original article here.
Opinions expressed by DZone contributors are their own.