Scrivio Interview library
ConceptIntermediate6 min read · updated 12 November 2026

What query features did Hibernate 6 and Jakarta Persistence 3.2 add to HQL and JPQL that previously required native SQL?

Hibernate 6 rewrote its query engine, and HQL gained features that used to force native SQL: set operations (union, intersect, except), common table expressions and window functions in later 6.x releases, subqueries in the from clause, joins on unrelated entities and direct instantiation of records and classes. Jakarta Persistence 3.2 standardises some of these in JPQL, including union, intersect and except and string concatenation with ||. Using them lets us replace native queries with portable ones, as long as we check that the dialect and our exact version support each feature.

Why this question is asked

Many codebases are full of native queries written years ago because JPQL could not express a union, a running total or a top-N per group. Each of those native queries is tied to one database, bypasses the entity mapping and has to be maintained by hand. Interviewers ask this question to see whether we have kept up with the ORM and can judge when to move a query back into HQL. In this answer, we will recap why native queries were needed, then go through set operations, CTEs and window functions, record instantiation and unrelated joins, see what Jakarta Persistence 3.2 standardised, and finish with the portability caveats.

1Why Native Queries Were Needed

Before Hibernate 6, HQL and JPQL covered selects, joins along associations, grouping and simple subqueries. Anything beyond that, such as combining two result sets, ranking rows within a group or a recursive query over a tree, needed a native query: SQL written for one database, run through createNativeQuery() or @Query(nativeQuery = true). Native queries work, but their results are not automatically checked against the mapping, renaming an attribute does not update them, and they lock the code to one database's syntax.

Hibernate 6 replaced the old query translator with a new semantic query model, and that rewrite made room for much more of SQL in HQL. Hibernate 6 implements Jakarta Persistence 3.1, and Hibernate 7 implements 3.2.

2Set Operations: Union, Intersect and Except

HQL in Hibernate 6 supports union, union all, intersect and except between queries with compatible select lists. A typical use is combining two kinds of records into one list.

Java
List<String> emails = session.createSelectionQuery("""
select c.email from Customer c where c.active = true
union
select s.email from NewsletterSubscriber s where s.confirmed = true""", String.class)
.getResultList();
// ["ada@example.com", "alan@example.com", "grace@example.com"], duplicates removed by union
// except: active customers who never ordered
// select c.id from Customer c except select o.customer.id from Order o

Jakarta Persistence 3.2 standardises union, intersect and except in JPQL, so on a 3.2 provider the same query is portable across JPA implementations, not only Hibernate.

3Common Table Expressions and Window Functions

A common table expression, CTE, is a named subquery declared with with at the start of a query; it makes complex queries readable and allows recursive queries over trees. A window function computes a value over a group of rows related to the current row without collapsing them, such as a rank within each category or a running total. Hibernate 6 added both to HQL in its later 6.x releases (check your version for exact support), along with subqueries in the from clause.

Java
// top 3 products by revenue in each category, entirely in HQL
with ranked as (
select p.category.name as category, p.name as product, sum(l.amount) as revenue,
rank() over (partition by p.category.name order by sum(l.amount) desc) as pos
from OrderLine l join l.product p
group by p.category.name, p.name
)
select r.category, r.product, r.revenue from ranked r where r.pos <= 3
// Books | Clean Code | 18400.00
// Books | Refactoring | 12950.00
// ...
Where each feature arrived
FeatureHibernate HQLStandard JPQL
union, intersect, exceptHibernate 6Jakarta Persistence 3.2
|| string concatenationHibernate 6Jakarta Persistence 3.2
CTEs (with), window functionslater Hibernate 6.x, check versionnot standard
Subqueries in fromHibernate 6not standard
Joins on unrelated entitiesHibernate 5.1 and latercheck your version
Record or class instantiationselect new; result type in Hibernate 6select new
Check the release notes of your exact version; support grew across 6.x and 7.x.

Before Hibernate 6, this query would have been native SQL. Notice that it still uses attribute paths such as p.category.name and l.product, so Hibernate generates the joins and the column names from the mapping.

