15. HQL and JPQL

The Hibernate Query Language (HQL) and Java Persistence Query Language (JPQL) are both object model focused query languages similar in nature to SQL. JPQL is a heavily-inspired-by subset of HQL. A JPQL query is always a valid HQL query, the reverse is not true, however.

Both HQL and JPQL are non-type-safe ways to perform query operations. Criteria queries offer a type-safe approach to querying. See Criteria for more information.

15.1. Example domain model

To better understand the further HQL and JPQL examples, it’s time to familiarize the domain model entities that are used in all the examples features in this chapter.

Example 483. Examples domain model

  1. @NamedQueries({
  2. @NamedQuery(
  3. name = "get_person_by_name",
  4. query = "select p from Person p where name = :name"
  5. )
  6. ,
  7. @NamedQuery(
  8. name = "get_read_only_person_by_name",
  9. query = "select p from Person p where name = :name",
  10. hints = {
  11. @QueryHint(
  12. name = "org.hibernate.readOnly",
  13. value = "true"
  14. )
  15. }
  16. )
  17. })
  18. @NamedStoredProcedureQueries(
  19. @NamedStoredProcedureQuery(
  20. name = "sp_person_phones",
  21. procedureName = "sp_person_phones",
  22. parameters = {
  23. @StoredProcedureParameter(
  24. name = "personId",
  25. type = Long.class,
  26. mode = ParameterMode.IN
  27. ),
  28. @StoredProcedureParameter(
  29. name = "personPhones",
  30. type = Class.class,
  31. mode = ParameterMode.REF_CURSOR
  32. )
  33. }
  34. )
  35. )
  36. @Entity
  37. public class Person {
  38. @Id
  39. @GeneratedValue
  40. private Long id;
  41. private String name;
  42. private String nickName;
  43. private String address;
  44. @Temporal(TemporalType.TIMESTAMP )
  45. private Date createdOn;
  46. @OneToMany(mappedBy = "person", cascade = CascadeType.ALL)
  47. @OrderColumn(name = "order_id")
  48. private List<Phone> phones = new ArrayList<>();
  49. @ElementCollection
  50. @MapKeyEnumerated(EnumType.STRING)
  51. private Map<AddressType, String> addresses = new HashMap<>();
  52. @Version
  53. private int version;
  54. //Getters and setters are omitted for brevity
  55. }
  56. public enum AddressType {
  57. HOME,
  58. OFFICE
  59. }
  60. @Entity
  61. public class Partner {
  62. @Id
  63. @GeneratedValue
  64. private Long id;
  65. private String name;
  66. @Version
  67. private int version;
  68. //Getters and setters are omitted for brevity
  69. }
  70. @Entity
  71. public class Phone {
  72. @Id
  73. private Long id;
  74. @ManyToOne(fetch = FetchType.LAZY)
  75. private Person person;
  76. @Column(name = "phone_number")
  77. private String number;
  78. @Enumerated(EnumType.STRING)
  79. @Column(name = "phone_type")
  80. private PhoneType type;
  81. @OneToMany(mappedBy = "phone", cascade = CascadeType.ALL, orphanRemoval = true)
  82. private List<Call> calls = new ArrayList<>( );
  83. @OneToMany(mappedBy = "phone")
  84. @MapKey(name = "timestamp")
  85. @MapKeyTemporal(TemporalType.TIMESTAMP )
  86. private Map<Date, Call> callHistory = new HashMap<>();
  87. @ElementCollection
  88. private List<Date> repairTimestamps = new ArrayList<>( );
  89. //Getters and setters are omitted for brevity
  90. }
  91. public enum PhoneType {
  92. LAND_LINE,
  93. MOBILE;
  94. }
  95. @Entity
  96. @Table(name = "phone_call")
  97. public class Call {
  98. @Id
  99. @GeneratedValue
  100. private Long id;
  101. @ManyToOne
  102. private Phone phone;
  103. @Column(name = "call_timestamp")
  104. private Date timestamp;
  105. private int duration;
  106. //Getters and setters are omitted for brevity
  107. }
  108. @Entity
  109. @Inheritance(strategy = InheritanceType.JOINED)
  110. public class Payment {
  111. @Id
  112. @GeneratedValue
  113. private Long id;
  114. private BigDecimal amount;
  115. private boolean completed;
  116. @ManyToOne
  117. private Person person;
  118. //Getters and setters are omitted for brevity
  119. }
  120. @Entity
  121. public class CreditCardPayment extends Payment {
  122. }
  123. @Entity
  124. public class WireTransferPayment extends Payment {
  125. }

15.2. Query API

When using Hibernate, you can execute entity queries either via JPA or the Hibernate-specific API. Since 5.2, the Hibernate Session interface extends the JPA EntityManager interface. For this reason, the query API was also merged, and now the Hibernate org.hibernate.query.Query interface extends the JPA javax.persistence.Query.

Next, we are going to see how the query API differs between the standard JPA interfaces and the Hibernate-specific API.

15.2.1. JPA Query API

In JPA, the query is represented by javax.persistence.Query or javax.persistence.TypedQuery as obtained from the EntityManager. The create an inline Query or TypedQuery, you need to use the EntityManager#createQuery method. For named queries, the EntityManager#createNamedQuery method is needed.

Example 484. Obtaining a JPA Query or a TypedQuery reference

  1. Query query = entityManager.createQuery(
  2. "select p " +
  3. "from Person p " +
  4. "where p.name like :name"
  5. );
  6. TypedQuery<Person> typedQuery = entityManager.createQuery(
  7. "select p " +
  8. "from Person p " +
  9. "where p.name like :name", Person.class
  10. );

Example 485. Obtaining a JPA Query or a TypedQuery reference for a named query

  1. @NamedQuery(
  2. name = "get_person_by_name",
  3. query = "select p from Person p where name = :name"
  4. )
  5. Query query = entityManager.createNamedQuery( "get_person_by_name" );
  6. TypedQuery<Person> typedQuery = entityManager.createNamedQuery(
  7. "get_person_by_name", Person.class
  8. );

Hibernate offers a specific @NamedQuery annotation which provides ways to configure various query features, like flush mode, cacheability, time out interval.

Example 486. Obtaining a Hibernate Query or a TypedQuery reference for a named query

  1. @NamedQueries({
  2. @NamedQuery(
  3. name = "get_phone_by_number",
  4. query = "select p " +
  5. "from Phone p " +
  6. "where p.number = :number",
  7. timeout = 1,
  8. readOnly = true
  9. )
  10. })
  11. Phone phone = entityManager
  12. .createNamedQuery( "get_phone_by_number", Phone.class )
  13. .setParameter( "number", "123-456-7890" )
  14. .getSingleResult();

The Query interface can then be used to control the execution of the query. For example, we may want to specify an execution timeout or control caching.

Example 487. Basic JPA Query usage

  1. Query query = entityManager.createQuery(
  2. "select p " +
  3. "from Person p " +
  4. "where p.name like :name" )
  5. // timeout - in milliseconds
  6. .setHint( "javax.persistence.query.timeout", 2000 )
  7. // flush only at commit time
  8. .setFlushMode( FlushModeType.COMMIT );

For complete details, see the Query Javadocs. Many of the settings controlling the execution of the query are defined as hints. JPA defines some standard hints (like timeout in the example), but most are provider specific. Relying on provider specific hints limits your applications portability to some degree.

javax.persistence.query.timeout

Defines the query timeout, in milliseconds.

javax.persistence.fetchgraph

Defines a fetchgraph EntityGraph. Attributes explicitly specified as AttributeNodes are treated as FetchType.EAGER (via join fetch or subsequent select). For details, see the EntityGraph discussions in Fetching.

javax.persistence.loadgraph

Defines a loadgraph EntityGraph. Attributes explicitly specified as AttributeNodes are treated as FetchType.EAGER (via join fetch or subsequent select). Attributes that are not specified are treated as FetchType.LAZY or FetchType.EAGER depending on the attribute’s definition in metadata. For details, see the EntityGraph discussions in Fetching.

org.hibernate.cacheMode

Defines the CacheMode to use. See org.hibernate.query.Query#setCacheMode.

org.hibernate.cacheable

Defines whether the query is cacheable. true/false. See org.hibernate.query.Query#setCacheable.

org.hibernate.cacheRegion

For queries that are cacheable, defines a specific cache region to use. See org.hibernate.query.Query#setCacheRegion.

org.hibernate.comment

Defines the comment to apply to the generated SQL. See org.hibernate.query.Query#setComment.

org.hibernate.fetchSize

Defines the JDBC fetch-size to use. See org.hibernate.query.Query#setFetchSize.

org.hibernate.flushMode

Defines the Hibernate-specific FlushMode to use. See org.hibernate.query.Query#setFlushMode. If possible, prefer using javax.persistence.Query#setFlushMode instead.

org.hibernate.readOnly

Defines that entities and collections loaded by this query should be marked as read-only. See org.hibernate.query.Query#setReadOnly.

The final thing that needs to happen before the query can be executed is to bind the values for any defined parameters. JPA defines a simplified set of parameter binding methods. Essentially, it supports setting the parameter value (by name/position) and a specialized form for Calendar/Date types additionally accepting a TemporalType.

Example 488. JPA name parameter binding

  1. Query query = entityManager.createQuery(
  2. "select p " +
  3. "from Person p " +
  4. "where p.name like :name" )
  5. .setParameter( "name", "J%" );
  6. // For generic temporal field types (e.g. `java.util.Date`, `java.util.Calendar`)
  7. // we also need to provide the associated `TemporalType`
  8. Query query = entityManager.createQuery(
  9. "select p " +
  10. "from Person p " +
  11. "where p.createdOn > :timestamp" )
  12. .setParameter( "timestamp", timestamp, TemporalType.DATE );

JPQL-style positional parameters are declared using a question mark followed by an ordinal - ?1, ?2. The ordinals start with 1. Just like with named parameters, positional parameters can also appear multiple times in a query.

Example 489. JPA positional parameter binding

  1. Query query = entityManager.createQuery(
  2. "select p " +
  3. "from Person p " +
  4. "where p.name like ?1" )
  5. .setParameter( 1, "J%" );

It’s good practice not to mix parameter binding forms in a given query.

In terms of execution, JPA Query offers 3 different methods for retrieving a result set.

  • Query#getResultList() - executes the select query and returns back the list of results.

  • Query#getResultStream() - executes the select query and returns back a Stream over the results.

  • Query#getSingleResult() - executes the select query and returns a single result. If there were more than one result an exception is thrown.

Example 490. JPA getResultList() result

  1. List<Person> persons = entityManager.createQuery(
  2. "select p " +
  3. "from Person p " +
  4. "where p.name like :name" )
  5. .setParameter( "name", "J%" )
  6. .getResultList();

Example 491. JPA getResultStream() result

  1. try(Stream<Person> personStream = entityManager.createQuery(
  2. "select p " +
  3. "from Person p " +
  4. "where p.name like :name", Person.class )
  5. .setParameter( "name", "J%" )
  6. .getResultStream()) {
  7. List<Person> persons = personStream
  8. .skip( 5 )
  9. .limit( 5 )
  10. .collect( Collectors.toList() );
  11. }

