between predicatein predicateexists predicateHibernate 6 is a major redesign of the world’s most popular and feature-rich ORM solution.The redesign has touched almost every subsystem of Hibernate, including the APIs, mapping annotations, and, above all else, the query language.
This is the second time Hibernate Query Language has been completely reimplemented from scratch, but the first time in more than fifteen years.In this new incarnation, HQL is far more powerful, and the HQL compiler much more robust.
At long last, HQL has a feature set to match that of modern dialects of SQL, and is able to take full advantage of the power of modern SQL databases.
This document is a reference guide to the full feature set of the language, and is the only up-to-date source for those who wish to learn how to write HQL effectively in Hibernate 6.
If you are unfamiliar with Hibernate, be sure to first readIntroduction to Hibernate or check out theQuick Start.
This document describes Hibernate Query Language (HQL), which is, I suppose we could say, a "dialect" of the Java (now Jakarta) Persistence Query Language (JPQL).
Or is it the other way around? JPQL was inspired by early versions of HQL, and is a proper subset of modern HQL.Here we focus on describing the complete, more powerful HQL language as it exists today. If strict JPA compliance is what you’re looking for, use the setting We don’t recommend the use of this setting. |
The truth is that HQL today has capabilities that go far beyond what is possible in plain JPQL.We’re not going to fuss too much about not limiting ourselves to the standard here.Faced with a choice between writing database-specific native SQL, or database-independent HQL, we know what our preference is.
Throughout this document, we’ll assume you know SQL and the relational model, at least at a basic level.HQL and JPQL are loosely based on SQL and are easy to learn for anyone familiar with SQL.
For example, if you understand this SQL query:
selectbook.title,pub.name/* projection */fromBookasbook/* root table */joinPublisheraspub/* table join */onbook.publisherId=pub.id/* join condition */wherebook.titlelike'Hibernate%'/* restriction (selection) */orderbybook.title/* sorting */Then we bet you can already make sense of this HQL:
selectbook.title,pub.name/* projection */fromBookasbook/* root entity */joinbook.publisheraspub/* association join */wherebook.titlelike'Hibernate%'/* restriction (selection) */orderbybook.title/* sorting */You might notice that even for this very simple example, the HQL version is slightly shorter.This is typical.Actually, HQL queries are usually much more compact than the SQL they compile to.
But there’s one huge difference: in HQL, |
In this chapter, we’ll demonstrate how similar HQL is to SQL by giving a quick overview of the basic statement types.You’ll be bored to discover they’re exactly the ones you expect:select,insert,update, anddelete.
This is a reference guide.We’re not going to explain basic concepts like ternary logic, joins, aggregation, selection, or projection, because that information is freely available elsewhere, and anyway we couldn’t possibly do these topics justice here.If you don’t have a firm grasp of these ideas, it’s time to pick up a book about SQL or about the relational model. |
But first we need to mention something that’s a bit different to SQL.HQL has a slightly complicated way of dealing with case sensitively.
Lexically, JPQL is quite similar to SQL, so in this section we’ll limit ourselves to mentioning those places where it differs.
An identifier is a name used to refer to an entity, an attribute of a Java class, anidentification variable, or a function.
For example,Book,title,author, andupper are all identifiers, but they refer to different kinds of things.In HQL and JPQL, the case sensitivity of an identifier depends on the kind of thing the identifier refers to.
The rules for case sensitivity are:
keywords and function names are case-insensitive, but
identification variable names, Java class names, and the names of attributes of Java classes, are case-sensitive.
We apologize for this inconsistency.In hindsight, it might have been better to define the whole language as case-sensitive.
Incidentally, it’s standard practice to use lowercase keywords in HQL. The use of uppercase keywords indicates an endearing but unhealthy attachment to the culture of the 1970’s. |
Just to reiterate these rules:
| All the same, |
| Same, |
| Different, refer to different Java classes |
| Different, since the path expression element |
| All different, since the first element of a path expression is anidentification variable |
The JPQL specification defines identification variables as case-insensitive.And so in strict JPA-compliant mode, Hibernate treats |
Aquoted identifier is written in backticks. Quoting lets you use a keyword as an identifier.
selectthing.interval.`from`fromThingthingActually, in most contexts, HQL keywords are "soft", and don’t need to be quoted.The parser is usually able to distinguish if the reserved word is being used as a keyword or as an identifier.
Comments in HQL look like multiline comments in Java.They’re delimited by/* and*/.
Neither SQL-style-- nor Java-style// line-ending comments are allowed.
It’s quite rare to see comments in HQL, but perhaps it will be more common now that Java has text blocks.
Parameters come in two flavors in JPQL, and HQL supports a third flavor for historical reasons:
| Parameter type | Examples | Usage from Java |
|---|---|---|
Named parameters |
|
|
Ordinal parameters |
|
|
JDBC-style parameters 💀 |
|
|
JDBC-style parameters of form? are like ordinal parameters where the index is inferred from the position in the text of the query.JDBC-style parameters are deprecated.
It’sextremely important to use parameters to pass user input to the database.Constructing a query by concatenating HQL fragments with user input is extremely dangerous, opening the door to the possibility of executing arbitrary code on the database server. |
Some of the syntax for literal values also departs from the standard syntax in ANSI SQL, especially in the area of date/time literals, but we’ll discuss all that later, inLiterals.
We’ll describe the syntax of the language as we go along, sometimes displaying fragments of the grammar in an ANTLR-like BNF form.(Occasionally we’ll simplify these snippets for readability, so please don’t take them as canonical.)
The full canonical grammar for HQL can be found inthe github project.
The grammar for JPQL may be found in chapter 4 of the JPA specification.
JPA doesn’t have a well-specified type system, but, reading between the lines a bit, the following types may be discerned:
entity types,
numeric values,
strings,
dates/times,
booleans, and
enumerated types.
Such a coarse-grained type system is in some sense an insufficient constraint on implementors of the specification, or, viewed from a different perspective, it leaves us quite a lot of flexibility.
The way HQL interprets this type system is to assign a Java type to every expression in the language.Thus, numeric expressions have types likeLong,Float, orBigInteger, date/time expressions have types likeLocalDate,LocalDateTime, orInstant, and boolean expressions are always of typeBoolean.
Going further, an expression likelocal datetime - document.created is assigned the Java typejava.time.Duration, a type which doesn’t appear anywhere in the JPA specification.
Since the language must be executed on SQL databases, every type accommodates null values.
The SQLnull behaves quite differently to a null value in Java.
In Java, an expression likenumber + 1 produces in an exception ifnumber is null.
But in SQL, and therefore also in HQL and JPQL, such an expression evaluates tonull.
It’s almost always the case that an operation applied to a null value yields another null value.This rule applies to function application, to operators like The exceptions to this rule are the |
This rule is the source of the famous (and controversial)ternary logic of SQL.A logical expression likefirstName='Gavin' and team='Hibernate' isn’t restricted to the valuestrue andfalse.It may also benull.
This can, in principle, lead to some quite unintuitive results: we can’t use the law of the excluded middle to reason about logical expressions in SQL!But in practice, we’ve never once run into a case where this caused us problems.
As you probably know, when a logical predicate occurs as arestriction, rows for which the predicate evaluates tonull areexcluded from the result set.That is, in this context at least, a logical null is interpreted as "effectively false".
HQL features four different kinds of statement:
select queries,
update statements,
delete statements, and
insert … values andinsert … select statements.
Collectively,insert,update, anddelete statements are sometimes calledmutation queries.We need to be a little bit careful when executing mutation queries via a stateful session.
The effect of an It’s the responsibility of the client program to maintain synchronization of state held in memory with the database after execution of an |
Let’s consider each type of mutation query in turn, beginning with the most useful type.
TheBNF for anupdate statement is quite straightforward:
updateStatement : "UPDATE" "VERSIONED"? targetEntity setClause whereClause?targetEntity: entityName variable?setClause: "SET" assignment ("," assignment)*assignment : simplePath "=" expressionTheset clause has a list of assignments to attributes of the given entity.
For example:
updatePersonsetnickName='Nacho'wherename='Ignacio'Update statements are polymorphic, and affect mapped subclasses of the given entity class.Therefore, a single HQLupdate statement might result in multiple SQL update statements executed against the database.
Anupdate statement must be executed usingQuery.executeUpdate().
// JPA APIintupdatedEntities=entityManager.createQuery("update Person p set p.name = :newName where p.name = :oldName").setParameter("oldName",oldName).setParameter("newName",newName).executeUpdate();// Hibernate native APIintupdatedEntities=session.createMutationQuery("update Person set name = :newName where name = :oldName").setParameter("oldName",oldName).setParameter("newName",newName).executeUpdate();The integer value returned byexecuteUpdate() indicates the number of entity instances affected by the operation.
In a |
Anupdate statement, by default, does not affect the column mapped by the@Version attribute of the affected entities.
Adding the keywordversioned—writingupdate versioned—specifies that Hibernate should increment the version number or update the last modification timestamp.
updateversionedBooksettitle=:newTitlewheressn=:ssnAnupdate statement may not directlyjoin other entities, but it may:
have animplicit join, or
have subqueries in itsset clause, or in itswhere clause, and the subqueries may contain joins.
The BNF for adelete statement is even simpler:
deleteStatement : "DELETE" "FROM"? targetEntity whereClause?For example:
deleteAuthorauthorwhereisemptyauthor.booksAs in SQL, the presence or absence of thefrom keyword has absolutely no effect on the semantics of thedelete statement.
Just like update statements, delete statements are polymorphic, and affect mapped subclasses of the given entity class.Therefore, a single HQLdelete statement might result in multiple SQL delete statements executed against the database.
Adelete statement is executed by callingQuery.executeUpdate().
The integer value returned byexecuteUpdate() indicates the number of entity instances affected by the operation.
Adelete statement may not directlyjoin other entities, but it may:
have animplicit join, or
have subqueries in itswhere clause, and the subqueries may contain joins.
There are two kinds ofinsert statement:
insert … values, where the attribute values to insert are given directly as tuples, and
insert … select, where the inserted attribute values are sourced from a subquery.
The first form inserts a single row in the database, or multiple rows if you provide multiple tuples in thevalues clause.The second form may insert many new rows, or none at all.
The first sort of But you might consider using it to set up test data. |
|
The BNF for aninsert statement is:
insertStatement : "INSERT" "INTO"? targetEntity targetFields (queryExpression | valuesList) conflictClause?targetEntity: entityName variable?targetFields: "(" simplePath ("," simplePath)* ")"valuesList: "VALUES" values ("," values)*values: "(" expression ("," expression)* ")"For example:
insertPerson(id,name)values(100L,'Jane Doe'),(200L,'John Roe')insertintoAuthor(id,name,bio)selectid,name,name||' is a newcomer for '||str(year(localdate))fromPersonwhereid=:pidAs in SQL, the presence or absence of theinto keyword has no effect on the semantics of theinsert statement.
From these examples we might notice thatinsert statements are in one respect a bit different toupdate anddelete statements.
An |
ThequeryExpression in aninsert … select statement may be any validselect query, with the caveat that the types of the values in theselect list must match the types of the target fields.
This is checked during query compilation rather than allowing the type check to delegate to the database.This may cause problems when two Java types map to the same database type.For example, an attribute of type |
There are two ways to assign a value to the@Id attribute:
explicitly specify the id attribute in the list of target fields, and its value in the values assigned to the target fields, or
omit it, in which case a generated value is used.
Of course, the second option is only available for entities with database-level id generation (sequences or identity/autoincrement columns).It’s not available for entities whose id generator is implemented in Java, nor for entities whose id is assigned by the application.
The same two options are available for a@Version attribute.When no version is explicitly specified, the version for a new entity instance is used.
Theon conflict clause lets us specify what action should be taken when the database already contains the record we’re attempting to insert.
conflictClause: ON CONFLICT conflictTarget? "DO" conflictActionconflictTarget: ON CONSTRAINT identifier| "(" simplePath ("," simplePath)* ")"conflictAction: "NOTHING"| "UPDATE" setClause whereClause?Note that theon constraint variant accepting the name of a unique constraint only works on certain databases, or when just a single row is being inserted.
insertPerson(ssn,name,phone)values('116-76-1234','Jane Doe','404 888 4319')onconflict(ssn)doupdatesetphone=excluded.phoneLikeupdate anddelete statements, aninsert statement must be executed by callingQuery.executeUpdate().
Now it’s time to look at somethingmuch more complicated.
Select statements retrieve and analyse data.This is what we’re really here for.
The full BNF for aselect query is quite complicated, but there’s no need to understand it now.We’re displaying it here for future reference.
selectStatement: queryExpressionqueryExpression: withClause? orderedQuery (setOperator orderedQuery)*orderedQuery: (query | "(" queryExpression ")") queryOrder?query: selectClause fromClause? whereClause? (groupByClause havingClause?)?| fromClause whereClause? (groupByClause havingClause?)? selectClause?| whereClausequeryOrder: orderByClause limitClause? offsetClause? fetchClause?fromClause: "FROM" entityWithJoins ("," entityWithJoins)*entityWithJoins: fromRoot (join | crossJoin | jpaCollectionJoin)*fromRoot: entityName variable?| "LATERAL"? "(" subquery ")" variable?join: joinType "JOIN" "FETCH"? joinTarget joinRestriction?joinTarget: path variable?| "LATERAL"? "(" subquery ")" variable?withClause: "WITH" cte ("," cte)*;Most of the complexity here arises from the interplay of set operators (union,intersect, andexcept) with sorting.
We’ll describe the various clauses of a query later, inRoot entities and joins and inSelection, projection, and aggregation, but for now, to summarize, a query might have these bits:
| Clause | Jargon | Purpose |
|---|---|---|
| Common table expressions | Declaresnamed subqueries to be used in the following query |
| Roots and joins | Specifies the entities involved in the query, and how they’rerelated to each other |
| Selection/restriction | Specifies arestriction on the data returned by the query |
| Aggregation/grouping | Controlsaggregation |
| Selection/restriction | Specifies arestriction to applyafter aggregation |
| Projection | Specifies aprojection (the things to return from the query) |
| Set algebra | These areset operators applied to the results of multiple subqueries |
| Ordering | Specifies how the results should besorted |
| Limits | Allows forlimiting or paginating the results |
Every one of these clauses is optional!
For example, the simplest query in HQL has noselect clause at all:
fromBookBut we don’t necessarilyrecommend leaving off theselect list.
HQL doesn’t require a |
Naturally, the previous query may be written with aselect clause:
selectbookfromBookbookBut when there’s no explicitselect clause, the select list is implied by the result type of the query:
// result type Book, only the Book selectedList<Book>books=session.createQuery("from Book join authors",Book.class).getResultList();for(Bookbook:books){...}// result type Object[], both Book and Author selectedList<Object[]>booksWithAuthors=session.createQuery("from Book join authors",Book.class,Object[].class).getResultList();for(varbookWithAuthor:booksWithAuthors){Bookbook=(Book)bookWithAuthor[0];Authorauthor=(Author)bookWithAuthor[1];...}For complicated queries, it’s probably best to explicitly specify aselect list.
An alternative "simplest" query hasonly aselect list:
selectlocaldatetimeThis results in a SQLfrom dual query (or equivalent).
Looking carefully at the BNF given above, you might notice that the Of course, standard SQL, and JPQL, require that the This form of the query is more readable, because the alias is declaredbefore it’s used, just as God and nature intended. |
Naturally, queries are always polymorphic.Indeed, a fairly innocent-looking HQL query can easily translate to a SQL statement with many joins and unions.
We need to be abit careful about that, but actually it’s usually a good thing.HQL makes it very easy to fetch all the data we need in a single trip to the database, and that’s absolutely key to achieving high performance in data access code.Typically, it’s much worse to fetch exactly the data we need, but in many round trips to the database server, than it is to fetch just a bit more data than what we’re going to need, all a single SQL query. |
When there’s no explicitselect clause, a further abbreviation is sometimes possible.
When the result type of a |
One of the most uncomfortable aspects of working with data in Java is that there’s no good way to represent a table.Languages designed for working with data—R is an excellent example—always feature some sort of built-in table or "data frame" type.Of course, Java’s type system gets in the way here.This problem is much easier to solve in a dynamically-typed language.The fundamental problem for Java is that it doesn’t have tuple types.
Queries in Hibernate return tables.Sure, often a column holds whole entity objects, but we’re not restricted to returning a single entity, and we often write queries that return multiple entities in each result, or which return things which aren’t entities.
So we’re faced with the problem if representing such result sets, and, we’re sad to say, there’s no fully general and completely satisfying solution.
Let’s begin with the easy case.
If there’s just one projected item in theselect list, then, no sweat, that’s the type of each query result.
List<String>results=entityManager.createQuery("select title from Book",String.class).getResultList();There’s really no need to fuss about with trying to represent a "tuple of length 1".We’re not even sure what to call those.
Problems arise as soon as we have multiple items in theselect list of a query.
When there are multiple expressions in the select list then, by default, and in compliance with JPA, each query result is packaged as an array of typeObject[].
List<Object[]>results=entityManager.createQuery("select title, left(book.text, 200) from Book",Object[].class).getResultList();for(varresult:results){Stringtitle=(String)result[0];Stringpreamble=(String)result[1];}This is bearable, but let’s explore some other options.
JPA lets us specify that we want each query result packaged as an instance ofjakarta.persistence.Tuple.All we have to do is pass the classTuple tocreateQuery().
List<Tuple>tuples=entityManager.createQuery("select title as title, left(book.text, 200) as preamble from Book",Tuple.class).getResultList();for(Tupletuple:tuples){Stringtitle=tuple.get("title",String.class);Stringpreamble=tuple.get("preamble",String.class);}The names of theTuple elements are determined by the aliases given to the projected items in the select list.If no aliases are specified, the elements may be accessed by their position in the list, where the first item is assigned the position zero.
As an extension to JPA, and in a similar vein, Hibernate lets us passMap orList, and have each result packaged as a map or list:
varresults=entityManager.createQuery("select title as title, left(book.text, 200) as preamble from Book",Map.class).getResultList();for(varmap:results){Stringtitle=(String)map.get("title");Stringpreamble=(String)map.get("preamble");}varresults=entityManager.createQuery("select title, left(book.text, 200) from Book",List.class).getResultList();for(varlist:results){Stringtitle=(String)list.get(0);Stringpreamble=(String)list.get(1);}Unfortunately, not one of the typesObject[],List,Map, norTuple lets us access an individual item in a result tuple without a type cast.SureTuple does the type cast for us when we pass a class object toget(), but it’s logically identical.Fortunately there’s one more option, as we’re about to see.
Actually, |
Hibernate 6 lets us pass an arbitrary class type with an appropriate constructor tocreateQuery() and will use it to package the query results.This works extremely nicely withrecord types.
recordBookSummary(Stringtitle,Stringsummary){}List<BookSummary>results=entityManager.createQuery("select title, left(book.text, 200) from Book",BookSummary.class).getResultList();for(varresult:results){Stringtitle=result.title();Stringpreamble=result.summary();}It’s important that the constructor ofBookSummary has parameters which exactly match the items in theselect list.
This class does not need to be mapped or annotated in any way. Even if the classis an entity class, the resulting instances arenot managed entities and arenot associated with the session. |
We must caution that this still isn’t typesafe.In fact, we’ve just pushed the typecasts down into the call tocreateQuery().But at least we don’t have to write them explicitly.
In JPQL, and in older versions of Hibernate, this functionality required more ceremony.
| Result type | Legacy syntax | Streamlined syntax | JPA standard |
|---|---|---|---|
|
|
| ✖/✖ |
|
|
| ✖/✖ |
Arbitrary class |
|
| ✔/✖ |
For example, the JPA-standardselect new construct packages the query results into a user-written Java class instead of an array.
recordBookSummary(Stringtitle,Stringsummary){}List<BookSummary>results=entityManager.createQuery("select new BookSummary(title, left(book.text, 200)) from Book",BookSummary.class).getResultList();for(varresult:results){Stringtitle=result.title();Stringpreamble=result.summary();}Simplifying slightly, the BNF for a projected item is:
selection : (expression | instantiation) alias?instantiation : "NEW" instantiationTarget "(" selection ("," selection)* ")"alias : "AS"? identifierWhere the list ofselections in aninstantiation is essentially a nested projection list.
We now switch gears, and begin describing the language from the bottom up.The very bottom of a programming language is its syntax for literal values.
The most important literal value in this language isnull. It’s assignable to any other type.
The boolean literal values are the (case-insensitive) keywordstrue andfalse.
String literals are enclosed in single quotes.
select'hello world'To escape a single quote within a string literal, use a doubled single quote:''.
fromBookwheretitlelike'Ender''s'Alternatively, Java-style double-quoted strings are also allowed, with the usual Java character escape syntax.
select"hello\tworld"This option is not much used.
Numeric literals come in several different forms:
| Kind | Type | Example |
|---|---|---|
Integer literals |
|
|
Decimal literals |
|
|
Hexadecimal literals |
|
|
Scientific notation |
|
|
For example:
fromBookwhereprice<100.0selectauthor,count(book)fromAuthorasauthorjoinauthor.booksasbookgroupbyauthorhavingcount(book)>10The type of a numeric literal may be specified using a Java-style postfix:
| Postfix | Type | Java type |
|---|---|---|
| long integer |
|
| double precision |
|
| single precision |
|
| large integer |
|
| exact decimal |
|
It’s not usually necessary to specify the precision explicitly.
In a literal with an exponent, the |
According to the JPQL specification, date and time literals may be specified using the JDBC escape syntax.Since this syntax is rather unpleasant to look at, HQL provides not one, but two alternatives.
| Date/time type | Recommended Java type | JDBC escape syntax 💀 | Braced literal syntax | Explicitly typed literal syntax |
|---|---|---|---|---|
Date |
|
|
|
|
Time |
|
|
|
|
Time with seconds |
|
|
|
|
Datetime |
|
|
|
|
Datetime with milliseconds |
|
|
|
|
Datetime with an offset |
|
|
|
|
Datetime with a time zone |
|
|
|
|
Literals referring to the current date and time are also provided.Again there is some flexibility.
| Date/time type | Java type | Underscored syntax | Spaced syntax |
|---|---|---|---|
Date |
|
|
|
Time |
|
|
|
Datetime |
|
|
|
Offset datetime |
|
|
|
Instant |
|
|
|
Date |
|
|
|
Time |
|
|
|
Datetime |
|
|
|
Of these, onlylocal date,local time,local datetime,current_date,current_time, andcurrent_timestamp are defined by the JPQL specification.
The use of date and time types from the |
There are two sorts of duration in HQL:
year-day durations, that is, the length of an interval between two dates, and
week-nanosecond durations, that is, the length of an interval between two datetimes.
For conceptual reasons, the two kinds of duration cannot be cleanly composed.
Literal duration expressions are of formn unit, for example1 day or10 year or100 nanosecond.
selectstart,end,start-30minutefromEventThe unit may be:day,week,month,quarter,year,second,minute,hour, ornanosecond.
A HQL duration is considered to map to a Java |
HQL also provides a choice of formats for binary strings:
the braced syntax{0xDE, 0xAD, 0xBE, 0xEF}, a list of Java-style hexadecimal byte literals, or
the quoted syntaxX’DEADBEEF' orx’deadbeef', similar to SQL.
Literal values of a Java enumerated type may be written without needing to specify the enum class name:
fromBookwherestatus<>OUT_OF_PRINTfromBookwheretypein(BOOK,MAGAZINE)updateBooksetstatus=OUT_OF_PRINTIn the examples above, the enum class is inferred from the type of the expression on the left of the comparison, assignment operator, orin predicate.
fromBookorderbycasetypewhenBOOKthen1whenMAGAZINEthen2whenJOURNALthen3else4endIn this example the enum class is inferred from the type of thecase expression.
HQL allows any Javastatic constant to be used in HQL, but it must be referenced by its fully-qualified name:
selectjava.lang.Math.PIEntity names may also occur as a literal value. They do not need to be qualified.
fromPaymentaspaymentwheretype(payment)=CreditCardPaymentA path expression is either:
a reference to anidentification variable, or
acompound path, beginning with a reference to an identification variable, and followed by a period-separated list of references to entity attributes.
As an extension to the JPA spec, HQL, just like SQL, allows a compound path expression where the identification variable at the beginning of the path is missing.That is, instead ofvar.foo.bar, it’s legal to write justfoo.bar.But this is only allowed when the identification variable may be unambiguously inferred from the first element,foo of the compound path.The query must have exactly one identification variablevar for which the pathvar.foo refers to an entity attribute.Note that we will continue to call these paths "compound", even if they only have one element.
This streamlines the query rather nicely when there’s just one root entity and no joins.But when the query has multiple identification variables it makes the query much harder to understand. |
If an element of a compound path refers to an association, the path expression produces animplicit join.
selectbook.publisher.namefromBookbookAn element of a compound path referring to a many-to-one or on-to-one association may have thetreat function applied to it.
selecttreat(order.paymentasCreditCardPayment).creditCardNumberfromOrderorderIf an element of a compound path refers to a collection or many-valued association, it must have one ofthese special functions applied to it.
selectelement(book.authors).namefromBookbookNo other function may be applied to a non-terminal element of a path expression.
Alternatively, if the element of the compound path refers to a list or map, it may have the indexing operator applied to it:
selectbook.editions[0].datefromBookbookNo other operator may be applied to a non-terminal element of a path expression.
HQL has operators for working with strings, numeric values, and date/time types.
The operator precedence is given by this table, from highest to lowest precedence:
| Precedence class | Type | Operators |
|---|---|---|
Grouping and tuple instantiation |
| |
Case lists |
| |
Member reference | Binary infix |
|
Function application | Postfix |
|
Indexing | Postfix |
|
Unary numeric | Unary prefix |
|
Duration conversions | Unary postfix |
|
Binary multiplicative | Binary infix |
|
Binary additive | Binary infix |
|
Concatenation | Binary infix |
|
Nullness, emptiness, truth | Unary postfix |
|
Containment | Binary infix |
|
Between | Ternary infix |
|
Pattern matching | Binary infix |
|
Comparison operators | Binary infix |
|
Nullsafe comparison | Binary infix |
|
Existence | Unary prefix |
|
Membership | Binary infix |
|
Logical negation | Unary prefix |
|
Logical conjunction | Binary infix |
|
Logical disjunction | Binary infix |
|
HQL defines two ways to concatenate strings:
the SQL-style concatenation operator,||, and
the JPQL-standardconcat() function.
Seebelow for details of theconcat() function.
selectbook.title||' by '||listagg(author.name,' & ')fromBookasbookjoinbook.authorsasauthorgroupbybookMany more operations on strings are defined below, inFunctions.
The basic SQL arithmetic operators,+,-,*, and/ are joined by the remainder operator%.
select(1.0+:taxRate)*sum(item.book.price*item.quantity)fromOrderasordjoinord.itemsasitemwhereord.id=:oidWhen both operands of a binary numeric operator have the same type, the result type of the whole expression is the same as the operands.
By default, the semantics of integer division depend on the database:
This default behavior may be changed using configuration property |
When the operands are of different type, one of the operands is implicitly converted towider type, with wideness given, in decreasing order, by the list below:
Double (widest)
Float
BigDecimal
BigInteger
Long
Integer
Short
Byte
Many more numeric operations are defined below, inFunctions.
Arithmetic involving dates, datetimes, and durations is quite subtle.Among the issues to consider are:
There’s two kinds of duration: year-day, and week-nanosecond durations.The first is a difference between dates; the second is a difference between datetimes.
We can subtract dates and datetimes, but we can’t add them.
A Java-style duration has much too much precision, and so in order to use it for anything useful, we must somehow truncate it to something coarser-grained.
Here we list the basic operations.
| Operator | Expression type | Example | Resulting type |
|---|---|---|---|
| Difference between two dates |
| year-day duration |
| Difference between two datetimes |
| week-nanosecond duration |
| Difference of a date and a year-day duration |
| date |
| Difference of a datetime and a week-nanosecond duration |
| datetime |
| Difference between two durations |
| duration |
| Sum of a date and a year-day duration |
| date |
| Sum of a datetime and a week-nanosecond duration |
| datetime |
| Sum of two durations |
| duration |
| Product of an integer and a duration |
| duration |
| Convert a duration to an integer |
| integer |
Theby unit operator converts a duration to an integer, for example:(local date - your.birthday) by day evaluates to the number of days you still have to wait.
The functionextract(unit from …) extracts a field from a date, time, or datetime type, for example,extract(year from your.birthday) produces the year in which you were born, and throws away important information about your birthday.
Please carefully note the difference between these two operations: |
Additional datetime operations, including the usefulformat() function, are defined below, inFunctions.
Just like in standard SQL, there are two forms of case expression:
thesimple case expression, and
the so-calledsearched case expression.
Case expressions are verbose.It’s often simpler to use the |
The syntax of the simple form is defined by:
"CASE" expression ("WHEN" expression "THEN" expression)+ ("ELSE" expression)? "END"For example:
selectcaseauthor.nomDePlumewhen''thenperson.nameelseauthor.nomDePlumeendfromAuthorasauthorjoinauthor.personaspersonThe searched form has the following syntax:
"CASE" ("WHEN" predicate "THEN" expression)+ ("ELSE" expression)? "END"For example:
selectcasewhenauthor.nomDePlumeisnullthenperson.nameelseauthor.nomDePlumeendfromAuthorasauthorjoinauthor.personaspersonAcase expression may contain complex expression, including operator expressions.
Atuple instantiation is an expression like(1, 'hello'), and may be used to "vectorize" comparison expressions.
fromPersonwhere(firstName,lastName)=('Ludwig','Boltzmann')fromEventwhere(year,day)>(year(localdate),day(localdate))This syntax may be used even when the underlying SQL dialect doesnot support so-called "row value" constructors.
A tuple value may be compared to an embedded field:
fromPersonwhereaddress=('1600 Pennsylvania Avenue, NW','Washington','DC',20500,'USA')Both HQL and JPQL define some standard functions and make them portable between databases.
A program that wishes to remain portable between Jakarta Persistence providers should in principle limit itself to the use of the functions which are blessed by the specification.Unfortunately, there’s not so many of them. |
In some cases, the syntax of these functions looks a bit funny at first, for example,cast(number as String), orextract(year from date), or eventrim(leading '.' from string).This syntax is inspired by standard ANSI SQL, and we promise you’ll get used to it.
HQL abstracts away from the actual database-native SQL functions, letting you write queries which are portable between databases. For some functions, and always depending on the database, a HQL function invocation translates to a quite complicated SQL expression! |
In addition, there are several ways to use a database function that’s not known to Hibernate.
The following special functions make it possible to discover or narrow expression types:
| Special function | Purpose | Signature | JPA standard |
|---|---|---|---|
| The (concrete) entity or embeddable type |
| ✔ |
| Narrow an entity or embeddable type |
| ✔ |
| Narrow a basic type |
| ✖ |
| Cast to a string |
| ✖ |
Let’s see what these functions do.
The functiontype(), applied to an identification variable or to an entity-valued or embeddable-valued path expression, evaluates to the concrete type, that is, the JavaClass, of the referenced entity or embeddable.This is mainly useful when dealing with entity inheritance hierarchies.
selectpaymentfromPaymentaspaymentwheretype(payment)=CreditCardPaymentThe functiontreat() may be used to narrow the type of an identification variable.This is useful when dealing with entity or embeddable inheritance hierarchies.
selectpaymentfromPaymentaspaymentwherelength(treat(paymentasCreditCardPayment).cardNumber)between16and20The type of the expressiontreat(p as CreditCardPayment) is the narrowed type,CreditCardPayment, instead of the declared typePayment ofp.This allows the attributecardNumber declared by the subtypeCreditCardPayment to be referenced.
The first argument is usually an identification variable.
The second argument is the target type given as an unqualified entity name.
Thetreat() function may even occur in ajoin.
The functioncast() has a similar syntax, but is used to narrow basic types.
Its first argument is usually an attribute of an entity, or a more complex expression involving entity attributes.
Its second argument is the target type given as an unqualified Java class name:String,Long,Integer,Double,Float,Character,Byte,BigInteger,BigDecimal,LocalDate,LocalTime,LocalDateTime, etc.
selectcast(idasString)fromOrderThe functionstr(x) is a synonym forcast(x as String).
selectstr(id)fromOrderThe following functions make it easy to deal with null values:
| Function | Purpose | Signature | JPA standard |
|---|---|---|---|
| First non-null argument |
| ✔ |
| Second argument if first is null |
| ✖ |
|
|
| ✔ |
Thecoalesce() function is a sort of abbreviatedcase expression that returns the first non-null operand.
selectcoalesce(author.nomDePlume,person.name)fromAuthorasauthorjoinauthor.personaspersonHQL allowsifnull() as a synonym forcoalesce() in the case of exactly two arguments.
selectifnull(author.nomDePlume,person.name)fromAuthorasauthorjoinauthor.personaspersonOn the other hand,nullif() evaluates to null if its operands are equal, or to its first argument otherwise.
selectifnull(nullif(author.nomDePlume,person.name),'Real name')fromAuthorasauthorjoinauthor.personaspersonThere are some very important functions for working with dates and times.
| Special function | Purpose | Signature | JPA standard |
|---|---|---|---|
| Extract a datetime field |
| ✔ |
| Format a datetime as a string |
| ✖ |
| Datetime truncation |
| ✖ |
The special functionextract() obtains a single field of a date, time, or datetime.
Its first argument is an expression that evaluates to a date, time, or datetime.
Its second argument is a date/timefield type.
Recognized Field types are listed below.
| Field | Type | Range | Notes | JPA standard |
|---|---|---|---|---|
|
| 1-31 | Calendar day of month | ✔ |
|
| 1-12 | ✔ | |
|
| ✔ | ||
|
| 1-53 | ISO-8601 week number (different to | ✔ |
|
| 1-4 | Quarter defined as 3 months | ✔ |
|
| 0-23 | Standard 24-hour time | ✔ |
|
| 0-59 | ✔ | |
|
| 0-59 | Includes fractional seconds | ✔ |
|
| Granularity varies by database | ✖ | |
|
| 1-7 | ✖ | |
|
| 1-31 | Synonym for | ✖ |
|
| 1-365 | ✖ | |
|
| 1-5 | ✖ | |
|
| 1-53 | ✖ | |
|
| Elapsed seconds since January 1, 1970 | ✖ | |
|
| Date part of a datetime | ✔ | |
|
| Time part of a datetime | ✔ | |
|
| Timezone offset | ✖ | |
|
| Hours of offset | ✖ | |
|
| 0-59 | Minutes of offset | ✖ |
For a full list of field types, see the Javadoc forTemporalUnit.
fromOrderwhereextract(datefromcreated)=localdateselectextract(yearfromcreated),extract(monthfromcreated)fromOrderThe following functions are abbreviations forextract():
| Function | Long form usingextract() | JPA standard |
|---|---|---|
|
| ✖ |
|
| ✖ |
|
| ✖ |
|
| ✖ |
|
| ✖ |
|
| ✖ |
| These abbreviations aren’t part of the JPQL standard, but on the other hand they’re a lot less verbose. |
selectyear(created),month(created)fromOrderTheformat() function formats a date, time, or datetime according to a pattern.
Its first argument is an expression that evaluates to a date, time, or datetime.
Its second argument is a formatting pattern, given as a string.
The pattern must be written in a subset of the pattern language defined by Java’sjava.time.format.DateTimeFormatter.
selectformat(localdatetimeas'yyyy-MM-dd HH:mm:ss')For a full list offormat() pattern elements, see the Javadoc forDialect.appendDatetimeFormat.
Thetruncate() function truncates the precision of a date, time, or datetime to the temporal unit specified by field type.
Its first argument is an expression that evaluates to a date, time, or datetime.
Its second argument is a date/time field type, specifying the precision of the truncated value.
Supported temporal units are:year,month,day,hour,minute orsecond.
selecttrunc(localdatetime,hour)Truncating a date, time or datetime value means obtaining a value of the same type in which all temporal units smaller thanfield have been pruned.For hours, minutes and second this means setting them to00. For months and days, this means setting them to01.
Naturally, there are a good number of functions for working with strings.
| Function | Purpose | Syntax | JPA standard / ANSI SQL Standard |
|---|---|---|---|
| The string, with lowercase characters converted to uppercase |
| ✔ / ✔ |
| The string, with uppercase characters converted to lowercase |
| ✔ / ✔ |
| The length of the string |
| ✔ / ✖ |
| Concatenate strings |
| ✔ / ✖ |
| Location of string within a string |
| ✔ / ✖ |
| Similar to |
| ✖ / ✔ |
| Substring of a string (JPQL-style) |
| ✔ / ✖ |
| Substring of a string (ANSI SQL-style) |
| ✖ / ✔ |
| Trim characters from string |
| ✔ / ✔ |
| For replacing a substring |
| ✖ / ✔ |
| Pads a string with whitespace, or with a specified character |
| ✖ / ✖ |
| The leftmost characters of a string |
| ✖ / ✖ |
| The rightmost characters of a string |
| ✖ / ✖ |
| Replace every occurrence of a pattern in a string |
| ✖ / ✖ |
| Concatenate a string with itself multiple times |
| ✖ / ✖ |
| Select a collation |
| ✖ / ✖ |
Let’s take a closer look at just some of these.
Contrary to Java, positions of characters within strings are indexed from 1 instead of 0! |
The JPQL-standard and ANSI SQL-standardconcat() function accepts a variable number of arguments, and produces a string by concatenating them.
selectconcat(book.title,' by ',listagg(author.name,' & '))fromBookasbookjoinbook.authorsasauthorgroupbybookThe JPQL functionlocate() determines the position of a substring within another string.
The first argument is the pattern to search for within the second string.
The second argument is the string to search in.
The optional third argument is used to specify a position at which to start the search.
selectlocate('Hibernate',title)fromBookTheposition() function has a similar purpose, but follows the ANSI SQL syntax.
selectposition('Hibernate'intitle)fromBookUnsurprisingly,substring() returns a substring of the given string.
The second argument specifies the position of the first character of the substring.
The optional third argument specifies the maximum length of the substring.
selectsubstring(title,1,position(' for Dummies'intitle))fromBook/* JPQL-style */selectsubstring(titlefrom1forposition(' for Dummies'intitle))fromBook/* ANSI SQL-style */Thetrim() function follows the syntax and semantics of ANSI SQL.It may be used to trimleading characters,trailing characters, or both.
selecttrim(title)fromBookselecttrim(trailing' 'fromtext)fromBookIts BNF is funky:
"TRIM" "(" (("LEADING" | "TRAILING" | "BOTH")? trimCharacter? "FROM")? expression ")" ;Thepad() function has a syntax inspired bytrim().
selectconcat(pad(b.titlewith40trailing'.'),pad(a.firstNamewith10leading),pad(a.lastNamewith10leading))fromBookasbjoinb.authorsasaIts BNF is given by:
"PAD" "(" expression "WITH" expression ("LEADING" | "TRAILING") padCharacter? ")"Thecollate() function selects a collation to be used for its string-valued argument.Collations are useful forbinary comparisons with< or>, and in theorder by clause.
For example,collate(p.name as ucs_basic) specifies the SQL standard collationucs_basic.
| Collations aren’t very portable between databases. |
Some PostgreSQL collation names must be quoted with backticks, for example,collate(name as `zh_TW.UTF-8`). |
The@Collate annotation may be used to specify the collation of a column, which is usually more convenient than using thecollate() function. |
Of course, we also have a number of functions for working with numeric values.
| Function | Purpose | Signature | JPA standard |
|---|---|---|---|
| The magnitude of a number |
| ✔ |
| The sign of a number |
| ✔ |
| Remainder of integer division |
| ✔ |
| Square root of a number |
| ✔ |
| Exponential function |
| ✔ |
| Exponentiation |
| ✔ |
| Natural logarithm |
| ✔ |
| Numeric rounding |
| ✔ |
| Numeric truncation |
| ✖ |
| Floor function |
| ✔ |
| Ceiling function |
| ✔ |
| Base-10 logarithm |
| ✖ |
| Arbitrary-base logarithm |
| ✖ |
| π |
| ✖ |
| Basic trigonometric functions |
| ✖ |
| Two-argument arctangent (range |
| ✖ |
| Hyperbolic functions |
| ✖ |
| Convert radians to degrees |
| ✖ |
| Convert degrees to radians |
| ✖ |
| Return the smallest of the given arguments |
| ✖ |
| Return the largest of the given arguments |
| ✖ |
| Bitwise functions |
| ✖ |
We haven’t includedaggregate functions,ordered set aggregate functions, orwindow functions in this list, because their purpose is more specialized, and because they come with extra special syntax.
The functions described in this section are especially useful when dealing with@ElementCollection mappings, or with collection mappings involving an@OrderColumn or@MapKeyColumn.
The following functions accept either:
an identification variable that refers to ajoined collection or many-valued association, or
acompound path that refers to a collection or many-valued association of an entity.
In case 2, application of the function produces animplicit join.
| Function | Applies to | Purpose | JPA standard |
|---|---|---|---|
| Any collection | The size of a collection | ✔ |
| Any collection | The element of a set or list | ✖ |
| Lists | The index of a list element | ✔ |
| Maps | The key of a map entry | ✔ |
| Maps | The value of a map entry | ✔ |
| Maps | The whole entry in a map | ✔ |
The next group of functions always accept a compound path referring to a collection or many-valued association of an entity.They’re interpreted as referring to the collection as a whole.
| Function | Applies to | Purpose | JPA standard |
|---|---|---|---|
| Any collection | The elements of a set or list, collectively | ✖ |
| Lists | The indexes of a list, collectively | ✖ |
| Maps | The keys of a map, collectively | ✖ |
| Maps | The values of a map, collectively | ✖ |
Application of one of these function produces an implicit subquery or implicit join.
This query has an implicit join:
selecttitle,element(tags)fromBookThis query has an implicit subquery:
selecttitlefromBookwhere'hibernate'inelements(tags)It never makes sense to apply the functionselements(),indices(),keys(), orvalues() to an identification variable or single-valued path expression.These functions must be applied to a reference to a many-valued path expression. |
Thesize() function returns the number of elements of a collection or to-many association.
selectname,size(books)fromAuthorTheelement() function returns a reference to an element of a joined set or list.For an identification variable (case 1 above), this function is optional.For a compound path (case 2), it’s required.
Theindex() function returns a reference to the index of a joined list.
In this example,element() is optional, butindex() is required:
selectid(book),index(ed),element(ed)fromBookbookasbookjoinbook.editionsasedThekey() function returns a reference to a key of a joined map.Thevalue() function returns a reference to its value.
selectkey(entry),value(entry)fromThingasthingjointhing.entriesasentryThe functionselements(),indices(),keys(), andvalues() are used to quantify over collections.We may use them with:
| Shortcut | Equivalent subquery |
|---|---|
|
|
|
|
|
|
|
|
For example:
selecttitlefromBookwhere'hibernate'inelements(tags)Don’t confuse theelements() function withelement(), theindices() function withindex(), thekeys() function withkey(), or thevalues() function withvalue().The functions named in singular deal with elements of "flattened" collections.If not already joined, they add an implicit join to the query.The functions with plural naming donot flatten a collection by joining it.
The following queries are different: The first query produces a single row, with |
Finally, the following functions evaluate the id, version, or natural id of an entity, or the foreign key of a to-one association:
| Function | Purpose | JPA standard |
|---|---|---|
| The value of the entity | ✖ |
| The value of the entity | ✖ |
| The value of the entity | ✖ |
| The value of the foreign key column mapped by a | ✖ |
The following special functions let us embed a call to a native SQL function, refer directly to a column, or evaluate an expression written in native SQL.
| Function | Purpose | Signature | JPA standard |
|---|---|---|---|
| Call a SQL function |
| ✔ |
| Call a SQL function |
| ✖ |
| A column value |
| ✖ |
| Evaluate a SQL expression |
| ✖ |
| Before using one of these functions, ask yourself if it might be better to just write the whole query in native SQL. |
Thecolumn() function lets us refer to an unmapped column of a table.The column name must be qualified by an identification variable or path expression.
selectcolumn(log.ctidasString)fromLoglogOf course, the table itself must be mapped by an entity class.
The functions we’ve described above are the functions abstracted by HQL and made portable across databases.But, of course, HQL can’t abstract every function in your database.
There are several ways to call native or user-defined SQL functions.
A native or user-defined function may be called using JPQL’sfunction syntax, for example,function('sinh', phi), or HQL’s extension to that syntax, for examplefunction(sinh as Double, phi).(This is the easiest way, but not the best way.)
A user-writtenFunctionContributor may register user-defined functions.
A customDialect may register additional native functions by overridinginitializeFunctionRegistry().
Registering a function isn’t hard, but is beyond the scope of this guide. (It’s even possible to use the APIs Hibernate provides to make your ownportable functions!) |
Fortunately, every built-inDialect already registers many native functions for the database it supports.
Try setting the log category |
The special functionsql() allows the use of native SQL fragments inside an HQL query.
The signature of this function issql(pattern[, argN]*), wherepattern must be a string literal but the remaining arguments may be of any type.The pattern literal is unquoted and embedded in the generated SQL.Occurrences of? in the pattern are replaced with the remaining arguments of the function.
We may use this, for example, to perform a native PostgreSQL typecast:
fromComputercwherec.ipAddress=sql('?::inet','127.0.0.1')This results in SQL logically equivalent to:
select*fromComputercwherec.ipAddress='127.0.0.1'::inetOr we can use a native SQL operator:
fromHumanhorderbysql('(? <-> ?)',h.workLocation,h.homeLocation)And this time the SQL is logically equivalent to:
select*fromHumanhwhere(h.workLocation<->h.homeLocation)A predicate is an operator which, when applied to some argument, evaluates totrue orfalse.In the world of SQL-style ternary logic, we must expand this definition to encompass the possibility that the predicate evaluates tonull.Typically, a predicate evaluates tonull when one of its arguments isnull.
Predicates occur in thewhere clause, thehaving clause and in searched case expressions.
The binary comparison operators are borrowed from SQL:=,>,>=,<,<=,<>.
If you prefer, HQL treats!= as a synonym for<>. |
The operands should be of the same type.
fromBookwhereprice<1.0fromAuthorasauthorwhereauthor.nomDePlume<>author.person.nameselectid,totalfrom(selectord.idasid,sum(item.book.price*item.quantity)astotalfromOrderasordjoinItemasitemgroupbyord)wheretotal>100.0between predicateThe ternarybetween operator, and its negation,not between, determine if a value falls within a range.
Of course, all three operands must be of compatible type.
fromBookwherepricebetween1.0and100.0The following operators make it easier to deal with null values.These predicates never evaluate tonull.
| Operator | Negation | Type | Semantics |
|---|---|---|---|
|
| Unary postfix |
|
|
| Binary |
|
fromAuthorwherenomDePlumeisnotnullThese operators perform comparisons on values of typeboolean.These predicates never evaluate tonull.
The values |
Forlogical operations onpredicates, seeLogical operators below.
| Operator | Negation | Type | Semantics |
|---|---|---|---|
|
| Unary postfix |
|
|
| Binary |
|
fromBookwherediscontinuedisnottrueThe following operators apply to collection-valued attributes and to-many associations.
| Operator | Negation | Type | Semantics |
|---|---|---|---|
|
| Unary postfix |
|
|
| Binary |
|
fromAuthorwherebooksisemptyselectauthor,bookfromAuthorasauthor,Bookasbookwhereauthormemberofbook.authorsThelike operator performs pattern matching on strings.Its friendilike performs case-insensitive matching.
Their syntax is defined by:
expression "NOT"? ("LIKE" | "ILIKE") expression ("ESCAPE" character)?The expression on the right is a pattern, where:
_ matches any single character,
% matches any number of characters, and
if an escape character is specified, it may be used to escape either of these wildcards.
fromBookwheretitlenotlike'% for Dummies'The optionalescape character allows a pattern to include a literal_ or% character.
As you can guess,not like andnot ilike are the enemies oflike andilike, and evaluate to the exact opposite boolean values.
in predicateThein predicates evaluates to true if the value to its left is in … well, whatever it finds to its right.
Its syntax is unexpectedly complicated:
expression "NOT"? "IN" inListinList: collectionQuantifier "(" simplePath ")"| "(" (expression ("," expression)*)? ")"| "(" subquery ")"| parameterThis less-than-lovely fragment of the HQL ANTLR grammar tells us that the thing to the right might be:
a list of values enclosed in parentheses,
a subquery,
one of the collection-handling functions definedabove, or
a query parameter,
The type of the expression on the left, and the types of all the values on the right must be compatible.
JPQL limits the legal types to string, numeric, date/time, and enum types, and in JPQL the left expression must be either:
HQL is far more permissive. HQL itself does not restrict the type in any way, though the database itself might.Even embedded attributes are allowed, although that feature depends on the level of support for tuple or "row value" constructors in the underlying database. |
fromPaymentaspaymentwheretype(payment)in(CreditCardPayment,WireTransferPayment)fromAuthorasauthorwhereauthor.person.namein(selectnamefromOldAuthorData)fromBookasbookwhere:editioninelements(book.editions)It’s quite common to have a parameterized list of values.
Here’s a very useful idiom: |
We may even "vectorize" anin predicate, using a tuple constructor and a subquery with multiple selection items:
fromAuthorasauthorwhere(author.person.name,author.person.birthdate)in(selectname,birthdatefromOldAuthorData)The binary comparisons we metabove may involve a quantifier, either:
a quantified subquery, or
a quantifier applied to one of the functions definedabove.
The quantifiers are unary prefix operators:all,every,any, andsome.
| Subquery operator | Synonym | Semantics |
|---|---|---|
|
| Evaluates to true of the comparison is true forevery value in the result set of the subquery |
|
| Evaluates to true of the comparison is true forat least one value in the result set of the subquery |
fromPublisherpubwhere100.0<all(selectpricefrompub.books)fromPublisherpubwhere:title=some(selecttitlefrompub.books)exists predicateThe unary prefixexists operator evaluates to true if the thing to its right is nonempty.
The thing to its right might be:
a subquery, or
one of the functions definedabove.
As you can surely guess,not exists evaluates to true if the thing to the rightis empty.
fromAuthorwhereexistselements(books)fromAuthorasauthorwhereexists(fromOrderjoinitemswherebookinelements(author.books))The logical operators are binary infixand andor, and unary prefixnot.
Just like SQL, logical expressions are based on ternary logic.A logical operator evaluates to null if it has a null operand.
Thefrom clause, and its subordinatejoin clauses sit right at the heart of most queries.
Thefrom clause is responsible for declaring the entities available in the rest of the query, and assigning them aliases, or, in the language of the JPQL specification,identification variables.
An identification variable is just a name we can use to refer to an entity and its attributes from expressions in the query.It may be any legal Java identifier.According to the JPQL specification, identification variables must be treated as case-insensitive language elements.
The identification variable is actually optional, but for queries involving more than one entity it’s almost always a good idea to declare one. Thisworks, but it isn’t particularly good form: |
Identification variables may be declared with theas keyword, but this is optional.
A root entity reference, or what the JPQL specification calls arange variable declaration, is a direct reference to a mapped@Entity type by its entity name.
Remember, theentity name is the value of the |
selectbookfromBookasbookIn this example,Book is the entity name, andbook is the identification variable.Theas keyword is optional.
Alternatively, a fully-qualified Java class name may be specified.Then Hibernate will query every entity which inherits the named type.
selectdocfromorg.hibernate.example.AbstractDocumentasdocwheredoc.textlike:patternOf course, there may be multiple root entities.
selecta,bfromAuthora,Authorb,Bookbookwhereainelements(book.authors)andbinelements(book.authors)This query may even be written using the syntaxcross join in place of the commas:
selecta,bfromBookbookcrossjoinAuthoracrossjoinAuthorbwhereainelements(book.authors)andbinelements(book.authors)Of course, it’s possible to write old-fashioned pre-ANSI-era joins:
selectbook.title,publisher.namefromBookbook,Publisherpublisherwherebook.publisher=publisherandbook.titlelike:titlePatternBut we never write HQL this way.
HQL and JPQL queries are inherently polymorphic.Consider:
selectpaymentfromPaymentaspaymentThis query names thePayment entity explicitly.But theCreditCardPayment andWireTransferPayment entities inheritPayment, and sopayment ranges over all three types.Instances of all these entities are returned by the query.
The query It returns every object of every mapped entity type. |
Aderived root is an uncorrelated subquery which occurs in thefrom clause.
selectid,totalfrom(selectord.idasid,sum(item.book.price*item.quantity)astotalfromOrderasordjoinItemasitemgroupbyord)wheretotal>100.0The derived root may declare an identification variable.
selectstuff.id,stuff.totalfrom(selectord.idasid,sum(item.book.price*item.quantity)astotalfromOrderasordjoinItemasitemgroupbyord)asstuffwheretotal>100.0This feature can be used to break a more complicated query into smaller pieces.
We emphasize that a derived root must be anuncorrelated subquery.It may not refer to other roots declared in the same |
A subquery may also occur in ajoin, in which case it may be a correlated subquery.
from clauseAcommon table expression (CTE) is like a derived root with a name.We’ll discuss CTEslater.
Joins allow us to navigate from one entity to another, via its associations, or via explicit join conditions.There are:
explicit joins, declared within thefrom clause using the keywordjoin, and
implicit joins, which don’t need to be declared in thefrom clause.
An explicit join may be either:
aninner join, written asjoin orinner join,
aleft outer join, written asleft join orleft outer join,
aright outer join, written asright join orright outer join, or
afull outer join, written asfull join orfull outer join.
An explicit root join works just like an ANSI-style join in SQL.
selectbook.title,publisher.namefromBookbookjoinPublisherpublisheronbook.publisher=publisherwherebook.titlelike:titlePatternThe join condition is written out explicitly in theon clause.
This looks nice and familiar, but it’snot the most common sort of join in HQL or JPQL. |
Every explicit association join specifies an entity attribute to be joined.The specified attribute:
is usually a@OneToMany,@ManyToMany,@OneToOne, or@ManyToOne association, but
it could be an@ElementCollection, and
it might even be an attribute of embeddable type.
In the case of an association or collection, the generated SQL will have a join of the same type.(For a many-to-many association it will havetwo joins.)In the case of an embedded attribute, the join is purely logical and does not result in a join in the generated SQL.
An explicit join may assign an identification variable to the joined entity.
fromBookasbookjoinbook.publisheraspublisherjoinbook.authorsasauthorwherebook.titlelike:titlePatternselectbook.title,author.name,publisher.nameFor an outer join, we must write our query to accommodate the possibility that the joined association is missing.
fromBookasbookleftjoinbook.publisheraspublisherjoinbook.authorsasauthorwherebook.titlelike:titlePatternselectbook.title,author.name,ifnull(publisher.name,'-')For further information about collection-valued association references, seeJoining collections and many-valued associations.
Thewith oron clause allows explicit qualification of the join conditions.
The specified join conditions areadded to the join conditions specified by the foreign key association.That’s why, historically, HQL uses the keword The |
Join conditions occurring in thewith oron clause are added to theon clause in the generated SQL.
fromBookasbookleftjoinbook.publisheraspublisherwithpublisher.closureDateisnotnullleftjoinbook.authorsasauthorwithauthor.type<>COLLABORATIONwherebook.titlelike:titlePatternselectbook.title,author.name,publisher.nameAfetch join overrides the laziness of a given association, specifying that the association should be fetched with a SQL join.The join may be an inner or outer join.
Ajoin fetch, or, more explicitly,inner join fetch, only returns base entities with an associated entity.
Aleft join fetch, or—for lovers of verbosity—left outer join fetch, returns all the base entities, including those which have no associated joined entity.
This is one of the most important features of Hibernate.To achieve acceptable performance with HQL, you’ll need to use |
For example, ifPerson has a one-to-many association namedphones, the use ofjoin fetch in the following query specifies that the collection elements should be fetched in the same SQL query:
selectbookfromBookasbookleftjoinfetchbook.publisherjoinfetchbook.authorsIn this example, we used a left outer join forbook.publisher because we also wanted to obtain books with no publisher, but a regular inner join forbook.authors because every book has at least one author.
A query may have more than one fetch join, but be aware that:
it’s perfectly safe to fetch several to-one associations in series or parallel in a single query, and
a single series ofnested fetch joins is also fine, but
fetching multiple collections or to-many associations inparallel results in a Cartesian product at the database level, and might exhibit very poor performance.
HQL doesn’t disallow it, but it’s usually a bad idea to apply a restriction to ajoin fetched entity, since the elements of the fetched collection would be incomplete.Indeed, it’s best to avoid even assigning an identification variable to a fetched joined entity except for the purpose of specifying a nested fetch join.
Fetch joins should usually be avoided in limited or paged queries.This includes:
Nor should they be used with the |
Fetch joins are disallowed in subqueries, where they would make no sense.
An explicit join may narrow the type of the joined entity usingtreat().
fromOrderasordjointreat(ord.paymentsasCreditCardPayment)ascreditCardPaymentwherelength(creditCardPayment.cardNumber)between16and20selectord.id,creditCardPayment.cardNumber,creditCardPayment.amountHere, the identification variableccp declared to the right oftreat() has the narrowed typeCreditCardPayment, instead of the declared typePayment.This allows the attributecardNumber declared by the subtypeCreditCardPayment to be referenced in the rest of the query.
SeeTypes and typecasts for more information abouttreat().
Ajoin clause may contain a subquery, either:
an uncorrelated subquery, which is almost the same as aderived root, except that it may have anon restriction, or
alateral join, which is a correlated subquery, and may refer to other roots declared earlier in the samefrom clause.
Thelateral keyword just distinguishes the two cases.
fromPhoneasphoneleftjoin(selectcall.durationasduration,call.phone.idascidfromCallascallorderbycall.durationdesclimit1)aslongestoncid=phone.idwherephone.number=:phoneNumberselectlongest.durationThis query may also be expressed using alateral join:
fromPhoneasphoneleftjoinlateral(selectcall.durationasdurationfromphone.callsascallorderbycall.durationdesclimit1)aslongestwherephone.number=:phoneNumberselectlongest.durationA lateral join may be an inner or left outer join, but not a right join, nor a full join.
Traditional SQL doesn’t allow correlated subqueries in the On some databases, It’s almost as if they’redeliberately trying to confuse us. |
Lateral joins are particularly useful for computing top-N elements of multiple groups.
Most databases support some flavor of |
It’s not necessary to explicitlyjoin every entity that occurs in a query.Instead, entity associations may benavigated, just like in Java:
if an attribute is of embedded type, or is a to-one association, it may be further navigated, but
if an attribute is of basic type, it is considered terminal, and may not be further navigated, and
if an attribute is collection-valued, or is a to-many association, it may be navigated, but only with the help ofvalue(),element(), orkey().
It’s clear that:
A path expression likeauthor.name with only two elements just refers to state held directly by an entity with an aliasauthor defined infrom orjoin.
But a longer path expression, for example,author.person.name, might refer to state held by an associated entity.(Alternatively, it might refer to state held by an embedded class.)
In the second case, Hibernate with automatically add a join to the generated SQL if necessary.
fromBookasbookwherebook.publisher.namelike:pubNameAs in this example, implicit joins usually appear outside thefrom clause of the HQL query.However, they always affect thefrom clause of the SQL query.
The example above is equivalent to:
selectbookfromBookasbookjoinbook.publisheraspubwherepub.namelike:pubNameNote that:
Implicit joins are always treated as inner joins.
Multiple occurrences of the same implicit join always refer to the same SQL join.
This query:
selectbookfromBookasbookwherebook.publisher.namelike:pubNameandbook.publisher.closureDateisnullresults in just one SQL join, and is just a different way to write:
selectbookfromBookasbookjoinbook.publisheraspubwherepub.namelike:pubNameandpub.closureDateisnullWhen a join involves a collection or many-valued association, the declared identification variable refers to theelements of the collection, that is:
to the elements of aSet,
to the elements of aList, not to their indices in the list, or
to the values of aMap, not to their keys.
selectpublisher.name,author.namefromPublisheraspublisherjoinpublisher.booksasbookjoinbook.authorsauthorwhereauthor.namelike:namePatternIn this example, the identification variableauthor is of typeAuthor, the element type of the listBook.authors.But if we need to refer to the index of anAuthor in the list, we need some extra syntax.
You might recall that we mentionedList indexes andMap keys and values a bit earlier.These functions may be applied to the identification variable declared in a collection join or many-valued association join.
| Function | Applies to | Interpretation | Notes |
|---|---|---|---|
| Any collection | The collection element or map entry value | Often optional. |
| Any | The index of the element in the list | For backward compatibility, it’s also an alternative to |
| Any | The key of the entry in the list | If the key is of entity type, it may be further navigated. |
| Any | The map entry, that is, the | Only legal as a terminal path, and only allowed in the |
In particular,index() andkey() obtain a reference to a list index or map key.
selectbook.title,author.name,index(author)fromBookasbookjoinbook.authorsasauthorselectpublisher.name,leadAuthor.namefromPublisheraspublisherjoinpublisher.booksasbookjoinbook.authorsleadAuthorwhereleadAuthor.namelike:namePatternandindex(leadAuthor)==0A path expression likebook.authors.name is not considered legal.We can’t just navigate a many-valued association with this syntax.
Instead, the functionselement(),index(),key(), andvalue() may be applied to a path expression to express an implicit join.So we must writeelement(book.authors).name orindex(book.authors).
selectbook.title,element(book.authors).name,index(book.authors)fromBookbookAn element of an indexed collection (an array, list, or map) may even be identified using the index operator:
selectpublisher.name,book.authors[0].namefromPublisheraspublisherjoinpublisher.booksasbookwherebook.authors[0].namelike:namePatternJoining is one kind ofrelational operation.It’s an operation that produces relations (tables) from other relations.Such operations, taken together, form therelational algebra.
We must now understand the rest of this family: restriction a.k.a. selection, projection, aggregation, union/intersection, and, finally, ordering and limiting, operations which are not strictly part of the calculus of relations, but which usually come along for the ride because they’re veryuseful.
We’ll start with the operation that’s easiest to understand.
Thewhere clause restricts the results returned by aselect query or limits the scope of anupdate ordelete query.
This operation is usually calledselection, but since that term is often confused with theselect keyword, and since both projection and selection involve "selecting" things, here we’ll use the less-ambiguous termrestriction. |
A restriction is nothing more than a single logical expression, a topic we exhausted above inPredicates.Therefore, we’ll move quickly onto the next, and more interesting, operation.
An aggregate query is one withaggregate functions in its projection list.It collapses multiple rows into a single row.Aggregate queries are used for summarizing and analysing data.
An aggregate query might have agroup by clause.Thegroup by clause divides the result set into groups, so that a query with aggregate functions in the select list returns not a single result for the whole query, but one result for each group.If an aggregate querydoesn’t have agroup by clause, it always produces a single row of results.
| In short,grouping controls the effect ofaggregation. |
A query with aggregation may also have ahaving clause, a restriction applied to the groups.
Thegroup by clause looks quite similar to theselect clause—it has a list of grouped items, but:
if there’s just one item, then the query will have a single result for each unique value of that item, or
if there are multiple items, the query will have a result for each uniquecombination or their values.
The BNF for a grouped item is just:
identifier | INTEGER_LITERAL | expressionConsider the following queries:
selectbook.isbn,sum(quantity)astotalSold,sum(quantity*book.price)astotalBilledfromItemwherebook.isbn=:isbnselectbook.isbn,year(order.dateTime)asyear,sum(quantity)asyearlyTotalSold,sum(quantity*book.price)asyearlyTotalBilledfromItemwherebook.isbn=:isbngroupbyyear(order.dateTime)The first query calculates complete totals over all orders in years.The second calculates totals for each year, after grouping the orders by year.
The special functionsrollup() andcube() may be used in thegroup by clause, when supported by the database.The semantics are identical to SQL.
These functions are especially useful for reporting.
Agroup by clause withrollup() is used to produce subtotals and grand totals.
Agroup by clause withcube() allows totals for every combination of columns.
In a grouped query, thewhere clause applies to the non-aggregated values (it determines which rows will make it into the aggregation).Thehaving clause also restricts results, but it operates on the aggregated values.
In anexample above, we calculated totals for every year for which data was available.But our dataset might extend far back into the past, perhaps even as far back as those terrible dark ages before Hibernate 2.0.So let’s restrict our result set to data from our own more civilized times:
selectbook.isbn,year(order.dateTime)asyear,sum(quantity)asyearlyTotalSold,sum(quantity*book.price)asyearlyTotalBilledfromItemwherebook.isbn=:isbngroupbyyear(order.dateTime)havingyear(order.dateTime)>2003andsum(quantity)>0Thehaving clause follows the same rules as thewhere clause and is also just a logical predicate.Thehaving restriction is applied after grouping and aggregation has already been performed, whereas thewhere clause is applied before the data is grouped or aggregated.
Theselect list identifies which objects and values to return as the query results.This operation is calledprojection.
selectClause : "SELECT" "DISTINCT"? selection (","" selection)*Any of the expression types discussed inExpressions may occur in the projection list, unless otherwise noted.
If a query has no explicit |
Thedistinct keyword helps remove duplicate results from the query result list.It’s only effect is to adddistinct to the generated SQL.
selectdistinctlastNamefromPersonselectdistinctauthorfromPublisheraspubjoinpub.booksasbookjoinbook.authorsasauthorwherepub.id=:pidAs of Hibernate 6, duplicate results arising from the use of |
It’s common to have aggregate functions likecount(),sum(), andmax() in a select list.Aggregate functions are special functions that reduce the size of the result set.
The standard aggregate functions defined in both ANSI SQL and JPQL are these ones:
| Aggregate function | Argument type | Result type | JPA standard / ANSI SQL standard |
|---|---|---|---|
| Any |
| ✔/✔ |
| Any numeric type |
| ✔/✔ |
| Any numeric type, or string | Same as the argument type | ✔/✔ |
| Any numeric type, or string | Same as the argument type | ✔/✔ |
| Any numeric type | See table below | ✔/✔ |
| Any numeric type |
| ✖/✔ |
| Any numeric type |
| ✖/✔ |
selectcount(distinctitem.book)fromItemasitemwhereyear(item.order.dateTime)=:yearselectsum(item.quantity)astotalSalesfromItemasitemwhereitem.book.isbn=:isbnselectyear(item.order.dateTime)asyear,sum(item.quantity)asyearlyTotalfromItemasitemwhereitem.book.isbn=:isbngroupbyyear(item.order.dateTime)selectmonth(item.order.dateTime)asmonth,avg(item.quantity)asmonthlyAveragefromItemasitemwhereitem.book.isbn=:isbngroupbymonth(item.order.dateTime)In the case ofsum(), the rules for assigning a result type are:
| Argument type | Result type |
|---|---|
Any integral numeric type except |
|
Any floating point numeric type |
|
|
|
|
|
HQL defines two additional aggregate functions which accept a logical predicate as an argument.
| Aggregate function | Argument type | Result type | JPA standard |
|---|---|---|---|
| Logical predicate |
| ✖ |
| Logical predicate |
| ✖ |
We may write, for example,every(p.amount < 1000.0).
Below, we’ll meet theordered set aggregate functions.
Aggregate functions usually appear in theselect clause, but control over aggregation is the responsibility of thegroup by clause, as describedbelow. |
Theelements() andindices() functions we metearlier let us apply aggregate functions to a collection:
| New syntax | Legacy HQL function 💀 | Applies to | Purpose |
|---|---|---|---|
|
| Any collection with sortable elements | The maximum element or map value |
|
| Any collection with sortable elements | The minimum element or map value |
| — | Any collection with numeric elements | The sum of the elements or map values |
| — | Any collection with numeric elements | The average of the elements or map values |
|
| Indexed collections (lists and maps) | The maximum list index or map key |
|
| Indexed collections (lists and maps) | The minimum list index or map key |
| — | Indexed collections (lists and maps) | The sum of the list indexes or map keys |
| — | Indexed collections (lists and maps) | The average of the list indexes or map keys |
These operations are mostly useful when working with@ElementCollections.
selecttitle,max(indices(authors))+1,max(elements(editions))fromBookAll aggregate functions support the inclusion of afilter clause, a sort of mini-where applying a restriction to just one item of the select list:
selectyear(item.order.dateTime)asyear,sum(item.quantity)filter(wherenotitem.order.fulfilled)asunfulfilled,sum(item.quantity)filter(whereitem.order.fulfilled)asfulfilled,sum(item.quantity*item.book.price)filter(whereitem.order.paid)fromItemasitemwhereitem.book.isbn=:isbngroupbyyear(item.order.dateTime)The BNF for thefilter clause is simple:
filterClause: "FILTER" "(" "WHERE" predicate ")"Anordered set aggregate function is a special aggregate function which has:
not only an optional filter clause, as above, but also
awithin group clause containing a mini-order by specification.
The BNF forwithin group is straightforward:
withinGroupClause: "WITHIN" "GROUP" "(" "ORDER" "BY" sortSpecification ("," sortSpecification)* ")"There are two main types of ordered set aggregate function:
aninverse distribution function calculates a value that characterizes the distribution of values within the group, for example,percentile_cont(0.5) is the median, andpercentile_cont(0.25) is the lower quartile.
ahypothetical set function determines the position of a "hypothetical" value within the ordered set of values.
The following ordered set aggregate functions are available on many platforms:
| Type | Functions |
|---|---|
Inverse distribution functions |
|
Hypothetical set functions |
|
Other |
|
This query calculates the median price of a book:
selectpercentile_cont(0.5)withingroup(orderbyprice)fromBookThis query finds the percentage of books with prices less than 10 dollars:
select100*percent_rank(10.0)withingroup(orderbyprice)fromBookActually, the most widely-supported ordered set aggregate function is one which builds a string by concatenating the values within a group.This function has different names on different databases, but HQL abstracts these differences, and—following ANSI SQL—calls itlistagg().
selectlistagg(title,', ')withingroup(orderbyisbn)fromBookgroupbyelement(authors)This very useful function produces a string by concatenation of the aggregated values of its argument.
Awindow function is one which also has anover clause, for example:
selectitem.order.dateTime,sum(item.quantity)over(orderbyitem.order.dateTime)asrunningTotalfromItemitemThis query returns a running total of sales over time.That is, thesum() is taken over a window comprising the current row of the result set, together with all previous rows.
A window function application may optionally specify any of the following clauses:
| Optional clause | Keyword | Purpose |
|---|---|---|
Partitioning of the result set |
| Very similar to |
Ordering of the partition |
| Specifies the order of rows within a partition |
Windowing |
| Defines the bounds of a window frame within a partition |
Restriction |
| As aggregate functions, window functions may optionally specify a filter |
For example, we may partition the running total by book:
selectitem.book.isbn,item.order.dateTime,sum(item.quantity)over(partitionbyitem.bookorderbyitem.order.dateTime)asrunningTotalfromItemitemEvery partition runs in isolation, that is, rows can’t leak across partitions.
The full syntax for window function application is amazingly involved, as shown by this BNF:
overClause: "OVER" "(" partitionClause? orderByClause? frameClause? ")"partitionClause: "PARTITION" "BY" expression ("," expression)*frameClause: ("RANGE"|"ROWS"|"GROUPS") frameStart frameExclusion?| ("RANGE"|"ROWS"|"GROUPS") "BETWEEN" frameStart "AND" frameEnd frameExclusion?frameStart: "CURRENT" "ROW"| "UNBOUNDED" "PRECEDING"| expression "PRECEDING"| expression "FOLLOWING"frameEnd: "CURRENT" "ROW"| "UNBOUNDED" "FOLLOWING"| expression "PRECEDING"| expression "FOLLOWING"frameExclusion: "EXCLUDE" "CURRENT" "ROW"| "EXCLUDE" "GROUP"| "EXCLUDE" "TIES"| "EXCLUDE" "NO" "OTHERS"Window functions are similar to aggregate functions in the sense that they compute some value based on a "frame" comprising multiple rows.But unlike aggregate functions, window functions don’t flatten rows within a window frame.
Thewindow frame is the set of rows within a given partition that is passed to the window function.There’s a different window frame for each row of the result set.In our example, the window frame comprised all the preceding rows within the partition, that is, all the rows with the sameitem.book and with an earlieritem.order.dateTime.
The boundary of the window frame is controlled via the windowing clause, which may specify one of the following modes:
| Mode | Definition | Example | Interpretation |
|---|---|---|---|
| Frame bounds defined by a given number of rows |
| The previous 5 rows in the partition |
| Frame bounds defined by a given number ofpeer groups, rows belonging to the same peer group if they are assigned the same position by |
| The rows in the previous 5 peer groups in the partition |
| Frame bounds defined by a maximum difference invalue of the expression used to |
| The rows whose |
The frame exclusion clause allows excluding rows around the current row:
| Option | Interpretation |
|---|---|
| Excludes the current row |
| Excludes rows of the peer group of the current row |
| Excludes rows of the peer group of the current row except the current row |
| The default, does not exclude anything |
By default, the window frame is defined asrows between unbounded preceding and current row exclude no others, meaning every row up to and including the current row.
The modes |
The following window functions are available on all major platforms:
| Window function | Purpose | Signature |
|---|---|---|
| The position of the current row within its frame |
|
| The value of a subsequent row in the frame |
|
| The value of a previous row in the frame |
|
| The value of a first row in the frame |
|
| The value of a last row in the frame |
|
| The value of the `n`th row in the frame |
|
In principle every aggregate or ordered set aggregate function might also be used as a window function, just by specifyingover, but not every function is supported on every database.
Window functions and ordered set aggregate functions aren’t available on every database.Even where they are available, support for particular features varies widely between databases.Therefore, we won’t waste time going into further detail here.For more information about the syntax and semantics of these functions, consult the documentation for your dialect of SQL. |
These operators apply not to expressions, but to entire result sets:
union andunion all,
intersect andintersect all, and
except andexcept all.
Just like in SQL,all suppresses the elimination of duplicate results.
selectnomDePlumefromAuthorwherenomDePlumeisnotnullunionselectnamefromPersonBy default, the results of the query are returned in an arbitrary order.
Imposing an order on a set is calledsorting. A relation (a database table) is a set, and therefore certain particularly dogmatic purists have argued that sorting has no place in the algebra of relations.We think this is more than a bit silly: practical data analysis almost always involves sorting, which is a perfectly well-defined operation. |
Theorder by clause specifies a list of projected items used to sort the results.Each sorted item may be:
an attribute of an entity or embeddable class,
a more complexexpression,
the alias of a projected item declared in the select list, or
a literal integer indicating the ordinal position of a projected item in the select list.
Of course, in principle, only certain types may be sorted: numeric types, string, and date and time types.But HQL is very permissive here and will allow an expression of almost any type to occur in a sort list.Even the identification variable of an entity with a sortable identifier type may occur as a sorted item.
The JPQL specification requires that every sorted item in the Therefore, you might wish to avoid the use of complex expressions in the sort list. |
The BNF for a sorted item is:
sortExpression sortDirection? nullsPrecedence?sortExpression : identifier | INTEGER_LITERAL | expressionsortDirection : "ASC" | "DESC"nullsPrecedence : "NULLS" ("FIRST" | "LAST")Each sorted item listed in theorder by clause may explicitly specify a direction, either:
asc for ascending order, or
desc for descending order.
If no direction is explicitly specified, the results are returned in ascending order.
Of course, there’s an ambiguity with respect to null values.Therefore, the sorting of null values may be explicitly specified:
| Precedence | Interpretation |
|---|---|
| Puts null values at the beginning of the result set |
| Puts them at the end |
selecttitle,publisher.namefromBookorderbytitle,publisher.namenullslastselectbook.isbn,year(order.dateTime)asyear,sum(quantity)asyearlyTotalSold,sum(quantity*book.price)asyearlyTotalBilledfromItemwherebook.isbn=:isbngroupbyyear(order.dateTime)havingyear(order.dateTime)>2000andsum(quantity)>0orderbyyearlyTotalSolddesc,yeardescQueries with an ordered result list may have limits or pagination.
It’s often useful to place a hard upper limit on the number of results that may be returned by a query.Thelimit andoffset clauses are an alternative to the use ofsetMaxResults() andsetFirstResult() respectively,and may similarly be used for pagination.
If the |
The SQLfetch syntax is supported as an alternative:
| Short form | Verbose form | Purpose |
|---|---|---|
|
| Limit result set |
|
| Paginate result set |
The BNF gets a bit complicated:
limitClause : "LIMIT" parameterOrIntegerLiteraloffsetClause : "OFFSET" parameterOrIntegerLiteral ("ROW" | "ROWS")?fetchClause : "FETCH" ("FIRST" | "NEXT") (parameterOrIntegerLiteral | parameterOrNumberLiteral "%") ("ROW" | "ROWS") ("ONLY" | "WITH" "TIES")These two queries are identical:
selecttitlefromBookorderbytitle,publisheddesclimit50selecttitlefromBookorderbytitle,publisheddescfetchfirst50rowsonlyThese are well-defined limits: the number of results returned by the database will be limited to 50, as promised.But not every query is quite so well-behaved.
Limiting certainlyisn’t a well-defined relational operation, and must be used with care. In particular, limits don’t play well withfetch joins. |
This next query is accepted by HQL, and no more than 50 results are returned bygetResultList(), just as expected:
selecttitlefromBookjoinfetchauthorsorderbytitle,publisheddesclimit50However, if you log the SQL executed by Hibernate, you’ll notice something wrong:
selectb1_0.isbn,a1_0.books_isbn,a1_0.authors_ORDER,a1_1.id,a1_1.bio,a1_1.name,a1_1.person_id,b1_0.price,b1_0.published,b1_0.publisher_id,b1_0.titlefromBookb1_0join(Book_Authora1_0joinAuthora1_1ona1_1.id=a1_0.authors_id)onb1_0.isbn=a1_0.books_isbnorderbyb1_0.title,b1_0.publisheddescWhat happened to thelimit clause?
When limits or pagination are combined with a fetch join, Hibernate must retrieve all matching results from the database andapply the limit in memory! Thisalmost certainly isn’t the behavior you were hoping for, and in general will exhibitterrible performance characteristics. |
Acommon table expression or CTE may be thought of as a sort of named subquery.Any query with an uncorrelated subquery can in principle be rewritten so that the subquery occurs in thewith clause.
But CTEs have capabilities that subqueries don’t have.Thewith clause lets us:
specify materialization hints, and
write recursive queries.
On databases which don’t support CTEs natively, Hibernate attempts to rewrite any HQL query with CTEs as a SQL query with subqueries.This is impossible for recursive queries, unfortunately.
Let’s take a quick look at the BNF:
withClause: "WITH" cte ("," cte)*cte: identifier AS ("NOT"? "MATERIALIZED")? "(" queryExpression ")" searchClause? cycleClause?Thewith clause comes right at the start of a query.It may declare multiple CTEs with different names.
withpaidas(selectord.idasoid,sum(payment.amount)asamountPaidfromOrderasordleftjoinord.paymentsaspaymentgroupbyordhavinglocaldatetime-ord.dateTime<365day),owedas(selectord.idasoid,sum(item.quantity*item.book.price)asamountOwedfromOrderasordleftjoinord.itemsasitemgroupbyordhavinglocaldatetime-ord.dateTime<365day)selectid,paid.amountPaid,owed.amountOwedfromOrderwherepaid.amountPaid<owed.amountOwedandpaid.oid=idandowed.oid=idNotice that if we rewrote this query using subqueries, it would look quite a lot clumsier.
Thematerialized keyword is a hint to the database that the subquery should be separately executed and its results stored in a temporary table.
On the other hand, its nemesis,not materialized, is a hint that the subquery should be inlined at each use site, with each usage optimized independently.
The precise impact of materialization hints is quite platform-dependant. |
Our example query from above hardly changes.We just addmaterialized to the CTE declarations.
withpaidasmaterialized(selectord.idasoid,sum(payment.amount)asamountPaidfromOrderasordleftjoinord.paymentsaspaymentgroupbyordhavinglocaldatetime-ord.dateTime<365day),owedasmaterialized(selectord.idasoid,sum(item.quantity*item.book.price)asamountOwedfromOrderasordleftjoinord.itemsasitemgroupbyordhavinglocaldatetime-ord.dateTime<365day)selectid,paid.amountPaid,owed.amountOwedfromOrderwherepaid.amountPaid<owed.amountOwedandpaid.oid=idandowed.oid=idArecursive query is one where the CTE is defined self-referentially.Recursive queries follow a very particular pattern.The CTE is defined as a union of:
a base subquery returning an initial set of rows where the recursion begins,
a recursively-executed subquery which returns additional rows by joining against the CTE itself.
Let’s demonstrate this with an example.
First we’ll need some sort of tree-like entity:
@EntityclassNode{@IdLongid;Stringtext;@ManyToOneNodeparent;}We may obtain a tree ofNodes with the following recursive query:
withTreeas(/* base query */selectroot.idasid,root.textastext,0aslevelfromNoderootwhereroot.parentisnullunionall/* recursion */selectchild.idasid,child.textastext,level+1aslevelfromTreeparentjoinNodechildonchild.parent.id=parent.id)selecttext,levelfromTreeWhen querying a tree-like of data structure, the base subquery usually returns the root node or nodes.The recursively-executed subquery returns the children of the current set of nodes.It’s executed repeatedly with the results of the previous execution.Recursion terminates when the recursively-executed subquery returns no new nodes.
Hibernate cannot emulate recursive queries on databases which don’t support them natively. |
Now, if a graph contains cycles, that is, if it isn’t a tree, the recursion might never terminate.
Thecycle clause enables cycle detection, and aborts the recursion if a node is encountered twice.
withTreeas(/* base query */selectroot.idasid,root.textastext,0aslevelfromNoderootwhereroot.parentisnullunionall/* recursion */selectchild.idasid,child.textastext,level+1aslevelfromTreeparentjoinNodechildonchild.parent.id=parent.id)cycleidsetabortto'aborted!'default''/* cycle detection */selecttext,level,abortfromTreeorderbylevelHere:
theid column is used to detect cycles, and
theabort column is set to the string value'aborted!' if a cycle is detected.
Hibernate emulates thecycle clause on databases which don’t support it natively.
The BNF forcycle is:
cycleClause : "CYCLE" identifier ("," identifier)* "SET" identifier ("TO" literal "DEFAULT" literal)? ("USING" identifier)?The column optionally specified byusing holds the path to the current row.
Thesearch clause allows us to control whether we would like the results of our query returned in an order that emulates a depth-first recursive search, or a breadth-first recursive search.
In our query above, we explicitly coded alevel column that holds the recursion depth, and ordered our result set according to this depth.With thesearch clause, that bookkeeping is already taken care of for us.
For depth-first search, we have:
withTreeas(/* base query */selectroot.idasid,root.textastextfromNoderootwhereroot.parentisnullunionall/* recursion */selectchild.idasid,child.textastextfromTreeparentjoinNodechildonchild.parent.id=parent.id)searchdepthfirstbyidsetlevel/* depth-first search */fromTreeselecttextorderbylevelAnd for breadth-first search, we only need to change a single keyword:
withTreeas(/* base query */selectroot.idasid,root.textastextfromNoderootwhereroot.parentisnullunionall/* recursion */selectchild.idasid,child.textastextfromTreeparentjoinNodechildonchild.parent.id=parent.id)searchbreadthfirstbyidsetlevel/* breadth-first search */fromTreeselecttextorderbyleveldescHibernate emulates thesearch clause on databases which don’t support it natively.
The BNF forsearch is:
searchClause : "SEARCH" ("BREADTH"|"DEPTH") "FIRST" "BY" searchSpecifications "SET" identifiersearchSpecifications : searchSpecification ("," searchSpecification)*searchSpecification : identifier sortDirection? nullsPrecedence?The full list of contributors to Hibernate ORM can be found on theGitHub repository.
The following contributors were involved in this documentation:
Gavin King