Last Updated on 12 December 2023   |   Print Email
The Hibernate ORM framework provides its own query language called Hibernate Query Language or HQL for short. It is very powerful and flexible and has the following characteristics:
SQL similarity: HQL’s syntax is very similar to standard SQL. If you are familiar with SQL then writing HQL would be pretty easy: from SELECT, FROM, ORDERBY to arithmetic expressions and aggregate functions, etc.
Fully object-oriented: HQL doesn’t use real names of table and columns. It uses class and property names instead. HQL can understand inheritance, polymorphism and association.
Case-insensitive for keywords: Like SQL, keywords in HQL are case-insensitive. That means SELECT, select or Select are the same.
Case-sensitive for Java classes and properties: HQL considers case-sensitive names for Java classes and their properties, meaning Person and person are two different objects.
In this tutorial, we show you how to write HQL for executing fundamental queries (CRUD) as well as other popular ones. The following diagram illustrates relationship of the tables used in examples of this tutorial:And here are the model classes annotated with JPA annotations:Category class:
package net.codejava.hibernate;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "CATEGORY")
public class Category {
private long id;
private String name;
private Set<Product> products;
public Category() {
}
public Category(String name) {
this.name = name;
}
@Id
@Column(name = "CATEGORY_ID")
@GeneratedValue
public long getId() {
return id;
}
@OneToMany(mappedBy = "category", cascade = CascadeType.ALL)
public Set<Product> getProducts() {
return products;
}
// other getters and setters
}
Product class:
package net.codejava.hibernate;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "PRODUCT")
public class Product {
private long id;
private String name;
private String description;
private float price;
private Category category;
public Product() {
}
public Product(String name, String description, float price,
Category category) {
this.name = name;
this.description = description;
this.price = price;
this.category = category;
}
@Id
@Column(name = "PRODUCT_ID")
@GeneratedValue
public long getId() {
return id;
}
@ManyToOne
@JoinColumn(name = "CATEGORY_ID")
public Category getCategory() {
return category;
}
// other getters and setters
}
Order class:
package net.codejava.hibernate;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
@Table(name = "ORDERS")
public class Order {
private int id;
private String customerName;
private Date purchaseDate;
private float amount;
private Product product;
@Id
@Column(name = "ORDER_ID")
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name = "CUSTOMER_NAME")
public String getCustomerName() {
return customerName;
}
@Column(name = "PURCHASE_DATE")
@Temporal(TemporalType.DATE)
public Date getPurchaseDate() {
return purchaseDate;
}
@ManyToOne
@JoinColumn(name = "PRODUCT_ID")
public Product getProduct() {
return product;
}
// other getters and setters
}
The upcoming examples are provided based on assumption that a Hibernate’s SessionFactory is opened and a transaction has been started. You can find more about how to obtain SessionFactory and start a transaction in the tutorial: Building Hibernate SessionFactory from Service Registry. The following table of content is provided for your convenience:
Basically, it’s fairly simple to execute HQL in Hibernate. Here are the steps:
Write your HQL:
String hql = "Your Query Goes Here";
Create a Query from the Session:
Query query = session.createQuery(hql);
Execute the query: depending on the type of the query (listing or update), an appropriate method is used:
For a listing query (SELECT):
List listResult = query.list();
For an update query (INSERT, UPDATE, DELETE):
int rowsAffected = query.executeUpdate();
Extract result returned from the query: depending of the type of the query, Hibernate returns different type of result set. For example:
Select query on a mapped object returns a list of those objects.
Join query returns a list of arrays of Objects which are aggregate of columns of the joined tables. This also applies for queries using aggregate functions (count, sum, avg, etc).
The cool thing here is Hibernate automatically generates JOIN query between the Product and Category tables behind the scene. Thus we don’t have to use explicit JOIN keyword:
You can parameterize your query using a colon before parameter name, for example :id indicates a placeholder for a parameter named id. The following example demonstrates how to write and execute a query using named parameters:
HQL doesn’t support regular INSERT statement (you know why - because the session.save(Object) method does it perfectly). So we can only write INSERT … SELECT query in HQL. The following code snippet executes a query that inserts all rows from Category table to OldCategory table:
String hql = "insert into Category (id, name)"
+ " select id, name from OldCategory";
Query query = session.createQuery(hql);
int rowsAffected = query.executeUpdate();
if (rowsAffected > 0) {
System.out.println(rowsAffected + "(s) were inserted");
}
Note that HQL is object-oriented, so Category and OldCategory must be mapped class names (not real table names).
Using the join keyword in HQL is called explicit join. Note that a JOIN query returns a list of Object arrays, so we need to deal with the result set differently:
List<Object[]> listResult = query.list();
HQL provides with keyword which can be used in case you want to supply extra join conditions. For example:
from Product p inner join p.category with p.price > 500
That joins the Product and Category together with a condition specifies that product’s price must be higher than 500.As stated earlier, we can write implicit join query which uses dot-notation. For example:
from Product where category.name = 'Computer'
That result in innerjoin in the resulting SQL statement.
A nice feature of Hibernate is that it is able to defer parameter type to generate the resulting SQL statement accordingly. So using date time parameters in HQL is quick and easy, for example:
String hql = "from Order where purchaseDate >= :beginDate and purchaseDate <= :endDate";
Query query = session.createQuery(hql);
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
Date beginDate = dateFormatter.parse("2014-11-01");
query.setParameter("beginDate", beginDate);
Date endDate = dateFormatter.parse("2014-11-22");
query.setParameter("endDate", endDate);
List<Order> listOrders = query.list();
for (Order anOrder : listOrders) {
System.out.println(anOrder.getProduct().getName() + " - "
+ anOrder.getAmount() + " - "
+ anOrder.getPurchaseDate());
}
The above query lists only orders whose purchase date is in a specified range.
For expressions used in the WHERE clause, HQL supports all basic arithmetic expressions similar to SQL include the following:
mathematical operators: +, -, *, /
binary comparison operators: =, >=, <=, <>, !=, like
logical operators: and, or, not
etc
For a complete list of expressions supported by Hibernate, click here.For example, the following query returns only products with price is ranging from 500 to 1000 dollars:
So far you have learn various code examples to use queries in Hibernate. I recommend you to take this course to learn how to use Hibernate for data access layer of a complete Java web application.
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.
Hi sir, I'm creating a Spring mvc web project same like the example you have given in this post. Now to obtain a student's name,age,department name and college name, what is the HQL statement ?
Comments
I'm creating a Spring mvc web project same like the example you have given in this post. Now to obtain a student's name,age,department name and college name, what is the HQL statement ?