Example 492. JPA getSingleResult()

  1. Person person = (Person) entityManager.createQuery(
  2. "select p " +
  3. "from Person p " +
  4. "where p.name like :name" )
  5. .setParameter( "name", "J%" )
  6. .getSingleResult();

15.2.2. Hibernate Query API

In Hibernate, the HQL query is represented as org.hibernate.query.Query which is obtained from a Session. If the HQL is a named query, Session#getNamedQuery would be used; otherwise Session#createQuery is needed.

Example 493. Obtaining a Hibernate Query

  1. org.hibernate.query.Query query = session.createQuery(
  2. "select p " +
  3. "from Person p " +
  4. "where p.name like :name"
  5. );

Example 494. Obtaining a Hibernate Query reference for a named query

  1. org.hibernate.query.Query query = session.getNamedQuery( "get_person_by_name" );

Not only was the JPQL syntax heavily inspired by HQL, but many of the JPA APIs were heavily inspired by Hibernate too. The two Query contracts are very similar.

The Query interface can then be used to control the execution of the query. For example, we may want to specify an execution timeout or control caching.

Example 495. Basic Query usage - Hibernate

  1. org.hibernate.query.Query query = session.createQuery(
  2. "select p " +
  3. "from Person p " +
  4. "where p.name like :name" )
  5. // timeout - in seconds
  6. .setTimeout( 2 )
  7. // write to L2 caches, but do not read from them
  8. .setCacheMode( CacheMode.REFRESH )
  9. // assuming query cache was enabled for the SessionFactory
  10. .setCacheable( true )
  11. // add a comment to the generated SQL if enabled via the hibernate.use_sql_comments configuration property
  12. .setComment( "+ INDEX(p idx_person_name)" );

For complete details, see the Query Javadocs.

Query hints here are database query hints. They are added directly to the generated SQL according to Dialect#getQueryHintString.

The JPA notion of query hints, on the other hand, refer to hints that target the provider (Hibernate). So even though they are called the same, be aware they have a very different purpose. Also, be aware that Hibernate query hints generally make the application non-portable across databases unless the code adding them first checks the Dialect.

Flushing is covered in detail in Flushing. Locking is covered in detail in Locking. The concept of read-only state is covered in Persistence Contexts.

Hibernate also allows an application to hook into the process of building the query results via the org.hibernate.transform.ResultTransformer contract. See its Javadocs as well as the Hibernate-provided implementations for additional details.

The last thing that needs to happen before we can execute the query is to bind the values for any parameters defined in the query. Query defines many overloaded methods for this purpose. The most generic form takes the value as well as the Hibernate Type.

Example 496. Hibernate name parameter binding

  1. org.hibernate.query.Query query = session.createQuery(
  2. "select p " +
  3. "from Person p " +
  4. "where p.name like :name" )
  5. .setParameter( "name", "J%", StringType.INSTANCE );

Hibernate generally understands the expected type of the parameter given its context in the query. In the previous example since we are using the parameter in a LIKE comparison against a String-typed attribute Hibernate would automatically infer the type; so the above could be simplified.

Example 497. Hibernate name parameter binding (inferred type)

  1. org.hibernate.query.Query query = session.createQuery(
  2. "select p " +
  3. "from Person p " +
  4. "where p.name like :name" )
  5. .setParameter( "name", "J%" );

There are also short hand forms for binding common types such as strings, booleans, integers, etc.

Example 498. Hibernate name parameter binding (short forms)

  1. org.hibernate.query.Query query = session.createQuery(
  2. "select p " +
  3. "from Person p " +
  4. "where p.name like :name " +
  5. " and p.createdOn > :timestamp" )
  6. .setParameter( "name", "J%" )
  7. .setParameter( "timestamp", timestamp, TemporalType.TIMESTAMP);

Traditionally, Hibernate used to support a JDBC positional parameter syntax form via a ? symbol without a following ordinal.

There was no way to relate two such positional parameters as being “the same” aside from binding the same value to each and, for this reason, this form is no longer supported.

  1. org.hibernate.query.Query query = session.createQuery(
  2. select p +
  3. from Person p +
  4. where p.name like ?” )
  5. .setParameter( 1, J%” );

In terms of execution, Hibernate offers 4 different methods. The 2 most commonly used are

  • Query#list - executes the select query and returns back the list of results.

  • Query#uniqueResult - executes the select query and returns the single result. If there were more than one result an exception is thrown.

Example 499. Hibernate list() result

  1. List<Person> persons = session.createQuery(
  2. "select p " +
  3. "from Person p " +
  4. "where p.name like :name" )
  5. .setParameter( "name", "J%" )
  6. .list();

It is also possible to extract a single result from a Query.

Example 500. Hibernate uniqueResult()

  1. Person person = (Person) session.createQuery(
  2. "select p " +
  3. "from Person p " +
  4. "where p.name like :name" )
  5. .setParameter( "name", "J%" )
  6. .uniqueResult();

If the unique result is used often and the attributes upon which it is based are unique, you may want to consider mapping a natural-id and using the natural-id loading API. See the Natural Ids for more information on this topic.

15.2.3. Query scrolling

Hibernate offers additional, specialized methods for scrolling the query and handling results using a server-side cursor.

Query#scroll works in tandem with the JDBC notion of a scrollable ResultSet.

The Query#scroll method is overloaded:

  • The main form accepts a single argument of type org.hibernate.ScrollMode which indicates the type of scrolling to be used. See the Javadocs for the details on each.

  • The second form takes no argument and will use the ScrollMode indicated by Dialect#defaultScrollMode.

Query#scroll returns a org.hibernate.ScrollableResults which wraps the underlying JDBC (scrollable) ResultSet and provides access to the results. Unlike a typical forward-only ResultSet, the ScrollableResults allows you to navigate the ResultSet in any direction.

Example 501. Scrolling through a ResultSet containing entities

  1. try ( ScrollableResults scrollableResults = session.createQuery(
  2. "select p " +
  3. "from Person p " +
  4. "where p.name like :name" )
  5. .setParameter( "name", "J%" )
  6. .scroll()
  7. ) {
  8. while(scrollableResults.next()) {
  9. Person person = (Person) scrollableResults.get()[0];
  10. process(person);
  11. }
  12. }

Since this form holds the JDBC ResultSet open, the application should indicate when it is done with the ScrollableResults by calling its close() method (as inherited from java.io.Closeable so that ScrollableResults will work with try-with-resources blocks).

If left unclosed by the application, Hibernate will automatically close the underlying resources (e.g. ResultSet and PreparedStatement) used internally by the ScrollableResults when the current transaction is ended (either commit or rollback).

However, it is good practice to close the ScrollableResults explicitly.

If you plan to use Query#scroll with collection fetches it is important that your query explicitly order the results so that the JDBC results contain the related rows sequentially.

Hibernate also supports Query#iterate, which is intended for loading entities when it is known that the loaded entries are already stored in the second-level cache. The idea behind iterate is that just the matching identifiers will be obtained in the SQL query. From these the identifiers are resolved by second-level cache lookup. If these second-level cache lookups fail, additional queries will need to be issued against the database.

This operation can perform significantly better for loading large numbers of entities that for certain already exist in the second-level cache. In cases where many of the entities do not exist in the second-level cache, this operation will almost definitely perform worse.

The Iterator returned from Query#iterate is actually a specially typed Iterator: org.hibernate.engine.HibernateIterator. It is specialized to expose a close() method (again, inherited from java.io.Closeable). When you are done with this Iterator you should close it, either by casting to HibernateIterator or Closeable, or by calling Hibernate#close(java.util.Iterator).

Since 5.2, Hibernate offers support for returning a Stream which can be later used to transform the underlying ResultSet.

Internally, the stream() behaves like a Query#scroll and the underlying result is backed by a ScrollableResults.

Fetching a projection using the Query#stream method can be done as follows:

Example 502. Hibernate stream() using a projection result type

  1. try ( Stream<Object[]> persons = session.createQuery(
  2. "select p.name, p.nickName " +
  3. "from Person p " +
  4. "where p.name like :name" )
  5. .setParameter( "name", "J%" )
  6. .stream() ) {
  7. persons
  8. .map( row -> new PersonNames(
  9. (String) row[0],
  10. (String) row[1] ) )
  11. .forEach( this::process );
  12. }

When fetching a single result, like a Person entity, instead of a Stream<Object[]>, Hibernate is going to figure out the actual type, so the result is a Stream<Person>.

Example 503. Hibernate stream() using an entity result type

  1. try( Stream<Person> persons = session.createQuery(
  2. "select p " +
  3. "from Person p " +
  4. "where p.name like :name" )
  5. .setParameter( "name", "J%" )
  6. .stream() ) {
  7. Map<Phone, List<Call>> callRegistry = persons
  8. .flatMap( person -> person.getPhones().stream() )
  9. .flatMap( phone -> phone.getCalls().stream() )
  10. .collect( Collectors.groupingBy( Call::getPhone ) );
  11. process(callRegistry);
  12. }

Just like with ScrollableResults, you should always close a Hibernate Stream either explicitly or using a try-with-resources block.

15.2.4. Query streaming

Since version 2.2, the JPA Query interface offers support for returning a Stream via the getResultStream method.

Just like the scroll method, you can use a try-with-resources block to close the Stream prior to closing the currently running Persistence Context.

Since Hibernate 5.4, the Stream is also closed when calling a terminal operation, as illustrated by the following example.

  1. List<Person> persons = entityManager.createQuery(
  2. "select p " +
  3. "from Person p " +
  4. "where p.name like :name", Person.class )
  5. .setParameter( "name", "J%" )
  6. .getResultStream()
  7. .skip( 5 )
  8. .limit( 5 )
  9. .collect( Collectors.toList() );

The Stream is closed automatically after calling the collect method, since there is no reason to keep the underlying JDBC ResultSet open if the Stream cannot be reused.

15.3. Case Sensitivity

With the exception of names of Java classes and properties, queries are case-insensitive. So SeLeCT is the same as sELEct is the same as SELECT, but org.hibernate.eg.FOO and org.hibernate.eg.Foo are different, as are foo.barSet and foo.BARSET.

This documentation uses lowercase keywords as a convention in query examples.

15.4. Statement types

Both HQL and JPQL allow SELECT, UPDATE and DELETE statements to be performed. HQL additionally allows INSERT statements, in a form similar to a SQL INSERT FROM SELECT.

Care should be taken as to when an UPDATE or DELETE statement is executed.

Caution should be used when executing bulk update or delete operations because they may result in inconsistencies between the database and the entities in the active persistence context. In general, bulk update and delete operations should only be performed within a transaction in a new persistence context or before fetching or accessing entities whose state might be affected by such operations.

— Section 4.10 of the JPA 2.0 Specification

15.5. Select statements

The BNF for SELECT statements in HQL is:

  1. select_statement :: =
  2. [select_clause]
  3. from_clause
  4. [where_clause]
  5. [groupby_clause]
  6. [having_clause]
  7. [orderby_clause]

The simplest possible HQL SELECT statement is of the form:

  1. List<Person> persons = session.createQuery(
  2. "from Person" )
  3. .list();