4Records, Classes and Unrelated Joins

Selecting into a DTO has long worked with select new and a fully qualified class name. Hibernate 6 also lets us pass a record or class as the query's result type and maps the select list to its constructor, so the query string stays shorter. And joins between entities that have no mapped association, using an explicit on condition, cover cases such as joining an audit table by a plain id column.

Java
record CustomerTotal(String name, BigDecimal total) {}
List<CustomerTotal> totals = session.createSelectionQuery(
"select c.name, sum(o.total) from Customer c join c.orders o group by c.name",
CustomerTotal.class).getResultList(); // Hibernate 6 calls the record's constructor
// unrelated entities joined on a value
// select c.name, a.action from Customer c join AuditEntry a on a.customerId = c.id

Jakarta Persistence 3.2 also adds smaller conveniences that used to need functions or native SQL, such as the || operator for concatenation, left() and right(), replace() and cast(); check the specification for the full list.

5Portability and Dialect Support

Moving a native query into HQL is only a win if the result runs correctly on our database. Hibernate translates each feature for the dialect, but not every database supports every feature in the same way; MySQL, for example, only gained intersect and except in version 8.0.31, and older versions or other databases may get an emulation or an error. Function support also varies: Hibernate maps standard functions for each dialect, and anything else must be registered or called with the function() syntax.

So the practical approach is to try the HQL version against the real database in an integration test, for example with Testcontainers, compare the generated SQL and the plan with the old native query, and only then delete the native one. The benefits are worth it: type checking of attribute names at startup, refactoring safety, and one query that keeps working if we change database.

6Follow-up questions

Are native queries still needed with Hibernate 6?

Sometimes. Database-specific features such as PostgreSQL's full-text search operators, JSON path queries on some versions, or vendor hints are still easier in native SQL. The goal is to keep native queries for what is genuinely database specific, not for things HQL can now express.

Does Spring Data JPA support these queries in @Query?

Yes, the query string is passed to Hibernate, so any HQL that Hibernate supports works in @Query. Spring Data parses parts of the query for features like sorting and count queries, so very new syntax may need an explicit countQuery; check your version.

What is the difference between HQL and JPQL now?

JPQL is the standard subset defined by Jakarta Persistence. HQL is Hibernate's superset. Every JPQL query is valid HQL, but HQL features such as CTEs and window functions are not portable to other JPA providers.

7Conclusion

In this answer, we saw how Hibernate 6's new query engine brought set operations, CTEs, window functions, subqueries in from, record instantiation and joins on unrelated entities into HQL, and how Jakarta Persistence 3.2 standardises union, intersect, except and || concatenation in JPQL. These features let us replace many native queries with portable ones that use the entity mapping, provided we check dialect support and our exact version with integration tests.

What to say in the interviewAbout 90 seconds aloud

Open with

Hibernate 6 rewrote the query engine around a new semantic model, and HQL picked up a lot of SQL that used to force native queries. You can use set operations, union, union all, intersect and except.

Then, if they want more

In the later 6.x versions you also get common table expressions with the with clause and window functions such as rank over partition by, plus subqueries in the from clause, so a top-N per category query can stay in HQL and still use attribute paths.

Hibernate 6 can also instantiate records or classes directly from a select list when you pass the class as the result type, and it supports joins between unrelated entities with an on clause, which has been there since 5.1.

On the standard side, Jakarta Persistence 3.2, which Hibernate 7 implements, standardises union, intersect and except in JPQL, string concatenation with the double pipe, and a few functions like left, right, replace and cast. The benefit is replacing native queries with queries that are checked against the mapping, survive refactoring and run on any supported database.

The caveat is dialect support: not every database supports every feature, so I would check the exact version, compare the generated SQL and plans in a Testcontainers test, and keep native SQL only for genuinely database-specific features.

About this answer

Last updated
12 November 2026
Versions it names
Hibernate 6, Hibernate 7
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What query features did Hibernate 6 and Jakarta Persistence 3.2 add to HQL and JPQL that...": https://scrivio-interview-library.netlify.app/databases/jpa-queries/5aed01f381/