HQL in Hibernate 7: List, Where With a Named Parameter, Bulk Update and Bulk Delete
HQL talks about classes, not tables. List every User, find Olivia with :email, move two users to a new email domain in one bulk update, then delete everyone after id 1, with the SQL Hibernate printed for each.
What you will learn
- Write HQL about the User class and see the SQL Hibernate generates
- List rows with getResultList and filter with a :named parameter
- Change many rows at once with a bulk update in a transaction
- Delete with a where clause and count what is left as a Long
Before you start
- Hibernate Entity Classes and CRUD With the Session
- Basic SQL WHERE, UPDATE and DELETE
session.find gets one row by its key. To get all users, or only the one with a certain email, you use HQL, the Hibernate Query Language. HQL is written about classes and fields, and Hibernate turns it into SQL about tables and columns.
HQL talks about classes
You can see that SQL because hibernate.show_sql is true in hibernate.cfg.xml; every statement prints with Hibernate: in front. The User entity is the same one as before: public class User with @Table(name = "users"), and fields name and email.
from User: every row
package org.studyeasy.hibernate;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Ex01_ListUsers {
public static void main(String[] args) {
SessionFactory factory =
new Configuration().configure().buildSessionFactory();
Session session = factory.openSession();
// HQL names the class User, not the table users
List<User> users = session.createQuery("from User", User.class)
.getResultList();
for (User user : users) {
System.out.println(user);
}
session.close();
factory.close();
}
}
Hibernate: select u1_0.id,u1_0.email,u1_0.name from users u1_0
User 1: Emma Clark <emma.clark@example.com>
User 2: Liam Turner <liam.turner@example.com>
User 3: Olivia Brooks <olivia.brooks@example.com>
from User with no where returns all three rows. User has a capital U because it's the class name. getResultList() gives you a real List<User>, with no ResultSet, no rs.next() and no getString.
where with a named parameter
package org.studyeasy.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Ex02_WhereNamedParameter {
public static void main(String[] args) {
SessionFactory factory =
new Configuration().configure().buildSessionFactory();
Session session = factory.openSession();
// :email is a named parameter, filled in by setParameter
User olivia = session.createQuery("from User where email = :email", User.class)
.setParameter("email", "[email protected]")
.getSingleResult();
System.out.println("Found: " + olivia);
session.close();
factory.close();
}
}
Hibernate: select u1_0.id,u1_0.email,u1_0.name from users u1_0 where u1_0.email=?
Found: User 3: Olivia Brooks <olivia.brooks@example.com>
:email is a named parameter and setParameter fills it. Why not glue the email into the string? Because that is exactly how SQL injection happens. The parameter travels separately, so it's safe. getSingleResult() expects exactly one row and throws if there's none, so use it only when you're sure.
Bulk update: every match at once
Liam and Olivia move to the StudyEasy team, so their emails change from example.com to studyeasy.org. Emma stays. A bulk update changes every matching row in one statement:
package org.studyeasy.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Ex03_BulkUpdate {
public static void main(String[] args) {
SessionFactory factory =
new Configuration().configure().buildSessionFactory();
Session session = factory.openSession();
session.beginTransaction(); // update and delete need a transaction
// one UPDATE for every row that matches the where: no User objects are loaded
int updated = session.createMutationQuery(
"update User set email = replace(email, :from, :to)"
+ " where id >= :id")
.setParameter("from", "@example.com")
.setParameter("to", "@studyeasy.org")
.setParameter("id", 2) // Liam and Olivia move teams, Emma stays
.executeUpdate();
System.out.println("Rows updated: " + updated);
session.getTransaction().commit();
session.close();
factory.close();
}
}
Hibernate: update users set email=replace(email,?,?) where id>=?
Rows updated: 2
Changes need a transaction. Changes that touch many rows use createMutationQuery, and no User objects are loaded at all. The condition id >= 2 picks Liam and Olivia; change that one value and a different set of rows is updated. executeUpdate() returns how many rows changed.
Bulk delete with a where
package org.studyeasy.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Ex04_BulkDelete {
public static void main(String[] args) {
SessionFactory factory =
new Configuration().configure().buildSessionFactory();
Session session = factory.openSession();
session.beginTransaction();
// one DELETE for every match: nothing is loaded first
int deleted = session.createMutationQuery("delete from User where id > :id")
.setParameter("id", 1)
.executeUpdate();
System.out.println("Rows deleted: " + deleted);
session.getTransaction().commit();
Long left = session.createQuery("select count(*) from User", Long.class)
.getSingleResult();
System.out.println("Users left: " + left);
session.close();
factory.close();
}
}
Hibernate: delete from users where id>?
Rows deleted: 2
Hibernate: select count(*) from users u1_0
Users left: 1
Every user after id 1 is deleted in one statement, with nothing loaded first. Read your delete twice: forget the where and every row goes. The count comes back as a Long, not an int. There's no import for Long because it lives in java.lang, which Java imports for you, like String.
Common mistakes
from users, the table name. HQL only knows classes:UnknownEntityException: Could not resolve root entity 'users'.- A parameter name the query doesn't have,
setParameter("mail", ...):IllegalArgumentException: No parameter named ':mail'. - Running a bulk update without a transaction:
TransactionRequiredException: No active transaction for update or delete query. delete from Userwith nowhere: Rows deleted: 3, Users left: 0.
Quick recap
| Goal | Call | Run with |
|---|---|---|
| Read many | createQuery("from User", User.class) | getResultList() |
| Read one by a field | ... where email = :email + setParameter | getSingleResult() |
| Change many | createMutationQuery("update User ...") | executeUpdate(), in a transaction |
| Delete many | createMutationQuery("delete from User where ...") | executeUpdate(), in a transaction |
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)