The select statement in JPQL is exactly the same as for HQL except that JPQL requires a select_clause, whereas HQL does not.

  1. List<Person> persons = entityManager.createQuery(
  2. select p +
  3. from Person p”, Person.class )
  4. .getResultList();

Even though HQL does not require the presence of a select_clause, it is generally good practice to include one. For simple queries the intent is clear and so the intended result of the select_clause is easy to infer. But on more complex queries that is not always the case.

It is usually better to explicitly specify intent. Hibernate does not actually enforce that a select_clause be present even when parsing JPQL queries, however, applications interested in JPA portability should take heed of this.

15.6. Update statements

The BNF for UPDATE statements is the same in HQL and JPQL:

  1. update_statement ::=
  2. update_clause [where_clause]
  3. update_clause ::=
  4. UPDATE entity_name [[AS] identification_variable]
  5. SET update_item {, update_item}*
  6. update_item ::=
  7. [identification_variable.]{state_field | single_valued_object_field} = new_value
  8. new_value ::=
  9. scalar_expression | simple_entity_expression | NULL

UPDATE statements, by default, do not affect the version or the timestamp attribute values for the affected entities.

However, you can force Hibernate to set the version or timestamp attribute values through the use of a versioned update. This is achieved by adding the VERSIONED keyword after the UPDATE keyword.

Versioned updates is a Hibernate-specific feature and will not work in a portable manner.

Custom version types, org.hibernate.usertype.UserVersionType, are not allowed in conjunction with an update versioned statement.

An UPDATE statement is executed using the executeUpdate() of either org.hibernate.query.Query or javax.persistence.Query. The method is named for those familiar with the JDBC executeUpdate() on java.sql.PreparedStatement.

The int value returned by the executeUpdate() method indicates the number of entities affected by the operation. This may or may not correlate to the number of rows affected in the database. An HQL bulk operation might result in multiple actual SQL statements being executed (for joined-subclass, for example). The returned number indicates the number of actual entities affected by the statement. Using a JOINED inheritance hierarchy, a delete against one of the subclasses may actually result in deletes against not just the table to which that subclass is mapped, but also the “root” table and tables “in between”.

Example 504. UPDATE query statements

  1. int updatedEntities = entityManager.createQuery(
  2. "update Person p " +
  3. "set p.name = :newName " +
  4. "where p.name = :oldName" )
  5. .setParameter( "oldName", oldName )
  6. .setParameter( "newName", newName )
  7. .executeUpdate();
  8. int updatedEntities = session.createQuery(
  9. "update Person " +
  10. "set name = :newName " +
  11. "where name = :oldName" )
  12. .setParameter( "oldName", oldName )
  13. .setParameter( "newName", newName )
  14. .executeUpdate();
  15. int updatedEntities = session.createQuery(
  16. "update versioned Person " +
  17. "set name = :newName " +
  18. "where name = :oldName" )
  19. .setParameter( "oldName", oldName )
  20. .setParameter( "newName", newName )
  21. .executeUpdate();

Neither UPDATE nor DELETE statements allow implicit joins. Their form already disallows explicit joins too.

15.7. Delete statements

The BNF for DELETE statements is the same in HQL and JPQL:

  1. delete_statement ::=
  2. delete_clause [where_clause]
  3. delete_clause ::=
  4. DELETE FROM entity_name [[AS] identification_variable]

A DELETE statement is also executed using the executeUpdate() method of either org.hibernate.query.Query or javax.persistence.Query.

15.8. Insert statements

HQL adds the ability to define INSERT statements as well.

There is no JPQL equivalent to HQL-style INSERT statements.

The BNF for an HQL INSERT statement is:

  1. insert_statement ::=
  2. insert_clause select_statement
  3. insert_clause ::=
  4. INSERT INTO entity_name (attribute_list)
  5. attribute_list ::=
  6. state_field[, state_field ]*

The attribute_list is analogous to the column specification in the SQL INSERT statement. For entities involved in mapped inheritance, only attributes directly defined on the named entity can be used in the attribute_list. Superclass properties are not allowed and subclass properties do not make sense. In other words, INSERT statements are inherently non-polymorphic.

select_statement can be any valid HQL select query, with the caveat that the return types must match the types expected by the insert. Currently, this is checked during query compilation rather than allowing the check to delegate to the database. This may cause problems between Hibernate Types which are equivalent as opposed to equal. For example, this might lead to issues with mismatches between an attribute mapped as a org.hibernate.type.DateType and an attribute defined as a org.hibernate.type.TimestampType, even though the database might not make a distinction or might be able to handle the conversion.

For the id attribute, the insert statement gives you two options. You can either explicitly specify the id property in the attribute_list, in which case its value is taken from the corresponding select expression, or omit it from the attribute_list in which case a generated value is used. This latter option is only available when using id generators that operate “in the database”; attempting to use this option with any “in memory” type generators will cause an exception during parsing.

For optimistic locking attributes, the insert statement again gives you two options. You can either specify the attribute in the attribute_list in which case its value is taken from the corresponding select expressions or omit it from the attribute_list in which case the seed value defined by the corresponding org.hibernate.type.VersionType is used.

Example 505. INSERT query statements

  1. int insertedEntities = session.createQuery(
  2. "insert into Partner (id, name) " +
  3. "select p.id, p.name " +
  4. "from Person p ")
  5. .executeUpdate();

15.9. The FROM clause

The FROM clause is responsible for defining the scope of object model types available to the rest of the query. It is also responsible for defining all the “identification variables” available to the rest of the query.

15.10. Identification variables

Identification variables are often referred to as aliases. References to object model classes in the FROM clause can be associated with an identification variable that can then be used to refer to that type throughout the rest of the query.

In most cases declaring an identification variable is optional, though it is usually good practice to declare them.

An identification variable must follow the rules for Java identifier validity.

According to JPQL, identification variables must be treated as case-insensitive. Good practice says you should use the same case throughout a query to refer to a given identification variable. In other words, JPQL says they can be case-insensitive and so Hibernate must be able to treat them as such, but this does not make it good practice.

15.11. Root entity references

A root entity reference, or what JPA calls a range variable declaration, is specifically a reference to a mapped entity type from the application. It cannot name component/embeddable types. And associations, including collections, are handled in a different manner, as later discussed.

The BNF for a root entity reference is:

  1. root_entity_reference ::=
  2. entity_name [AS] identification_variable

Example 506. Simple query example

  1. List<Person> persons = entityManager.createQuery(
  2. "select p " +
  3. "from org.hibernate.userguide.model.Person p", Person.class )
  4. .getResultList();

We see that the query is defining a root entity reference to the org.hibernate.userguide.model.Person object model type. Additionally, it declares an alias of p to that org.hibernate.userguide.model.Person reference, which is the identification variable.

Usually, the root entity reference represents just the entity name rather than the entity class FQN (fully-qualified name). By default, the entity name is the unqualified entity class name, here Person.

Example 507. Simple query using entity name for root entity reference

  1. List<Person> persons = entityManager.createQuery(
  2. "select p " +
  3. "from Person p", Person.class )
  4. .getResultList();

Multiple root entity references can also be specified, even when naming the same entity.

Example 508. Simple query using multiple root entity references

  1. List<Object[]> persons = entityManager.createQuery(
  2. "select distinct pr, ph " +
  3. "from Person pr, Phone ph " +
  4. "where ph.person = pr and ph is not null", Object[].class)
  5. .getResultList();
  1. List<Person> persons = entityManager.createQuery(
  2. "select distinct pr1 " +
  3. "from Person pr1, Person pr2 " +
  4. "where pr1.id <> pr2.id " +
  5. " and pr1.address = pr2.address " +
  6. " and pr1.createdOn < pr2.createdOn", Person.class )
  7. .getResultList();

15.12. Explicit joins

The FROM clause can also contain explicit relationship joins using the join keyword. These joins can be either inner or left outer style joins.

Example 509. Explicit inner join examples

  1. List<Person> persons = entityManager.createQuery(
  2. "select distinct pr " +
  3. "from Person pr " +
  4. "join pr.phones ph " +
  5. "where ph.type = :phoneType", Person.class )
  6. .setParameter( "phoneType", PhoneType.MOBILE )
  7. .getResultList();
  8. // same query but specifying join type as 'inner' explicitly
  9. List<Person> persons = entityManager.createQuery(
  10. "select distinct pr " +
  11. "from Person pr " +
  12. "inner join pr.phones ph " +
  13. "where ph.type = :phoneType", Person.class )
  14. .setParameter( "phoneType", PhoneType.MOBILE )
  15. .getResultList();

Example 510. Explicit left (outer) join examples

  1. List<Person> persons = entityManager.createQuery(
  2. "select distinct pr " +
  3. "from Person pr " +
  4. "left join pr.phones ph " +
  5. "where ph is null " +
  6. " or ph.type = :phoneType", Person.class )
  7. .setParameter( "phoneType", PhoneType.LAND_LINE )
  8. .getResultList();
  9. // functionally the same query but using the 'left outer' phrase
  10. List<Person> persons = entityManager.createQuery(
  11. "select distinct pr " +
  12. "from Person pr " +
  13. "left outer join pr.phones ph " +
  14. "where ph is null " +
  15. " or ph.type = :phoneType", Person.class )
  16. .setParameter( "phoneType", PhoneType.LAND_LINE )
  17. .getResultList();

HQL also defines a WITH clause to qualify the join conditions.

The HQL-style WITH keyword is specific to Hibernate. JPQL defines the ON clause for this feature.

Example 511. HQL WITH clause join example

  1. List<Object[]> personsAndPhones = session.createQuery(
  2. "select pr.name, ph.number " +
  3. "from Person pr " +
  4. "left join pr.phones ph with ph.type = :phoneType " )
  5. .setParameter( "phoneType", PhoneType.LAND_LINE )
  6. .list();

Example 512. JPQL ON clause join example

  1. List<Object[]> personsAndPhones = entityManager.createQuery(
  2. "select pr.name, ph.number " +
  3. "from Person pr " +
  4. "left join pr.phones ph on ph.type = :phoneType " )
  5. .setParameter( "phoneType", PhoneType.LAND_LINE )
  6. .getResultList();

The important distinction is that in the generated SQL the conditions of the WITH/ON clause are made part of the ON clause in the generated SQL, as opposed to the other queries in this section where the HQL/JPQL conditions are made part of the WHERE clause in the generated SQL.

The distinction in this specific example is probably not that significant. The with clause is sometimes necessary for more complicated queries.

Explicit joins may reference association or component/embedded attributes. In the case of component/embedded attributes, the join is simply logical and does not correlate to a physical (SQL) join. For further information about collection-valued association references, see Collection member references.

An important use case for explicit joins is to define FETCH JOINs which override the laziness of the joined association. As an example, given an entity named Person with a collection-valued association named phones, the JOIN FETCH will also load the child collection in the same SQL query:

Example 513. Fetch join example

  1. // functionally the same query but using the 'left outer' phrase
  2. List<Person> persons = entityManager.createQuery(
  3. "select distinct pr " +
  4. "from Person pr " +
  5. "left join fetch pr.phones ", Person.class )
  6. .getResultList();

