Single logo
  • Courses
  • Blog
  • About
Join on SkoolJoin on Skool

In this topic

Hibernate
  • SQLite and a JDBC Connection Pool in a Java Web App
  • Why an ORM: The Same UserDao in JDBC and in Hibernate
  • Hibernate 7 Setup: Three Dependencies, hibernate.cfg.xml and a First Session
  • Hibernate Entity Classes and CRUD With the Session: persist, find, a Setter and remove
  • Hibernate Inside a Servlet Web App: Replace the JDBC DAO, Keep Every Page
  • Spring MVC and SQLite Without Spring Boot: the Mini Blog in the Browser
  • Hibernate Entity Classes and CRUD With the Session: persist, find, a Setter and remove
  • Entities and H2 in Spring Boot: a Post Class Becomes a Table
  • JPA Entity Mapping: Ids, Enums and Dates, From a Java Class to a Table
  • HQL in Hibernate 7: List, Where With a Named Parameter, Bulk Update and Bulk Delete
  • The N+1 Problem in JPA: Fetch Types, Join Fetch and Entity Graphs
  1. Blog
  2. /
  3. Hibernate
  4. /
  5. Queries

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.

Chaand Sheikh

·27 November 2026·Updated 28 November 2026Queries

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

The HQL from User where email = :email becomes the SQL select u1_0.id, u1_0.email, u1_0.name from users u1_0 where u1_0.email = ?. User maps to users, the email field to the email column, and :email to a question mark. HQL you write SQL Hibernate writes from User where email = :email select u1_0.id,u1_0.email, u1_0.name from users u1_0 where u1_0.email=? Class User → table users, field email → column email, :email → ?
Hibernate is an ORM, so it knows which class maps to which table and which field to which column.

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 User with no where: Rows deleted: 3, Users left: 0.

Quick recap

GoalCallRun with
Read manycreateQuery("from User", User.class)getResultList()
Read one by a field... where email = :email + setParametergetSingleResult()
Change manycreateMutationQuery("update User ...")executeUpdate(), in a transaction
Delete manycreateMutationQuery("delete from User where ...")executeUpdate(), in a transaction

Keep going in Hibernate · 10 of 11

Previous
JPA Entity Mapping: Ids, Enums and Dates, From a Java Class to a Table
Next post
The N+1 Problem in JPA: Fetch Types, Join Fetch and Entity Graphs
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

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

Join free on Skool

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)

Related posts

The N+1 Problem in JPA: Fetch Types, Join Fetch and Entity Graphs

Relay's ticket list of 50 tickets quietly sends 51 queries. Count them with Hibernate statistics, bring it down to 1 with a left join fetch or an entity graph, and see why EAGER still sends 51.

On this page

  • HQL talks about classes
  • from User: every row
  • where with a named parameter
  • Bulk update: every match at once
  • Bulk delete with a where
  • Common mistakes
  • Quick recap
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

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

Join free on Skool

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)

Single logo

Empowering Education, One Course at a Time

About
About usContact us
Legal
Terms of UsePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.