As you can see from the example, a fetch join is specified by injecting the keyword fetch after the keyword join. In the example, we used a left outer join because we also wanted to return customers who have no orders.

Inner joins can also be fetched, but inner joins filter out the root entity. In the example, using an inner join instead would have resulted in customers without any orders being filtered out of the result.

Fetch joins are not valid in sub-queries.

Care should be taken when fetch joining a collection-valued association which is in any way further restricted (the fetched collection will be restricted too). For this reason, it is usually considered best practice not to assign an identification variable to fetched joins except for the purpose of specifying nested fetch joins.

Fetch joins should not be used in paged queries (e.g. setFirstResult() or setMaxResults()), nor should they be used with the scroll() or iterate() features.

15.13. Implicit joins (path expressions)

Another means of adding to the scope of object model types available to the query is through the use of implicit joins or path expressions.

Example 514. Simple implicit join example

  1. List<Phone> phones = entityManager.createQuery(
  2. "select ph " +
  3. "from Phone ph " +
  4. "where ph.person.address = :address ", Phone.class )
  5. .setParameter( "address", address )
  6. .getResultList();
  7. // same as
  8. List<Phone> phones = entityManager.createQuery(
  9. "select ph " +
  10. "from Phone ph " +
  11. "join ph.person pr " +
  12. "where pr.address = :address ", Phone.class )
  13. .setParameter( "address", address)
  14. .getResultList();

An implicit join always starts from an identification variable, followed by the navigation operator ( . ), followed by an attribute for the object model type referenced by the initial identification variable. In the example, the initial identification variable is ph which refers to the Phone entity. The ph.person reference then refers to the person attribute of the Phone entity. person is an association type so we further navigate to its age attribute.

If the attribute represents an entity association (non-collection) or a component/embedded, that reference can be further navigated. Basic values and collection-valued associations cannot be further navigated.

As shown in the example, implicit joins can appear outside the FROM clause. However, they affect the FROM clause.

Implicit joins are always treated as inner joins.

Multiple references to the same implicit join always refer to the same logical and physical (SQL) join.

Example 515. Reused implicit join

  1. List<Phone> phones = entityManager.createQuery(
  2. "select ph " +
  3. "from Phone ph " +
  4. "where ph.person.address = :address " +
  5. " and ph.person.createdOn > :timestamp", Phone.class )
  6. .setParameter( "address", address )
  7. .setParameter( "timestamp", timestamp )
  8. .getResultList();
  9. //same as
  10. List<Phone> phones = entityManager.createQuery(
  11. "select ph " +
  12. "from Phone ph " +
  13. "inner join ph.person pr " +
  14. "where pr.address = :address " +
  15. " and pr.createdOn > :timestamp", Phone.class )
  16. .setParameter( "address", address )
  17. .setParameter( "timestamp", timestamp )
  18. .getResultList();

Just as with explicit joins, implicit joins may reference association or component/embedded attributes. For further information about collection-valued association references, see Collection member references.

In the case of component/embedded attributes, the join is simply logical and does not correlate to a physical (SQL) join. Unlike explicit joins, however, implicit joins may also reference basic state fields as long as the path expression ends there.

15.14. Distinct

For JPQL and HQL, DISTINCT has two meanings:

  1. It can be passed to the database so that duplicates are removed from a result set

  2. It can be used to filter out the same parent entity references when join fetching a child collection

15.14.1. Using DISTINCT with SQL projections

For SQL projections, DISTINCT needs to be passed to the database because the duplicated entries need to be filtered out before being returned to the database client.

Example 516. Using DISTINCT with projection queries example

  1. List<String> lastNames = entityManager.createQuery(
  2. "select distinct p.lastName " +
  3. "from Person p", String.class)
  4. .getResultList();

When running the query above, Hibernate generates the following SQL query:

  1. SELECT DISTINCT
  2. p.last_name as col_0_0_
  3. FROM person p

For this particular use case, passing the DISTINCT keyword from JPQL/HQL to the database is the right thing to do.

15.14.2. Using DISTINCT with entity queries

DISTINCT can also be used to filter out entity object references when fetching a child association along with the parent entities.

Example 517. Using DISTINCT with entity queries example

  1. List<Person> authors = entityManager.createQuery(
  2. "select distinct p " +
  3. "from Person p " +
  4. "left join fetch p.books", Person.class)
  5. .getResultList();

In this case, DISTINCT is used because there can be multiple Books entities associated with a given Person. If in the database there are 3 Persons in the database and each person has 2 Books, without DISTINCT this query will return 6 Persons since the SQL-level result-set size is given by the number of joined Book records.

However, the DISTINCT keyword is passed to the database as well:

  1. SELECT DISTINCT
  2. p.id as id1_1_0_,
  3. b.id as id1_0_1_,
  4. p.first_name as first_na2_1_0_,
  5. p.last_name as last_nam3_1_0_,
  6. b.author_id as author_i3_0_1_,
  7. b.title as title2_0_1_,
  8. b.author_id as author_i3_0_0__,
  9. b.id as id1_0_0__
  10. FROM person p
  11. LEFT OUTER JOIN book b ON p.id=b.author_id

In this case, the DISTINCT SQL keyword is undesirable since it does a redundant result set sorting, as explained in this blog post. To fix this issue, Hibernate 5.2.2 added support for the HINT_PASS_DISTINCT_THROUGH entity query hint:

Example 518. Using DISTINCT with entity queries example

  1. List<Person> authors = entityManager.createQuery(
  2. "select distinct p " +
  3. "from Person p " +
  4. "left join fetch p.books", Person.class)
  5. .setHint( QueryHints.HINT_PASS_DISTINCT_THROUGH, false )
  6. .getResultList();

With this entity query hint, Hibernate will not pass the DISTINCT keyword to the SQL query:

  1. SELECT
  2. p.id as id1_1_0_,
  3. b.id as id1_0_1_,
  4. p.first_name as first_na2_1_0_,
  5. p.last_name as last_nam3_1_0_,
  6. b.author_id as author_i3_0_1_,
  7. b.title as title2_0_1_,
  8. b.author_id as author_i3_0_0__,
  9. b.id as id1_0_0__
  10. FROM person p
  11. LEFT OUTER JOIN book b ON p.id=b.author_id

When using the HINT_PASS_DISTINCT_THROUGH entity query hint, Hibernate can still remove the duplicated parent-side entities from the query result.

15.15. Collection member references

References to collection-valued associations actually refer to the values of that collection.

Example 519. Collection references example

  1. List<Phone> phones = entityManager.createQuery(
  2. "select ph " +
  3. "from Person pr " +
  4. "join pr.phones ph " +
  5. "join ph.calls c " +
  6. "where pr.address = :address " +
  7. " and c.duration > :duration", Phone.class )
  8. .setParameter( "address", address )
  9. .setParameter( "duration", duration )
  10. .getResultList();
  11. // alternate syntax
  12. List<Phone> phones = session.createQuery(
  13. "select ph " +
  14. "from Person pr, " +
  15. "in (pr.phones) ph, " +
  16. "in (ph.calls) c " +
  17. "where pr.address = :address " +
  18. " and c.duration > :duration" )
  19. .setParameter( "address", address )
  20. .setParameter( "duration", duration )
  21. .list();

In the example, the identification variable ph actually refers to the object model type Phone, which is the type of the elements of the Person#phones association.

The example also shows the alternate syntax for specifying collection association joins using the IN syntax. Both forms are equivalent. Which form an application chooses to use is simply a matter of taste.

15.16. Special case - qualified path expressions

We said earlier that collection-valued associations actually refer to the values of that collection. Based on the type of collection, there are also a set of explicit qualification expressions available.

Example 520. Qualified collection references example

  1. @OneToMany(mappedBy = "phone")
  2. @MapKey(name = "timestamp")
  3. @MapKeyTemporal(TemporalType.TIMESTAMP )
  4. private Map<Date, Call> callHistory = new HashMap<>();
  5. // select all the calls (the map value) for a given Phone
  6. List<Call> calls = entityManager.createQuery(
  7. "select ch " +
  8. "from Phone ph " +
  9. "join ph.callHistory ch " +
  10. "where ph.id = :id ", Call.class )
  11. .setParameter( "id", id )
  12. .getResultList();
  13. // same as above
  14. List<Call> calls = entityManager.createQuery(
  15. "select value(ch) " +
  16. "from Phone ph " +
  17. "join ph.callHistory ch " +
  18. "where ph.id = :id ", Call.class )
  19. .setParameter( "id", id )
  20. .getResultList();
  21. // select all the Call timestamps (the map key) for a given Phone
  22. List<Date> timestamps = entityManager.createQuery(
  23. "select key(ch) " +
  24. "from Phone ph " +
  25. "join ph.callHistory ch " +
  26. "where ph.id = :id ", Date.class )
  27. .setParameter( "id", id )
  28. .getResultList();
  29. // select all the Call and their timestamps (the 'Map.Entry') for a given Phone
  30. List<Map.Entry<Date, Call>> callHistory = entityManager.createQuery(
  31. "select entry(ch) " +
  32. "from Phone ph " +
  33. "join ph.callHistory ch " +
  34. "where ph.id = :id " )
  35. .setParameter( "id", id )
  36. .getResultList();
  37. // Sum all call durations for a given Phone of a specific Person
  38. Long duration = entityManager.createQuery(
  39. "select sum(ch.duration) " +
  40. "from Person pr " +
  41. "join pr.phones ph " +
  42. "join ph.callHistory ch " +
  43. "where ph.id = :id " +
  44. " and index(ph) = :phoneIndex", Long.class )
  45. .setParameter( "id", id )
  46. .setParameter( "phoneIndex", phoneIndex )
  47. .getSingleResult();

VALUE

Refers to the collection value. Same as not specifying a qualifier. Useful to explicitly show intent. Valid for any type of collection-valued reference.

INDEX

According to HQL rules, this is valid for both Maps and Lists which specify a javax.persistence.OrderColumn annotation to refer to the Map key or the List position (aka the OrderColumn value). JPQL however, reserves this for use in the List case and adds KEY for the Map case. Applications interested in JPA provider portability should be aware of this distinction.

KEY

Valid only for Maps. Refers to the map’s key. If the key is itself an entity, it can be further navigated.

ENTRY

Only valid for Maps. Refers to the map’s logical java.util.Map.Entry tuple (the combination of its key and value). ENTRY is only valid as a terminal path and it’s applicable to the SELECT clause only.

See Collection-related expressions for additional details on collection-related expressions.

15.17. Polymorphism

HQL and JPQL queries are inherently polymorphic.

  1. List<Payment> payments = entityManager.createQuery(
  2. "select p " +
  3. "from Payment p ", Payment.class )
  4. .getResultList();

This query names the Payment entity explicitly. However, all subclasses of Payment are also available to the query. So, if the CreditCardPayment and WireTransferPayment entities extend the Payment class, all three types would be available to the entity query, and the query would return instances of all three.

This behavior can be altered in two ways:

  • by limiting the query to select only from the subclass entity.

  • by using either the org.hibernate.annotations.Polymorphism annotation (global, and Hibernate-specific). See the @Polymorphism section for more info about this use case.

The HQL query from java.lang.Object is totally valid (although not very practical from a performance perspective)!

It returns every object of every entity type defined by your application mappings.

15.18. Expressions

Essentially, expressions are references that resolve to basic or tuple values.

15.19. Identification variable

See The FROM clause.

15.20. Path expressions

Again, see The FROM clause.

15.21. Literals

String literals are enclosed in single quotes. To escape a single quote within a string literal, use double single quotes.

Example 521. String literals examples

  1. List<Person> persons = entityManager.createQuery(
  2. "select p " +
  3. "from Person p " +
  4. "where p.name like 'Joe'", Person.class)
  5. .getResultList();
  6. // Escaping quotes
  7. List<Person> persons = entityManager.createQuery(
  8. "select p " +
  9. "from Person p " +
  10. "where p.name like 'Joe''s'", Person.class)
  11. .getResultList();

Numeric literals are allowed in a few different forms.

Example 522. Numeric literal examples

  1. // simple integer literal
  2. Person person = entityManager.createQuery(
  3. "select p " +
  4. "from Person p " +
  5. "where p.id = 1", Person.class)
  6. .getSingleResult();
  7. // simple integer literal, typed as a long
  8. Person person = entityManager.createQuery(
  9. "select p " +
  10. "from Person p " +
  11. "where p.id = 1L", Person.class)
  12. .getSingleResult();
  13. // decimal notation
  14. List<Call> calls = entityManager.createQuery(
  15. "select c " +
  16. "from Call c " +
  17. "where c.duration > 100.5", Call.class )
  18. .getResultList();
  19. // decimal notation, typed as a float
  20. List<Call> calls = entityManager.createQuery(
  21. "select c " +
  22. "from Call c " +
  23. "where c.duration > 100.5F", Call.class )
  24. .getResultList();
  25. // scientific notation
  26. List<Call> calls = entityManager.createQuery(
  27. "select c " +
  28. "from Call c " +
  29. "where c.duration > 1e+2", Call.class )
  30. .getResultList();
  31. // scientific notation, typed as a float
  32. List<Call> calls = entityManager.createQuery(
  33. "select c " +
  34. "from Call c " +
  35. "where c.duration > 1e+2F", Call.class )
  36. .getResultList();

In the scientific notation form, the E is case-insensitive.

Specific typing can be achieved through the use of the same suffix approach specified by Java. So, L denotes a long, D denotes a double, F denotes a float. The actual suffix is case-insensitive.

The boolean literals are TRUE and FALSE, again case-insensitive.

Enums can even be referenced as literals. The fully-qualified enum class name must be used. HQL can also handle constants in the same manner, though JPQL does not define that as being supported.

Entity names can also be used as literal. See Entity type.

Date/time literals can be specified using the JDBC escape syntax:

  • {d ‘yyyy-mm-dd’} for dates

  • {t ‘hh:mm:ss’} for times

  • {ts ‘yyyy-mm-dd hh:mm:ss[.millis]’} (millis optional) for timestamps.

These Date/time literals only work if the underlying JDBC driver supports them.

15.22. Arithmetic

Arithmetic operations also represent valid expressions.

Example 523. Numeric arithmetic examples

  1. // select clause date/time arithmetic operations
  2. Long duration = entityManager.createQuery(
  3. "select sum(ch.duration) * :multiplier " +
  4. "from Person pr " +
  5. "join pr.phones ph " +
  6. "join ph.callHistory ch " +
  7. "where ph.id = 1L ", Long.class )
  8. .setParameter( "multiplier", 1000L )
  9. .getSingleResult();
  10. // select clause date/time arithmetic operations
  11. Integer years = entityManager.createQuery(
  12. "select year( current_date() ) - year( p.createdOn ) " +
  13. "from Person p " +
  14. "where p.id = 1L", Integer.class )
  15. .getSingleResult();
  16. // where clause arithmetic operations
  17. List<Person> persons = entityManager.createQuery(
  18. "select p " +
  19. "from Person p " +
  20. "where year( current_date() ) - year( p.createdOn ) > 1", Person.class )
  21. .getResultList();

The following rules apply to the result of arithmetic operations:

  • If either of the operands is Double/double, the result is a Double

  • else, if either of the operands is Float/float, the result is a Float

  • else, if either operand is BigDecimal, the result is BigDecimal

  • else, if either operand is BigInteger, the result is BigInteger (except for division, in which case the result type is not further defined)

  • else, if either operand is Long/long, the result is Long (except for division, in which case the result type is not further defined)

  • else, (the assumption being that both operands are of integral type) the result is Integer (except for division, in which case the result type is not further defined)

Date arithmetic is also supported, albeit in a more limited fashion. This is due to differences in database support and partly to the lack of support for INTERVAL definition in the query language itself.

15.23. Concatenation (operation)

HQL defines a concatenation operator in addition to supporting the concatenation (CONCAT) function. This is not defined by JPQL, so portable applications should avoid its use. The concatenation operator is taken from the SQL concatenation operator (e.g ||).

Example 524. Concatenation operation example

  1. String name = entityManager.createQuery(
  2. "select 'Customer ' || p.name " +
  3. "from Person p " +
  4. "where p.id = 1", String.class )
  5. .getSingleResult();

See Scalar functions for details on the concat() function.

15.24. Aggregate functions

Aggregate functions are also valid expressions in HQL and JPQL. The semantics is the same as their SQL counterpart. The supported aggregate functions are:

COUNT (including distinct/all qualifiers)

The result type is always Long.

AVG

The result type is always Double.

MIN

The result type is the same as the argument type.

MAX

The result type is the same as the argument type.

SUM

The result type of the SUM() function depends on the type of the values being summed. For integral values (other than BigInteger), the result type is Long.

For floating point values (other than BigDecimal) the result type is Double. For BigInteger values, the result type is BigInteger. For BigDecimal values, the result type is BigDecimal.

Example 525. Aggregate function examples

  1. Object[] callStatistics = entityManager.createQuery(
  2. "select " +
  3. " count(c), " +
  4. " sum(c.duration), " +
  5. " min(c.duration), " +
  6. " max(c.duration), " +
  7. " avg(c.duration) " +
  8. "from Call c ", Object[].class )
  9. .getSingleResult();
  10. Long phoneCount = entityManager.createQuery(
  11. "select count( distinct c.phone ) " +
  12. "from Call c ", Long.class )
  13. .getSingleResult();
  14. List<Object[]> callCount = entityManager.createQuery(
  15. "select p.number, count(c) " +
  16. "from Call c " +
  17. "join c.phone p " +
  18. "group by p.number", Object[].class )
  19. .getResultList();

Aggregations often appear with grouping. For information on grouping see Group by.

15.25. Scalar functions

Both HQL and JPQL define some standard functions that are available regardless of the underlying database in use. HQL can also understand additional functions defined by the Dialect as well as the application.

15.26. JPQL standardized functions

Here is the list of functions defined as supported by JPQL. Applications interested in remaining portable between JPA providers should stick to these functions.

CONCAT

String concatenation function. Variable argument length of 2 or more string values to be concatenated together.

  1. List<String> callHistory = entityManager.createQuery(
  2. "select concat( p.number, ' : ' , cast(c.duration as string) ) " +
  3. "from Call c " +
  4. "join c.phone p", String.class )
  5. .getResultList();

SUBSTRING

Extracts a portion of a string value. The second argument denotes the starting position, where 1 is the first character of the string. The third (optional) argument denotes the length.

  1. List<String> prefixes = entityManager.createQuery(
  2. "select substring( p.number, 1, 2 ) " +
  3. "from Call c " +
  4. "join c.phone p", String.class )
  5. .getResultList();

UPPER

Upper cases the specified string.

  1. List<String> names = entityManager.createQuery(
  2. "select upper( p.name ) " +
  3. "from Person p ", String.class )
  4. .getResultList();

LOWER

Lower cases the specified string.

  1. List<String> names = entityManager.createQuery(
  2. "select lower( p.name ) " +
  3. "from Person p ", String.class )
  4. .getResultList();

TRIM

Follows the semantics of the SQL trim function.

  1. List<String> names = entityManager.createQuery(
  2. "select trim( p.name ) " +
  3. "from Person p ", String.class )
  4. .getResultList();

LENGTH

Returns the length of a string.

  1. List<Integer> lengths = entityManager.createQuery(
  2. "select length( p.name ) " +
  3. "from Person p ", Integer.class )
  4. .getResultList();

LOCATE

Locates a string within another string. The third argument (optional) is used to denote a position from which to start looking.

  1. List<Integer> sizes = entityManager.createQuery(
  2. "select locate( 'John', p.name ) " +
  3. "from Person p ", Integer.class )
  4. .getResultList();

ABS

Calculates the mathematical absolute value of a numeric value.

  1. List<Integer> abs = entityManager.createQuery(
  2. "select abs( c.duration ) " +
  3. "from Call c ", Integer.class )
  4. .getResultList();

MOD

Calculates the remainder of dividing the first argument by the second.

  1. List<Integer> mods = entityManager.createQuery(
  2. "select mod( c.duration, 10 ) " +
  3. "from Call c ", Integer.class )
  4. .getResultList();

SQRT

Calculates the mathematical square root of a numeric value.

  1. List<Double> sqrts = entityManager.createQuery(
  2. "select sqrt( c.duration ) " +
  3. "from Call c ", Double.class )
  4. .getResultList();

CURRENT_DATE

Returns the database current date.

  1. List<Call> calls = entityManager.createQuery(
  2. "select c " +
  3. "from Call c " +
  4. "where c.timestamp = current_date", Call.class )
  5. .getResultList();

CURRENT_TIME

Returns the database current time.

  1. List<Call> calls = entityManager.createQuery(
  2. "select c " +
  3. "from Call c " +
  4. "where c.timestamp = current_time", Call.class )
  5. .getResultList();

CURRENT_TIMESTAMP

Returns the database current timestamp.

  1. List<Call> calls = entityManager.createQuery(
  2. "select c " +
  3. "from Call c " +
  4. "where c.timestamp = current_timestamp", Call.class )
  5. .getResultList();

15.27. HQL functions

Beyond the JPQL standardized functions, HQL makes some additional functions available regardless of the underlying database in use.

BIT_LENGTH

Returns the length of binary data.

  1. List<Number> bits = entityManager.createQuery(
  2. "select bit_length( c.duration ) " +
  3. "from Call c ", Number.class )
  4. .getResultList();

CAST

Performs a SQL cast. The cast target should name the Hibernate mapping type to use. See the data types chapter on for more information.

  1. List<String> durations = entityManager.createQuery(
  2. "select cast( c.duration as string ) " +
  3. "from Call c ", String.class )
  4. .getResultList();

EXTRACT

Performs a SQL extraction on datetime values. An extraction extracts parts of the datetime (the year, for example).

  1. List<Integer> years = entityManager.createQuery(
  2. "select extract( YEAR from c.timestamp ) " +
  3. "from Call c ", Integer.class )
  4. .getResultList();

See the abbreviated forms below.

YEAR

Abbreviated extract form for extracting the year.

  1. List<Integer> years = entityManager.createQuery(
  2. "select year( c.timestamp ) " +
  3. "from Call c ", Integer.class )
  4. .getResultList();

MONTH

Abbreviated extract form for extracting the month.

DAY

Abbreviated extract form for extracting the day.

HOUR

Abbreviated extract form for extracting the hour.

MINUTE

Abbreviated extract form for extracting the minute.

SECOND

Abbreviated extract form for extracting the second.

STR

Abbreviated form for casting a value as character data.

  1. List<String> timestamps = entityManager.createQuery(
  2. "select str( c.timestamp ) " +
  3. "from Call c ", String.class )
  4. .getResultList();
  5. List<String> timestamps = entityManager.createQuery(
  6. "select str( cast(duration as float) / 60, 4, 2 ) " +
  7. "from Call c ", String.class )
  8. .getResultList();

15.28. User-defined functions

Hibernate Dialects can register additional functions known to be available for that particular database product. These functions are also available in HQL (and JPQL, though only when using Hibernate as the JPA provider, obviously). However, they would only be available when using that database Dialect. Applications that aim for database portability should avoid using functions in this category.

Application developers can also supply their own set of functions. This would usually represent either user-defined SQL functions or aliases for snippets of SQL. Such function declarations are made by using the addSqlFunction() method of the org.hibernate.boot.MetadataBuilder or the legacy org.hibernate.cfg.Configuration.

Now, let’s assume we have the following apply_vat PostgreSQL user-defined function:

Example 526. PostgreSQL user-defined function

  1. statement.executeUpdate(
  2. "CREATE OR REPLACE FUNCTION apply_vat(integer) RETURNS integer " +
  3. " AS 'select cast(($1 * 1.2) as integer);' " +
  4. " LANGUAGE SQL " +
  5. " IMMUTABLE " +
  6. " RETURNS NULL ON NULL INPUT;"
  7. );

Let’s consider we have persisted the following entity in our database:

Example 527. Book entity

  1. Book book = new Book();
  2. book.setIsbn( "978-9730228236" );
  3. book.setTitle( "High-Performance Java Persistence" );
  4. book.setAuthor( "Vlad Mihalcea" );
  5. book.setPriceCents( 4500 );
  6. entityManager.persist( book );

15.28.1. User-defined functions referenced in the WHERE clause

By default, Hibernate can pass through any user-defined function that’s being used in the WHERE clause of a JPQL/HQL entity query.

Example 528. User-defined function passing through the WHERE clause

  1. List<Book> books = entityManager.createQuery(
  2. "select b " +
  3. "from Book b " +
  4. "where apply_vat(b.priceCents) = :price ", Book.class )
  5. .setParameter( "price", 5400 )
  6. .getResultList();
  7. assertTrue( books
  8. .stream()
  9. .filter( book -> "High-Performance Java Persistence".equals( book.getTitle() ) )
  10. .findAny()
  11. .isPresent()
  12. );

While this works just fine with Hibernate, it might be a problem with other JPA providers. For this purpose, JPA offers the function JPQL keyword which works as follows.

Example 529. Using the JPQL function keyword

  1. List<Book> books = entityManager.createQuery(
  2. "select b " +
  3. "from Book b " +
  4. "where function('apply_vat', b.priceCents) = :price ", Book.class )
  5. .setParameter( "price", 5400 )
  6. .getResultList();
  7. assertTrue( books
  8. .stream()
  9. .filter( book -> "High-Performance Java Persistence".equals( book.getTitle() ) )
  10. .findAny()
  11. .isPresent()
  12. );

15.28.2. User-defined functions referenced in the SELECT clause

When the user-defined function is referenced in the SELECT clause of a JPQL/HQL entity query, Hibernate can no longer pass it through unless the function is registered.

Example 530. Registering a user-defined function using the MetadataBuilderContributor

  1. settings.put( "hibernate.metadata_builder_contributor",
  2. (MetadataBuilderContributor) metadataBuilder ->
  3. metadataBuilder.applySqlFunction(
  4. "apply_vat",
  5. new StandardSQLFunction(
  6. "apply_vat",
  7. StandardBasicTypes.INTEGER
  8. )
  9. )
  10. );

Now that that apply_vat is registered, we can reference it in the JPQL SELECT clause.

Example 531. User-defined function in the SELECT clause

  1. List<Tuple> books = entityManager.createQuery(
  2. "select b.title as title, apply_vat(b.priceCents) as price " +
  3. "from Book b " +
  4. "where b.author = :author ", Tuple.class )
  5. .setParameter( "author", "Vlad Mihalcea" )
  6. .getResultList();
  7. assertEquals( 1, books.size() );
  8. Tuple book = books.get( 0 );
  9. assertEquals( "High-Performance Java Persistence", book.get( "title" ) );
  10. assertEquals( 5400, ((Number) book.get( "price" )).intValue() );

15.29. Collection-related expressions

There are a few specialized expressions for working with collection-valued associations. Generally, these are just abbreviated forms or other expressions for the sake of conciseness.

SIZE

Calculate the size of a collection. Equates to a subquery!

MAXELEMENT

Available for use on collections of basic type. Refers to the maximum value as determined by applying the max SQL aggregation.

MAXINDEX

Available for use on indexed collections. Refers to the maximum index (key/position) as determined by applying the max SQL aggregation.

MINELEMENT

Available for use on collections of basic type. Refers to the minimum value as determined by applying the min SQL aggregation.

MININDEX

Available for use on indexed collections. Refers to the minimum index (key/position) as determined by applying the min SQL aggregation.

ELEMENTS

Used to refer to the elements of a collection as a whole. Only allowed in the where clause. Often used in conjunction with ALL, ANY or SOME restrictions.

INDICES

Similar to elements except that the indices expression refers to the collections indices (keys/positions) as a whole.

Example 532. Collection-related expressions examples

  1. List<Phone> phones = entityManager.createQuery(
  2. "select p " +
  3. "from Phone p " +
  4. "where maxelement( p.calls ) = :call", Phone.class )
  5. .setParameter( "call", call )
  6. .getResultList();
  7. List<Phone> phones = entityManager.createQuery(
  8. "select p " +
  9. "from Phone p " +
  10. "where minelement( p.calls ) = :call", Phone.class )
  11. .setParameter( "call", call )
  12. .getResultList();
  13. List<Person> persons = entityManager.createQuery(
  14. "select p " +
  15. "from Person p " +
  16. "where maxindex( p.phones ) = 0", Person.class )
  17. .getResultList();
  18. // the above query can be re-written with member of
  19. List<Person> persons = entityManager.createQuery(
  20. "select p " +
  21. "from Person p " +
  22. "where :phone member of p.phones", Person.class )
  23. .setParameter( "phone", phone )
  24. .getResultList();
  25. List<Person> persons = entityManager.createQuery(
  26. "select p " +
  27. "from Person p " +
  28. "where :phone = some elements ( p.phones )", Person.class )
  29. .setParameter( "phone", phone )
  30. .getResultList();
  31. List<Person> persons = entityManager.createQuery(
  32. "select p " +
  33. "from Person p " +
  34. "where exists elements ( p.phones )", Person.class )
  35. .getResultList();
  36. List<Phone> phones = entityManager.createQuery(
  37. "select p " +
  38. "from Phone p " +
  39. "where current_date() > key( p.callHistory )", Phone.class )
  40. .getResultList();
  41. List<Phone> phones = entityManager.createQuery(
  42. "select p " +
  43. "from Phone p " +
  44. "where current_date() > all elements( p.repairTimestamps )", Phone.class )
  45. .getResultList();
  46. List<Person> persons = entityManager.createQuery(
  47. "select p " +
  48. "from Person p " +
  49. "where 1 in indices( p.phones )", Person.class )
  50. .getResultList();
  51. List<Person> persons = entityManager.createQuery(
  52. "select p " +
  53. "from Person p " +
  54. "where size( p.phones ) = 2", Person.class )
  55. .getResultList();

Elements of indexed collections (arrays, lists, and maps) can be referred to by index operator.

Example 533. Index operator examples

  1. // indexed lists
  2. List<Person> persons = entityManager.createQuery(
  3. "select p " +
  4. "from Person p " +
  5. "where p.phones[ 0 ].type = 'LAND_LINE'", Person.class )
  6. .getResultList();
  7. // maps
  8. List<Person> persons = entityManager.createQuery(
  9. "select p " +
  10. "from Person p " +
  11. "where p.addresses[ 'HOME' ] = :address", Person.class )
  12. .setParameter( "address", address)
  13. .getResultList();
  14. //max index in list
  15. List<Person> persons = entityManager.createQuery(
  16. "select pr " +
  17. "from Person pr " +
  18. "where pr.phones[ maxindex(pr.phones) ].type = 'LAND_LINE'", Person.class )
  19. .getResultList();

See also Special case - qualified path expressions as there is a good deal of overlap.

15.30. Entity type

We can also refer to the type of an entity as an expression. This is mainly useful when dealing with entity inheritance hierarchies. The type can be expressed using a TYPE function used to refer to the type of an identification variable representing an entity. The name of the entity also serves as a way to refer to an entity type. Additionally, the entity type can be parameterized, in which case the entity’s Java Class reference would be bound as the parameter value.

Example 534. Entity type expression examples

  1. List<Payment> payments = entityManager.createQuery(
  2. "select p " +
  3. "from Payment p " +
  4. "where type(p) = CreditCardPayment", Payment.class )
  5. .getResultList();
  6. List<Payment> payments = entityManager.createQuery(
  7. "select p " +
  8. "from Payment p " +
  9. "where type(p) = :type", Payment.class )
  10. .setParameter( "type", WireTransferPayment.class)
  11. .getResultList();

HQL also has a legacy form of referring to an entity type using the class keyword, though that legacy form is considered deprecated in favor of TYPE.

The legacy form would have used p.class in the examples rather than type(p). It is mentioned only for completeness.

15.31. CASE expressions

Both the simple and searched forms are supported, as well as the two SQL defined abbreviated forms (NULLIF and COALESCE)

15.32. Simple CASE expressions

The simple form has the following syntax:

  1. CASE {operand} WHEN {test_value} THEN {match_result} ELSE {miss_result} END

Example 535. Simple case expression example

  1. List<String> nickNames = entityManager.createQuery(
  2. "select " +
  3. " case p.nickName " +
  4. " when 'NA' " +
  5. " then '<no nick name>' " +
  6. " else p.nickName " +
  7. " end " +
  8. "from Person p", String.class )
  9. .getResultList();
  10. // same as above
  11. List<String> nickNames = entityManager.createQuery(
  12. "select coalesce(p.nickName, '<no nick name>') " +
  13. "from Person p", String.class )
  14. .getResultList();

15.33. Searched CASE expressions

The searched form has the following syntax:

  1. CASE [ WHEN {test_conditional} THEN {match_result} ]* ELSE {miss_result} END

Example 536. Searched case expression example

  1. List<String> nickNames = entityManager.createQuery(
  2. "select " +
  3. " case " +
  4. " when p.nickName is null " +
  5. " then " +
  6. " case " +
  7. " when p.name is null " +
  8. " then '<no nick name>' " +
  9. " else p.name " +
  10. " end" +
  11. " else p.nickName " +
  12. " end " +
  13. "from Person p", String.class )
  14. .getResultList();
  15. // coalesce can handle this more succinctly
  16. List<String> nickNames = entityManager.createQuery(
  17. "select coalesce( p.nickName, p.name, '<no nick name>' ) " +
  18. "from Person p", String.class )
  19. .getResultList();

15.34. CASE expressions with arithmetic operations

If you want to use arithmetic operations in the CASE expressions, you need to wrap the arithmetic operation in parentheses as illustrated by the following example:

Example 537. Case expression with arithmetic operation example

  1. List<Long> values = entityManager.createQuery(
  2. "select " +
  3. " case when p.nickName is null " +
  4. " then (p.id * 1000) " +
  5. " else p.id " +
  6. " end " +
  7. "from Person p " +
  8. "order by p.id", Long.class)
  9. .getResultList();
  10. assertEquals(3, values.size());
  11. assertEquals( 1L, (long) values.get( 0 ) );
  12. assertEquals( 2000, (long) values.get( 1 ) );
  13. assertEquals( 3000, (long) values.get( 2 ) );

Without wrapping the arithmetic expression in ( and ), the entity query parser will not be able to parse the arithmetic operators.

15.35. NULLIF expressions

NULLIF is an abbreviated CASE expression that returns NULL if its operands are considered equal.

Example 538. NULLIF example

  1. List<String> nickNames = entityManager.createQuery(
  2. "select nullif( p.nickName, p.name ) " +
  3. "from Person p", String.class )
  4. .getResultList();
  5. // equivalent CASE expression
  6. List<String> nickNames = entityManager.createQuery(
  7. "select " +
  8. " case" +
  9. " when p.nickName = p.name" +
  10. " then null" +
  11. " else p.nickName" +
  12. " end " +
  13. "from Person p", String.class )
  14. .getResultList();

15.36. COALESCE expressions

COALESCE is an abbreviated CASE expression that returns the first non-null operand. We have seen a number of COALESCE examples above.

15.37. The SELECT clause

The SELECT clause identifies which objects and values to return as the query results. The expressions discussed in Expressions are all valid select expressions, except where otherwise noted. See the section Hibernate Query API for information on handling the results depending on the types of values specified in the SELECT clause.

There is a particular expression type that is only valid in the select clause. Hibernate calls this “dynamic instantiation”. JPQL supports some of that feature and calls it a “constructor expression”.

So rather than dealing with the Object[] (again, see Hibernate Query API) here, we are wrapping the values in a type-safe Java object that will be returned as the results of the query.

Example 539. Dynamic HQL and JPQL instantiation example

  1. public class CallStatistics {
  2. private final long count;
  3. private final long total;
  4. private final int min;
  5. private final int max;
  6. private final double avg;
  7. public CallStatistics(long count, long total, int min, int max, double avg) {
  8. this.count = count;
  9. this.total = total;
  10. this.min = min;
  11. this.max = max;
  12. this.avg = avg;
  13. }
  14. //Getters and setters omitted for brevity
  15. }
  16. CallStatistics callStatistics = entityManager.createQuery(
  17. "select new org.hibernate.userguide.hql.CallStatistics(" +
  18. " count(c), " +
  19. " sum(c.duration), " +
  20. " min(c.duration), " +
  21. " max(c.duration), " +
  22. " avg(c.duration)" +
  23. ") " +
  24. "from Call c ", CallStatistics.class )
  25. .getSingleResult();

The projection class must be fully qualified in the entity query, and it must define a matching constructor.

The class here need not be mapped. It can be a DTO class.

If it does represent an entity, the resulting instances are returned in the NEW state (not managed!).

HQL supports additional “dynamic instantiation” features. First, the query can specify to return a List rather than an Object[] for scalar results:

Example 540. Dynamic instantiation example - list

  1. List<List> phoneCallDurations = entityManager.createQuery(
  2. "select new list(" +
  3. " p.number, " +
  4. " c.duration " +
  5. ") " +
  6. "from Call c " +
  7. "join c.phone p ", List.class )
  8. .getResultList();

The results from this query will be a List<List> as opposed to a List<Object[]>

HQL also supports wrapping the scalar results in a Map.

Example 541. Dynamic instantiation example - map

  1. List<Map> phoneCallTotalDurations = entityManager.createQuery(
  2. "select new map(" +
  3. " p.number as phoneNumber , " +
  4. " sum(c.duration) as totalDuration, " +
  5. " avg(c.duration) as averageDuration " +
  6. ") " +
  7. "from Call c " +
  8. "join c.phone p " +
  9. "group by p.number ", Map.class )
  10. .getResultList();

The results from this query will be a List<Map<String, Object>> as opposed to a List<Object[]>. The keys of the map are defined by the aliases given to the select expressions. If the user doesn’t assign aliases, the key will be the index of each particular result set column (e.g. 0, 1, 2, etc).

15.38. Predicates

Predicates form the basis of the where clause, the having clause and searched case expressions. They are expressions which resolve to a truth value, generally TRUE or FALSE, although boolean comparisons involving NULL resolve typically to UNKNOWN.

15.39. Relational comparisons

Comparisons involve one of the comparison operators: =, >, >=, <, <=, <>. HQL also defines != as a comparison operator synonymous with <>. The operands should be of the same type.

Example 542. Relational comparison examples

  1. // numeric comparison
  2. List<Call> calls = entityManager.createQuery(
  3. "select c " +
  4. "from Call c " +
  5. "where c.duration < 30 ", Call.class )
  6. .getResultList();
  7. // string comparison
  8. List<Person> persons = entityManager.createQuery(
  9. "select p " +
  10. "from Person p " +
  11. "where p.name like 'John%' ", Person.class )
  12. .getResultList();
  13. // datetime comparison
  14. List<Person> persons = entityManager.createQuery(
  15. "select p " +
  16. "from Person p " +
  17. "where p.createdOn > '1950-01-01' ", Person.class )
  18. .getResultList();
  19. // enum comparison
  20. List<Phone> phones = entityManager.createQuery(
  21. "select p " +
  22. "from Phone p " +
  23. "where p.type = 'MOBILE' ", Phone.class )
  24. .getResultList();
  25. // boolean comparison
  26. List<Payment> payments = entityManager.createQuery(
  27. "select p " +
  28. "from Payment p " +
  29. "where p.completed = true ", Payment.class )
  30. .getResultList();
  31. // boolean comparison
  32. List<Payment> payments = entityManager.createQuery(
  33. "select p " +
  34. "from Payment p " +
  35. "where type(p) = WireTransferPayment ", Payment.class )
  36. .getResultList();
  37. // entity value comparison
  38. List<Object[]> phonePayments = entityManager.createQuery(
  39. "select p " +
  40. "from Payment p, Phone ph " +
  41. "where p.person = ph.person ", Object[].class )
  42. .getResultList();

Comparisons can also involve subquery qualifiers: ALL, ANY, SOME. SOME and ANY are synonymous.

The ALL qualifier resolves to true if the comparison is true for all of the values in the result of the subquery. It resolves to false if the subquery result is empty.

Example 543. ALL subquery comparison qualifier example

  1. // select all persons with all calls shorter than 50 seconds
  2. List<Person> persons = entityManager.createQuery(
  3. "select distinct p.person " +
  4. "from Phone p " +
  5. "join p.calls c " +
  6. "where 50 > all ( " +
  7. " select duration" +
  8. " from Call" +
  9. " where phone = p " +
  10. ") ", Person.class )
  11. .getResultList();

The ANY/SOME qualifier resolves to true if the comparison is true for some of (at least one of) the values in the result of the subquery. It resolves to false if the subquery result is empty.

15.40. Nullness predicate

It check a value for nullness. It can be applied to basic attribute references, entity references, and parameters. HQL additionally allows it to be applied to component/embeddable types.

Example 544. Nullness checking examples

  1. // select all persons with a nickname
  2. List<Person> persons = entityManager.createQuery(
  3. "select p " +
  4. "from Person p " +
  5. "where p.nickName is not null", Person.class )
  6. .getResultList();
  7. // select all persons without a nickname
  8. List<Person> persons = entityManager.createQuery(
  9. "select p " +
  10. "from Person p " +
  11. "where p.nickName is null", Person.class )
  12. .getResultList();

15.41. Like predicate

Performs a like comparison on string values. The syntax is:

  1. like_expression ::=
  2. string_expression
  3. [NOT] LIKE pattern_value
  4. [ESCAPE escape_character]

The semantics follow that of the SQL like expression. The pattern_value is the pattern to attempt to match in the string_expression. Just like SQL, pattern_value can use _ and % as wildcards. The meanings are the same. The _ symbol matches any single character and % matches any number of characters.

Example 545. Like predicate examples

  1. List<Person> persons = entityManager.createQuery(
  2. "select p " +
  3. "from Person p " +
  4. "where p.name like 'Jo%'", Person.class )
  5. .getResultList();
  6. List<Person> persons = entityManager.createQuery(
  7. "select p " +
  8. "from Person p " +
  9. "where p.name not like 'Jo%'", Person.class )
  10. .getResultList();

The optional escape 'escape character' is used to specify an escape character used to escape the special meaning of _ and % in the pattern_value. This is useful when needing to search on patterns including either _ or %.

The syntax is formed as follows: 'like_predicate' escape 'escape_symbol' So, if | is the escape symbol and we want to match all stored procedures prefixed with Dr_, the like criteria becomes: 'Dr|_%' escape '|':

Example 546. Like with escape symbol

  1. // find any person with a name starting with "Dr_"
  2. List<Person> persons = entityManager.createQuery(
  3. "select p " +
  4. "from Person p " +
  5. "where p.name like 'Dr|_%' escape '|'", Person.class )
  6. .getResultList();

15.42. Between predicate

Analogous to the SQL BETWEEN expression, it checks if the value is within boundaries. All the operands should have comparable types.

Example 547. Between predicate examples

  1. List<Person> persons = entityManager.createQuery(
  2. "select p " +
  3. "from Person p " +
  4. "join p.phones ph " +
  5. "where p.id = 1L and index(ph) between 0 and 3", Person.class )
  6. .getResultList();
  7. List<Person> persons = entityManager.createQuery(
  8. "select p " +
  9. "from Person p " +
  10. "where p.createdOn between '1999-01-01' and '2001-01-02'", Person.class )
  11. .getResultList();
  12. List<Call> calls = entityManager.createQuery(
  13. "select c " +
  14. "from Call c " +
  15. "where c.duration between 5 and 20", Call.class )
  16. .getResultList();
  17. List<Person> persons = entityManager.createQuery(
  18. "select p " +
  19. "from Person p " +
  20. "where p.name between 'H' and 'M'", Person.class )
  21. .getResultList();

15.43. In predicate

IN predicates performs a check that a particular value is in a list of values. Its syntax is:

  1. in_expression ::=
  2. single_valued_expression [NOT] IN single_valued_list
  3. single_valued_list ::=
  4. constructor_expression | (subquery) | collection_valued_input_parameter
  5. constructor_expression ::= (expression[, expression]*)

The types of the single_valued_expression and the individual values in the single_valued_list must be consistent.

JPQL limits the valid types here to string, numeric, date, time, timestamp, and enum types, and, in JPQL, single_valued_expression can only refer to:

  • “state fields”, which is its term for simple attributes. Specifically, this excludes association and component/embedded attributes.

  • entity type expressions. See Entity type.

In HQL, single_valued_expression can refer to a far more broad set of expression types. Single-valued association are allowed, and so are component/embedded attributes, although that feature depends on the level of support for tuple or “row value constructor syntax” in the underlying database. Additionally, HQL does not limit the value type in any way, though application developers should be aware that different types may incur limited support based on the underlying database vendor. This is largely the reason for the JPQL limitations.

The list of values can come from a number of different sources. In the constructor_expression and collection_valued_input_parameter, the list of values must not be empty; it must contain at least one value.

Example 548. In predicate examples

  1. List<Payment> payments = entityManager.createQuery(
  2. "select p " +
  3. "from Payment p " +
  4. "where type(p) in ( CreditCardPayment, WireTransferPayment )", Payment.class )
  5. .getResultList();
  6. List<Phone> phones = entityManager.createQuery(
  7. "select p " +
  8. "from Phone p " +
  9. "where type in ( 'MOBILE', 'LAND_LINE' )", Phone.class )
  10. .getResultList();
  11. List<Phone> phones = entityManager.createQuery(
  12. "select p " +
  13. "from Phone p " +
  14. "where type in :types", Phone.class )
  15. .setParameter( "types", Arrays.asList( PhoneType.MOBILE, PhoneType.LAND_LINE ) )
  16. .getResultList();
  17. List<Phone> phones = entityManager.createQuery(
  18. "select distinct p " +
  19. "from Phone p " +
  20. "where p.person.id in (" +
  21. " select py.person.id " +
  22. " from Payment py" +
  23. " where py.completed = true and py.amount > 50 " +
  24. ")", Phone.class )
  25. .getResultList();
  26. // Not JPQL compliant!
  27. List<Phone> phones = entityManager.createQuery(
  28. "select distinct p " +
  29. "from Phone p " +
  30. "where p.person in (" +
  31. " select py.person " +
  32. " from Payment py" +
  33. " where py.completed = true and py.amount > 50 " +
  34. ")", Phone.class )
  35. .getResultList();
  36. // Not JPQL compliant!
  37. List<Payment> payments = entityManager.createQuery(
  38. "select distinct p " +
  39. "from Payment p " +
  40. "where ( p.amount, p.completed ) in (" +
  41. " (50, true )," +
  42. " (100, true )," +
  43. " (5, false )" +
  44. ")", Payment.class )
  45. .getResultList();

15.44. Exists predicate

Exists expressions test the existence of results from a subquery. The affirmative form returns true if the subquery result contains values. The negated form returns true if the subquery result is empty.

15.45. Empty collection predicate

The IS [NOT] EMPTY expression applies to collection-valued path expressions. It checks whether the particular collection has any associated values.

Example 549. Empty collection expression examples

  1. List<Person> persons = entityManager.createQuery(
  2. "select p " +
  3. "from Person p " +
  4. "where p.phones is empty", Person.class )
  5. .getResultList();
  6. List<Person> persons = entityManager.createQuery(
  7. "select p " +
  8. "from Person p " +
  9. "where p.phones is not empty", Person.class )
  10. .getResultList();

15.46. Member-of collection predicate

The [NOT] MEMBER [OF] expression applies to collection-valued path expressions. It checks whether a value is a member of the specified collection.

Example 550. Member-of collection expression examples

  1. List<Person> persons = entityManager.createQuery(
  2. "select p " +
  3. "from Person p " +
  4. "where 'Home address' member of p.addresses", Person.class )
  5. .getResultList();
  6. List<Person> persons = entityManager.createQuery(
  7. "select p " +
  8. "from Person p " +
  9. "where 'Home address' not member of p.addresses", Person.class )
  10. .getResultList();

15.47. NOT predicate operator

The NOT operator is used to negate the predicate that follows it. If that following predicate is true, the NOT resolves to false.

If the predicate is true, NOT resolves to false. If the predicate is unknown (e.g. NULL), then NOT resolves to unknown as well.

15.48. AND predicate operator

The AND operator is used to combine 2 predicate expressions. The result of the AND expression is true if and only if both predicates resolve to true. If either predicate resolves to unknown, the AND expression resolves to unknown as well. Otherwise, the result is false.

15.49. OR predicate operator

The OR operator is used to combine 2 predicate expressions. The result of the OR expression is true if one predicate resolves to true. If both predicates resolve to unknown, the OR expression resolves to unknown. Otherwise, the result is false.

15.50. The WHERE clause

The WHERE clause of a query is made up of predicates which assert whether values in each potential row match the current filtering criteria. Thus, the where clause restricts the results returned from a select query and limits the scope of update and delete queries.

15.51. Group by

The GROUP BY clause allows building aggregated results for various value groups. As an example, consider the following queries:

Example 551. Group by example

  1. Long totalDuration = entityManager.createQuery(
  2. "select sum( c.duration ) " +
  3. "from Call c ", Long.class )
  4. .getSingleResult();
  5. List<Object[]> personTotalCallDurations = entityManager.createQuery(
  6. "select p.name, sum( c.duration ) " +
  7. "from Call c " +
  8. "join c.phone ph " +
  9. "join ph.person p " +
  10. "group by p.name", Object[].class )
  11. .getResultList();
  12. //It's even possible to group by entities!
  13. List<Object[]> personTotalCallDurations = entityManager.createQuery(
  14. "select p, sum( c.duration ) " +
  15. "from Call c " +
  16. "join c.phone ph " +
  17. "join ph.person p " +
  18. "group by p", Object[].class )
  19. .getResultList();

The first query retrieves the complete total of all orders. The second retrieves the total for each customer, grouped by each customer.

In a grouped query, the where clause applies to the non-aggregated values (essentially it determines whether rows will make it into the aggregation). The HAVING clause also restricts results, but it operates on the aggregated values. In the Group by example, we retrieved Call duration totals for all persons. If that ended up being too much data to deal with, we might want to restrict the results to focus only on customers with a summed total of more than 1000:

Example 552. Having example

  1. List<Object[]> personTotalCallDurations = entityManager.createQuery(
  2. "select p.name, sum( c.duration ) " +
  3. "from Call c " +
  4. "join c.phone ph " +
  5. "join ph.person p " +
  6. "group by p.name " +
  7. "having sum( c.duration ) > 1000", Object[].class )
  8. .getResultList();

The HAVING clause follows the same rules as the WHERE clause and is also made up of predicates. HAVING is applied after the groupings and aggregations have been done, while the WHERE clause is applied before.

15.52. Order by

The results of the query can also be ordered. The ORDER BY clause is used to specify the selected values to be used to order the result. The types of expressions considered valid as part of the ORDER BY clause include:

  • state fields

  • component/embeddable attributes

  • scalar expressions such as arithmetic operations, functions, etc.

  • identification variable declared in the select clause for any of the previous expression types

Additionally, JPQL says that all values referenced in the ORDER BY clause must be named in the SELECT clause. HQL does not mandate that restriction, but applications desiring database portability should be aware that not all databases support referencing values in the ORDER BY clause that are not referenced in the select clause.

Individual expressions in the order-by can be qualified with either ASC (ascending) or DESC (descending) to indicate the desired ordering direction. Null values can be placed in front or at the end of the sorted set using NULLS FIRST or NULLS LAST clause respectively.

Example 553. Order by example

  1. List<Person> persons = entityManager.createQuery(
  2. "select p " +
  3. "from Person p " +
  4. "order by p.name", Person.class )
  5. .getResultList();
  6. List<Object[]> personTotalCallDurations = entityManager.createQuery(
  7. "select p.name, sum( c.duration ) as total " +
  8. "from Call c " +
  9. "join c.phone ph " +
  10. "join ph.person p " +
  11. "group by p.name " +
  12. "order by total", Object[].class )
  13. .getResultList();

15.53. Read-only entities

As explained in entity immutability section, fetching entities in read-only mode is much more efficient than fetching read-write entities. Even if the entities are mutable, you can still fetch them in read-only mode, and benefit from reducing the memory footprint and speeding up the flushing process.

Read-only entities are skipped by the dirty checking mechanism as illustrated by the following example:

Example 554. Read-only entities query example

  1. List<Call> calls = entityManager.createQuery(
  2. "select c " +
  3. "from Call c " +
  4. "join c.phone p " +
  5. "where p.number = :phoneNumber ", Call.class )
  6. .setParameter( "phoneNumber", "123-456-7890" )
  7. .setHint( "org.hibernate.readOnly", true )
  8. .getResultList();
  9. calls.forEach( c -> c.setDuration( 0 ) );
  1. SELECT c.id AS id1_5_ ,
  2. c.duration AS duration2_5_ ,
  3. c.phone_id AS phone_id4_5_ ,
  4. c.call_timestamp AS call_tim3_5_
  5. FROM phone_call c
  6. INNER JOIN phone p ON c.phone_id = p.id
  7. WHERE p.phone_number = '123-456-7890'

As you can see, there is no SQL UPDATE being executed.

You can also pass the read-only hint to named queries using the JPA @QueryHint annotation.

Example 555. Fetching read-only entities using a named query and the read-only hint

  1. @NamedQuery(
  2. name = "get_read_only_person_by_name",
  3. query = "select p from Person p where name = :name",
  4. hints = {
  5. @QueryHint(
  6. name = "org.hibernate.readOnly",
  7. value = "true"
  8. )
  9. }
  10. )

The Hibernate native API offers a Query#setReadOnly method, as an alternative to using a JPA query hint:

Example 556. Read-only entities native query example

  1. List<Call> calls = entityManager.createQuery(
  2. "select c " +
  3. "from Call c " +
  4. "join c.phone p " +
  5. "where p.number = :phoneNumber ", Call.class )
  6. .setParameter( "phoneNumber", "123-456-7890" )
  7. .unwrap( org.hibernate.query.Query.class )
  8. .setReadOnly( true )
  9. .getResultList();

15.54. Entity query plan cache

Any entity query, be it JPQL or Criteria API, has to be parsed into an AST (Abstract Syntax Tree) so that Hibernate can generate the proper SQL statement. The entity query compilation takes time, and for this reason, Hibernate offers a query plan cache.

When executing an entity query, Hibernate first checks the plan cache, and only if there’s no plan available, a new one will be computed right away.

The query plan cache can be configured via the following configuration properties:

hibernate.query.plan_cache_max_size

This setting gives the maximum number of entries of the plan cache. The default value is 2048.

hibernate.query.plan_parameter_metadata_max_size

The setting gives the maximum number of ParameterMetadataImpl instances maintained by the query plan cache. The ParameterMetadataImpl object encapsulates metadata about parameters encountered within a query. The default value is 128.

Now, if you have many JPQL or Criteria API queries, it’s a good idea to increase the query plan cache size so that the vast majority of executing entity queries can skip the compilation phase, therefore reducing execution time.

To get a better understanding of the query plan cache effectiveness, Hibernate offers several statistics you can use. For more details, check out the Query plan cache statistics section.