2. Domain Model

The term domain model comes from the realm of data modeling. It is the model that ultimately describes the problem domain you are working in. Sometimes you will also hear the term persistent classes.

Ultimately the application domain model is the central character in an ORM. They make up the classes you wish to map. Hibernate works best if these classes follow the Plain Old Java Object (POJO) / JavaBean programming model. However, none of these rules are hard requirements. Indeed, Hibernate assumes very little about the nature of your persistent objects. You can express a domain model in other ways (using trees of java.util.Map instances, for example).

Historically applications using Hibernate would have used its proprietary XML mapping file format for this purpose. With the coming of JPA, most of this information is now defined in a way that is portable across ORM/JPA providers using annotations (and/or standardized XML format). This chapter will focus on JPA mapping where possible. For Hibernate mapping features not supported by JPA we will prefer Hibernate extension annotations.

2.1. Mapping types

Hibernate understands both the Java and JDBC representations of application data. The ability to read/write this data from/to the database is the function of a Hibernate type. A type, in this usage, is an implementation of the org.hibernate.type.Type interface. This Hibernate type also describes various behavioral aspects of the Java type such as how to check for equality, how to clone values, etc.

Usage of the word type

The Hibernate type is neither a Java type nor a SQL data type. It provides information about mapping a Java type to an SQL type as well as how to persist and fetch a given Java type to and from a relational database.

When you encounter the term type in discussions of Hibernate, it may refer to the Java type, the JDBC type, or the Hibernate type, depending on the context.

To help understand the type categorizations, let’s look at a simple table and domain model that we wish to map.

Example 1. A simple table and domain model

  1. create table Contact (
  2. id integer not null,
  3. first varchar(255),
  4. last varchar(255),
  5. middle varchar(255),
  6. notes varchar(255),
  7. starred boolean not null,
  8. website varchar(255),
  9. primary key (id)
  10. )
  1. @Entity(name = "Contact")
  2. public static class Contact {
  3. @Id
  4. private Integer id;
  5. private Name name;
  6. private String notes;
  7. private URL website;
  8. private boolean starred;
  9. //Getters and setters are omitted for brevity
  10. }
  11. @Embeddable
  12. public class Name {
  13. private String first;
  14. private String middle;
  15. private String last;
  16. // getters and setters omitted
  17. }

In the broadest sense, Hibernate categorizes types into two groups:

2.1.1. Value types

A value type is a piece of data that does not define its own lifecycle. It is, in effect, owned by an entity, which defines its lifecycle.

Looked at another way, all the state of an entity is made up entirely of value types. These state fields or JavaBean properties are termed persistent attributes. The persistent attributes of the Contact class are value types.

Value types are further classified into three sub-categories:

Basic types

in mapping the Contact table, all attributes except for name would be basic types. Basic types are discussed in detail in Basic types

Embeddable types

the name attribute is an example of an embeddable type, which is discussed in details in Embeddable types

Collection types

although not featured in the aforementioned example, collection types are also a distinct category among value types. Collection types are further discussed in Collections

2.1.2. Entity types

Entities, by nature of their unique identifier, exist independently of other objects whereas values do not. Entities are domain model classes which correlate to rows in a database table, using a unique identifier. Because of the requirement for a unique identifier, entities exist independently and define their own lifecycle. The Contact class itself would be an example of an entity.

Mapping entities is discussed in detail in Entity types.

2.2. Naming strategies

Part of the mapping of an object model to the relational database is mapping names from the object model to the corresponding database names. Hibernate looks at this as 2-stage process:

  • The first stage is determining a proper logical name from the domain model mapping. A logical name can be either explicitly specified by the user (e.g., using @Column or @Table) or it can be implicitly determined by Hibernate through an ImplicitNamingStrategy contract.

  • Second is the resolving of this logical name to a physical name which is defined by the PhysicalNamingStrategy contract.

Historical NamingStrategy contract

Historically Hibernate defined just a single org.hibernate.cfg.NamingStrategy. That singular NamingStrategy contract actually combined the separate concerns that are now modeled individually as ImplicitNamingStrategy and PhysicalNamingStrategy.

Also, the NamingStrategy contract was often not flexible enough to properly apply a given naming “rule”, either because the API lacked the information to decide or because the API was honestly not well defined as it grew.

Due to these limitation, org.hibernate.cfg.NamingStrategy has been deprecated in favor of ImplicitNamingStrategy and PhysicalNamingStrategy.

At the core, the idea behind each naming strategy is to minimize the amount of repetitive information a developer must provide for mapping a domain model.

JPA Compatibility

JPA defines inherent rules about implicit logical name determination. If JPA provider portability is a major concern, or if you really just like the JPA-defined implicit naming rules, be sure to stick with ImplicitNamingStrategyJpaCompliantImpl (the default).

Also, JPA defines no separation between logical and physical name. Following the JPA specification, the logical name is the physical name. If JPA provider portability is important, applications should prefer not to specify a PhysicalNamingStrategy.

2.2.1. ImplicitNamingStrategy

When an entity does not explicitly name the database table that it maps to, we need to implicitly determine that table name. Or when a particular attribute does not explicitly name the database column that it maps to, we need to implicitly determine that column name. There are examples of the role of the org.hibernate.boot.model.naming.ImplicitNamingStrategy contract to determine a logical name when the mapping did not provide an explicit name.

Implicit Naming Strategy Diagram

Hibernate defines multiple ImplicitNamingStrategy implementations out-of-the-box. Applications are also free to plug in custom implementations.

There are multiple ways to specify the ImplicitNamingStrategy to use. First, applications can specify the implementation using the hibernate.implicit_naming_strategy configuration setting which accepts:

  • pre-defined “short names” for the out-of-the-box implementations

    default

    for org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl - an alias for jpa

    jpa

    for org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl - the JPA 2.0 compliant naming strategy

    legacy-hbm

    for org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyHbmImpl - compliant with the original Hibernate NamingStrategy

    legacy-jpa

    for org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl - compliant with the legacy NamingStrategy developed for JPA 1.0, which was unfortunately unclear in many respects regarding implicit naming rules

    component-path

    for org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl - mostly follows ImplicitNamingStrategyJpaCompliantImpl rules, except that it uses the full composite paths, as opposed to just the ending property part

  • reference to a Class that implements the org.hibernate.boot.model.naming.ImplicitNamingStrategy contract

  • FQN of a class that implements the org.hibernate.boot.model.naming.ImplicitNamingStrategy contract

Secondly, applications and integrations can leverage org.hibernate.boot.MetadataBuilder#applyImplicitNamingStrategy to specify the ImplicitNamingStrategy to use. See Bootstrap for additional details on bootstrapping.

2.2.2. PhysicalNamingStrategy

Many organizations define rules around the naming of database objects (tables, columns, foreign keys, etc). The idea of a PhysicalNamingStrategy is to help implement such naming rules without having to hard-code them into the mapping via explicit names.

While the purpose of an ImplicitNamingStrategy is to determine that an attribute named accountNumber maps to a logical column name of accountNumber when not explicitly specified, the purpose of a PhysicalNamingStrategy would be, for example, to say that the physical column name should instead be abbreviated acct_num.

It is true that the resolution to acct_num could have been handled using an ImplicitNamingStrategy in this case.

But the point here is the separation of concerns. The PhysicalNamingStrategy will be applied regardless of whether the attribute explicitly specified the column name or whether we determined that implicitly. The ImplicitNamingStrategy would only be applied if an explicit name was not given. So, it all depends on needs and intent.

The default implementation is to simply use the logical name as the physical name. However applications and integrations can define custom implementations of this PhysicalNamingStrategy contract. Here is an example PhysicalNamingStrategy for a fictitious company named Acme Corp whose naming standards are to:

  • prefer underscore-delimited words rather than camel casing

  • replace certain words with standard abbreviations

Example 2. Example PhysicalNamingStrategy implementation

  1. /*
  2. * Hibernate, Relational Persistence for Idiomatic Java
  3. *
  4. * License: GNU Lesser General Public License (LGPL), version 2.1 or later.
  5. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
  6. */
  7. package org.hibernate.userguide.naming;
  8. import java.util.LinkedList;
  9. import java.util.List;
  10. import java.util.Locale;
  11. import java.util.Map;
  12. import java.util.TreeMap;
  13. import org.hibernate.boot.model.naming.Identifier;
  14. import org.hibernate.boot.model.naming.PhysicalNamingStrategy;
  15. import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
  16. import org.apache.commons.lang3.StringUtils;
  17. /**
  18. * An example PhysicalNamingStrategy that implements database object naming standards
  19. * for our fictitious company Acme Corp.
  20. * <p/>
  21. * In general Acme Corp prefers underscore-delimited words rather than camel casing.
  22. * <p/>
  23. * Additionally standards call for the replacement of certain words with abbreviations.
  24. *
  25. * @author Steve Ebersole
  26. */
  27. public class AcmeCorpPhysicalNamingStrategy implements PhysicalNamingStrategy {
  28. private static final Map<String,String> ABBREVIATIONS = buildAbbreviationMap();
  29. @Override
  30. public Identifier toPhysicalCatalogName(Identifier name, JdbcEnvironment jdbcEnvironment) {
  31. // Acme naming standards do not apply to catalog names
  32. return name;
  33. }
  34. @Override
  35. public Identifier toPhysicalSchemaName(Identifier name, JdbcEnvironment jdbcEnvironment) {
  36. // Acme naming standards do not apply to schema names
  37. return name;
  38. }
  39. @Override
  40. public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment jdbcEnvironment) {
  41. final List<String> parts = splitAndReplace( name.getText() );
  42. return jdbcEnvironment.getIdentifierHelper().toIdentifier(
  43. join( parts ),
  44. name.isQuoted()
  45. );
  46. }
  47. @Override
  48. public Identifier toPhysicalSequenceName(Identifier name, JdbcEnvironment jdbcEnvironment) {
  49. final LinkedList<String> parts = splitAndReplace( name.getText() );
  50. // Acme Corp says all sequences should end with _seq
  51. if ( !"seq".equalsIgnoreCase( parts.getLast() ) ) {
  52. parts.add( "seq" );
  53. }
  54. return jdbcEnvironment.getIdentifierHelper().toIdentifier(
  55. join( parts ),
  56. name.isQuoted()
  57. );
  58. }
  59. @Override
  60. public Identifier toPhysicalColumnName(Identifier name, JdbcEnvironment jdbcEnvironment) {
  61. final List<String> parts = splitAndReplace( name.getText() );
  62. return jdbcEnvironment.getIdentifierHelper().toIdentifier(
  63. join( parts ),
  64. name.isQuoted()
  65. );
  66. }
  67. private static Map<String, String> buildAbbreviationMap() {
  68. TreeMap<String,String> abbreviationMap = new TreeMap<> ( String.CASE_INSENSITIVE_ORDER );
  69. abbreviationMap.put( "account", "acct" );
  70. abbreviationMap.put( "number", "num" );
  71. return abbreviationMap;
  72. }
  73. private LinkedList<String> splitAndReplace(String name) {
  74. LinkedList<String> result = new LinkedList<>();
  75. for ( String part : StringUtils.splitByCharacterTypeCamelCase( name ) ) {
  76. if ( part == null || part.trim().isEmpty() ) {
  77. // skip null and space
  78. continue;
  79. }
  80. part = applyAbbreviationReplacement( part );
  81. result.add( part.toLowerCase( Locale.ROOT ) );
  82. }
  83. return result;
  84. }
  85. private String applyAbbreviationReplacement(String word) {
  86. if ( ABBREVIATIONS.containsKey( word ) ) {
  87. return ABBREVIATIONS.get( word );
  88. }
  89. return word;
  90. }
  91. private String join(List<String> parts) {
  92. boolean firstPass = true;
  93. String separator = "";
  94. StringBuilder joined = new StringBuilder();
  95. for ( String part : parts ) {
  96. joined.append( separator ).append( part );
  97. if ( firstPass ) {
  98. firstPass = false;
  99. separator = "_";
  100. }
  101. }
  102. return joined.toString();
  103. }
  104. }

There are multiple ways to specify the PhysicalNamingStrategy to use. First, applications can specify the implementation using the hibernate.physical_naming_strategy configuration setting which accepts:

  • reference to a Class that implements the org.hibernate.boot.model.naming.PhysicalNamingStrategy contract

  • FQN of a class that implements the org.hibernate.boot.model.naming.PhysicalNamingStrategy contract

Secondly, applications and integrations can leverage org.hibernate.boot.MetadataBuilder#applyPhysicalNamingStrategy. See Bootstrap for additional details on bootstrapping.

2.3. Basic types

Basic value types usually map a single database column, to a single, non-aggregated Java type. Hibernate provides a number of built-in basic types, which follow the natural mappings recommended by the JDBC specifications.

Internally Hibernate uses a registry of basic types when it needs to resolve a specific org.hibernate.type.Type.

2.3.1. Hibernate-provided BasicTypes

Table 1. Standard BasicTypes
Hibernate type (org.hibernate.type package)JDBC typeJava typeBasicTypeRegistry key(s)

StringType

VARCHAR

java.lang.String

string, java.lang.String

MaterializedClob

CLOB

java.lang.String

materialized_clob

TextType

LONGVARCHAR

java.lang.String

text

CharacterType

CHAR

char, java.lang.Character

character, char, java.lang.Character

BooleanType

BOOLEAN

boolean, java.lang.Boolean

boolean, java.lang.Boolean

NumericBooleanType

INTEGER, 0 is false, 1 is true

boolean, java.lang.Boolean

numeric_boolean

YesNoType

CHAR, ‘N’/‘n’ is false, ‘Y’/‘y’ is true. The uppercase value is written to the database.

boolean, java.lang.Boolean

yes_no

TrueFalseType

CHAR, ‘F’/‘f’ is false, ‘T’/‘t’ is true. The uppercase value is written to the database.

boolean, java.lang.Boolean

true_false

ByteType

TINYINT

byte, java.lang.Byte

byte, java.lang.Byte

ShortType

SMALLINT

short, java.lang.Short

short, java.lang.Short

IntegerType

INTEGER

int, java.lang.Integer

integer, int, java.lang.Integer

LongType

BIGINT

long, java.lang.Long

long, java.lang.Long

FloatType

FLOAT

float, java.lang.Float

float, java.lang.Float

DoubleType

DOUBLE

double, java.lang.Double

double, java.lang.Double

BigIntegerType

NUMERIC

java.math.BigInteger

big_integer, java.math.BigInteger

BigDecimalType

NUMERIC

java.math.BigDecimal

big_decimal, java.math.bigDecimal

TimestampType

TIMESTAMP

java.util.Date

timestamp, java.sql.Timestamp, java.util.Date

DbTimestampType

TIMESTAMP

java.util.Date

dbtimestamp

TimeType

TIME

java.util.Date

time, java.sql.Time

DateType

DATE

java.util.Date

date, java.sql.Date

CalendarType

TIMESTAMP

java.util.Calendar

calendar, java.util.Calendar, java.util.GregorianCalendar

CalendarDateType

DATE

java.util.Calendar

calendar_date

CalendarTimeType

TIME

java.util.Calendar

calendar_time

CurrencyType

VARCHAR

java.util.Currency

currency, java.util.Currency

LocaleType

VARCHAR

java.util.Locale

locale, java.util.Locale

TimeZoneType

VARCHAR, using the TimeZone ID

java.util.TimeZone

timezone, java.util.TimeZone

UrlType

VARCHAR

java.net.URL

url, java.net.URL

ClassType

VARCHAR (class FQN)

java.lang.Class

class, java.lang.Class

BlobType

BLOB

java.sql.Blob

blob, java.sql.Blob

ClobType

CLOB

java.sql.Clob

clob, java.sql.Clob

BinaryType

VARBINARY

byte[]

binary, byte[]

MaterializedBlobType

BLOB

byte[]

materialized_blob

ImageType

LONGVARBINARY

byte[]

image

WrapperBinaryType

VARBINARY

java.lang.Byte[]

wrapper-binary, Byte[], java.lang.Byte[]

CharArrayType

VARCHAR

char[]

characters, char[]

CharacterArrayType

VARCHAR

java.lang.Character[]

wrapper-characters, Character[], java.lang.Character[]

UUIDBinaryType

BINARY

java.util.UUID

uuid-binary, java.util.UUID

UUIDCharType

CHAR, can also read VARCHAR

java.util.UUID

uuid-char

PostgresUUIDType

PostgreSQL UUID, through Types#OTHER, which complies to the PostgreSQL JDBC driver definition

java.util.UUID

pg-uuid

SerializableType

VARBINARY

implementors of java.lang.Serializable

Unlike the other value types, multiple instances of this type are registered. It is registered once under java.io.Serializable, and registered under the specific java.io.Serializable implementation class names.

StringNVarcharType

NVARCHAR

java.lang.String

nstring

NTextType

LONGNVARCHAR

java.lang.String

ntext

NClobType

NCLOB

java.sql.NClob

nclob, java.sql.NClob

MaterializedNClobType

NCLOB

java.lang.String

materialized_nclob

PrimitiveCharacterArrayNClobType

NCHAR

char[]

N/A

CharacterNCharType

NCHAR

java.lang.Character

ncharacter

CharacterArrayNClobType

NCLOB

java.lang.Character[]

N/A

RowVersionType

VARBINARY

byte[]

row_version

ObjectType

VARCHAR

implementors of java.lang.Serializable

object, java.lang.Object

Table 2. Java 8 BasicTypes
Hibernate type (org.hibernate.type package)JDBC typeJava typeBasicTypeRegistry key(s)

DurationType

BIGINT

java.time.Duration

Duration, java.time.Duration

InstantType

TIMESTAMP

java.time.Instant

Instant, java.time.Instant

LocalDateTimeType

TIMESTAMP

java.time.LocalDateTime

LocalDateTime, java.time.LocalDateTime

LocalDateType

DATE

java.time.LocalDate

LocalDate, java.time.LocalDate

LocalTimeType

TIME

java.time.LocalTime

LocalTime, java.time.LocalTime

OffsetDateTimeType

TIMESTAMP

java.time.OffsetDateTime

OffsetDateTime, java.time.OffsetDateTime

OffsetTimeType

TIME

java.time.OffsetTime

OffsetTime, java.time.OffsetTime

ZonedDateTimeType

TIMESTAMP

java.time.ZonedDateTime

ZonedDateTime, java.time.ZonedDateTime

Table 3. Hibernate Spatial BasicTypes
Hibernate type (org.hibernate.spatial package)JDBC typeJava typeBasicTypeRegistry key(s)

JTSGeometryType

depends on the dialect

com.vividsolutions.jts.geom.Geometry

jts_geometry, and the class names of Geometry and its subclasses

GeolatteGeometryType

depends on the dialect

org.geolatte.geom.Geometry

geolatte_geometry, and the class names of Geometry and its subclasses

To use the Hibernate Spatial types, you must add the hibernate-spatial dependency to your classpath and use an org.hibernate.spatial.SpatialDialect implementation.

See the Spatial chapter for more details.

These mappings are managed by a service inside Hibernate called the org.hibernate.type.BasicTypeRegistry, which essentially maintains a map of org.hibernate.type.BasicType (a org.hibernate.type.Type specialization) instances keyed by a name. That is the purpose of the “BasicTypeRegistry key(s)” column in the previous tables.

2.3.2. The @Basic annotation

Strictly speaking, a basic type is denoted by the javax.persistence.Basic annotation. Generally speaking, the @Basic annotation can be ignored, as it is assumed by default. Both of the following examples are ultimately the same.

Example 3. @Basic declared explicitly

  1. @Entity(name = "Product")
  2. public class Product {
  3. @Id
  4. @Basic
  5. private Integer id;
  6. @Basic
  7. private String sku;
  8. @Basic
  9. private String name;
  10. @Basic
  11. private String description;
  12. }

Example 4. @Basic being implicitly implied

  1. @Entity(name = "Product")
  2. public class Product {
  3. @Id
  4. private Integer id;
  5. private String sku;
  6. private String name;
  7. private String description;
  8. }

The JPA specification strictly limits the Java types that can be marked as basic to the following listing:

  • Java primitive types (boolean, int, etc)

  • wrappers for the primitive types (java.lang.Boolean, java.lang.Integer, etc)

  • java.lang.String

  • java.math.BigInteger

  • java.math.BigDecimal

  • java.util.Date

  • java.util.Calendar

  • java.sql.Date

  • java.sql.Time

  • java.sql.Timestamp

  • byte[] or Byte[]

  • char[] or Character[]

  • enums

  • any other type that implements Serializable (JPA’s “support” for Serializable types is to directly serialize their state to the database).

If provider portability is a concern, you should stick to just these basic types.

Note that JPA 2.1 introduced the javax.persistence.AttributeConverter contract to help alleviate some of these concerns. See JPA 2.1 AttributeConverters for more on this topic.

The @Basic annotation defines 2 attributes.

optional - boolean (defaults to true)

Defines whether this attribute allows nulls. JPA defines this as “a hint”, which essentially means that its effect is specifically required. As long as the type is not primitive, Hibernate takes this to mean that the underlying column should be NULLABLE.

fetch - FetchType (defaults to EAGER)

Defines whether this attribute should be fetched eagerly or lazily. JPA says that EAGER is a requirement to the provider (Hibernate) that the value should be fetched when the owner is fetched, while LAZY is merely a hint that the value is fetched when the attribute is accessed. Hibernate ignores this setting for basic types unless you are using bytecode enhancement. See the Bytecode Enhancement for additional information on fetching and on bytecode enhancement.

2.3.3. The @Column annotation

JPA defines rules for implicitly determining the name of tables and columns. For a detailed discussion of implicit naming see Naming strategies.

For basic type attributes, the implicit naming rule is that the column name is the same as the attribute name. If that implicit naming rule does not meet your requirements, you can explicitly tell Hibernate (and other providers) the column name to use.

Example 5. Explicit column naming

  1. @Entity(name = "Product")
  2. public class Product {
  3. @Id
  4. private Integer id;
  5. private String sku;
  6. private String name;
  7. @Column( name = "NOTES" )
  8. private String description;
  9. }

Here we use @Column to explicitly map the description attribute to the NOTES column, as opposed to the implicit column name description.

The @Column annotation defines other mapping information as well. See its Javadocs for details.

2.3.4. BasicTypeRegistry

We said before that a Hibernate type is not a Java type, nor an SQL type, but that it understands both and performs the marshalling between them. But looking at the basic type mappings from the previous examples, how did Hibernate know to use its org.hibernate.type.StringType for mapping for java.lang.String attributes, or its org.hibernate.type.IntegerType for mapping java.lang.Integer attributes?

The answer lies in a service inside Hibernate called the org.hibernate.type.BasicTypeRegistry, which essentially maintains a map of org.hibernate.type.BasicType (an org.hibernate.type.Type specialization) instances keyed by a name.

We will see later, in the Explicit BasicTypes section, that we can explicitly tell Hibernate which BasicType to use for a particular attribute. But first, let’s explore how implicit resolution works and how applications can adjust the implicit resolution.

A thorough discussion of BasicTypeRegistry and all the different ways to contribute types is beyond the scope of this documentation.

Please see the Integration Guide for complete details.

As an example, take a String attribute such as we saw before with Product#sku. Since there was no explicit type mapping, Hibernate looks to the BasicTypeRegistry to find the registered mapping for java.lang.String. This goes back to the “BasicTypeRegistry key(s)” column we saw in the tables at the start of this chapter.

As a baseline within BasicTypeRegistry, Hibernate follows the recommended mappings of JDBC for Java types. JDBC recommends mapping Strings to VARCHAR, which is the exact mapping that StringType handles. So that is the baseline mapping within BasicTypeRegistry for Strings.

Applications can also extend (add new BasicType registrations) or override (replace an existing BasicType registration) using one of the MetadataBuilder#applyBasicType methods or the MetadataBuilder#applyTypes method during bootstrap. For more details, see Custom BasicTypes section.

2.3.5. Explicit BasicTypes

Sometimes you want a particular attribute to be handled differently. Occasionally Hibernate will implicitly pick a BasicType that you do not want (and for some reason you do not want to adjust the BasicTypeRegistry).

In these cases, you must explicitly tell Hibernate the BasicType to use, via the org.hibernate.annotations.Type annotation.

Example 6. Using @org.hibernate.annotations.Type

  1. @Entity(name = "Product")
  2. public class Product {
  3. @Id
  4. private Integer id;
  5. private String sku;
  6. @org.hibernate.annotations.Type( type = "nstring" )
  7. private String name;
  8. @org.hibernate.annotations.Type( type = "materialized_nclob" )
  9. private String description;
  10. }

This tells Hibernate to store the Strings as nationalized data. This is just for illustration purposes; for better ways to indicate nationalized character data see Mapping Nationalized Character Data section.

Additionally, the description is to be handled as a LOB. Again, for better ways to indicate LOBs see Mapping LOBs section.

The org.hibernate.annotations.Type#type attribute can name any of the following:

  • Fully qualified name of any org.hibernate.type.Type implementation

  • Any key registered with BasicTypeRegistry

  • The name of any known type definitions

2.3.6. Custom BasicTypes

Hibernate makes it relatively easy for developers to create their own basic type mappings type. For example, you might want to persist properties of type java.util.BigInteger to VARCHAR columns, or support completely new types.

There are two approaches to developing a custom type:

  • implementing a BasicType and registering it

  • implementing a UserType which doesn’t require type registration

As a means of illustrating the different approaches, let’s consider a use case where we need to support a java.util.BitSet mapping that’s stored as a VARCHAR.

Implementing a BasicType

The first approach is to directly implement the BasicType interface.

Because the BasicType interface has a lot of methods to implement, if the value is stored in a single database column, it’s much more convenient to extend the AbstractStandardBasicType or the AbstractSingleColumnStandardBasicType Hibernate classes.

First, we need to extend the AbstractSingleColumnStandardBasicType like this:

Example 7. Custom BasicType implementation

  1. public class BitSetType
  2. extends AbstractSingleColumnStandardBasicType<BitSet>
  3. implements DiscriminatorType<BitSet> {
  4. public static final BitSetType INSTANCE = new BitSetType();
  5. public BitSetType() {
  6. super( VarcharTypeDescriptor.INSTANCE, BitSetTypeDescriptor.INSTANCE );
  7. }
  8. @Override
  9. public BitSet stringToObject(String xml) throws Exception {
  10. return fromString( xml );
  11. }
  12. @Override
  13. public String objectToSQLString(BitSet value, Dialect dialect) throws Exception {
  14. return toString( value );
  15. }
  16. @Override
  17. public String getName() {
  18. return "bitset";
  19. }
  20. }

The AbstractSingleColumnStandardBasicType requires an sqlTypeDescriptor and a javaTypeDescriptor. The sqlTypeDescriptor is VarcharTypeDescriptor.INSTANCE because the database column is a VARCHAR. On the Java side, we need to use a BitSetTypeDescriptor instance which can be implemented like this:

Example 8. Custom AbstractTypeDescriptor implementation

  1. public class BitSetTypeDescriptor extends AbstractTypeDescriptor<BitSet> {
  2. private static final String DELIMITER = ",";
  3. public static final BitSetTypeDescriptor INSTANCE = new BitSetTypeDescriptor();
  4. public BitSetTypeDescriptor() {
  5. super( BitSet.class );
  6. }
  7. @Override
  8. public String toString(BitSet value) {
  9. StringBuilder builder = new StringBuilder();
  10. for ( long token : value.toLongArray() ) {
  11. if ( builder.length() > 0 ) {
  12. builder.append( DELIMITER );
  13. }
  14. builder.append( Long.toString( token, 2 ) );
  15. }
  16. return builder.toString();
  17. }
  18. @Override
  19. public BitSet fromString(String string) {
  20. if ( string == null || string.isEmpty() ) {
  21. return null;
  22. }
  23. String[] tokens = string.split( DELIMITER );
  24. long[] values = new long[tokens.length];
  25. for ( int i = 0; i < tokens.length; i++ ) {
  26. values[i] = Long.valueOf( tokens[i], 2 );
  27. }
  28. return BitSet.valueOf( values );
  29. }
  30. @SuppressWarnings({"unchecked"})
  31. public <X> X unwrap(BitSet value, Class<X> type, WrapperOptions options) {
  32. if ( value == null ) {
  33. return null;
  34. }
  35. if ( BitSet.class.isAssignableFrom( type ) ) {
  36. return (X) value;
  37. }
  38. if ( String.class.isAssignableFrom( type ) ) {
  39. return (X) toString( value);
  40. }
  41. throw unknownUnwrap( type );
  42. }
  43. public <X> BitSet wrap(X value, WrapperOptions options) {
  44. if ( value == null ) {
  45. return null;
  46. }
  47. if ( String.class.isInstance( value ) ) {
  48. return fromString( (String) value );
  49. }
  50. if ( BitSet.class.isInstance( value ) ) {
  51. return (BitSet) value;
  52. }
  53. throw unknownWrap( value.getClass() );
  54. }
  55. }

The unwrap method is used when passing a BitSet as a PreparedStatement bind parameter, while the wrap method is used to transform the JDBC column value object (e.g. String in our case) to the actual mapping object type (e.g. BitSet in this example).

The BasicType must be registered, and this can be done at bootstrapping time:

Example 9. Register a Custom BasicType implementation

  1. configuration.registerTypeContributor( (typeContributions, serviceRegistry) -> {
  2. typeContributions.contributeType( BitSetType.INSTANCE );
  3. } );

or using the MetadataBuilder

  1. ServiceRegistry standardRegistry =
  2. new StandardServiceRegistryBuilder().build();
  3. MetadataSources sources = new MetadataSources( standardRegistry );
  4. MetadataBuilder metadataBuilder = sources.getMetadataBuilder();
  5. metadataBuilder.applyBasicType( BitSetType.INSTANCE );

With the new BitSetType being registered as bitset, the entity mapping looks like this:

Example 10. Custom BasicType mapping

  1. @Entity(name = "Product")
  2. public static class Product {
  3. @Id
  4. private Integer id;
  5. @Type( type = "bitset" )
  6. private BitSet bitSet;
  7. public Integer getId() {
  8. return id;
  9. }
  10. //Getters and setters are omitted for brevity
  11. }

Alternatively, you can use the @TypeDef and skip the registration phase:

Example 11. Using @TypeDef to register a custom Type

  1. @Entity(name = "Product")
  2. @TypeDef(
  3. name = "bitset",
  4. defaultForType = BitSet.class,
  5. typeClass = BitSetType.class
  6. )
  7. public static class Product {
  8. @Id
  9. private Integer id;
  10. private BitSet bitSet;
  11. //Getters and setters are omitted for brevity
  12. }

To validate this new BasicType implementation, we can test it as follows:

Example 12. Persisting the custom BasicType

  1. BitSet bitSet = BitSet.valueOf( new long[] {1, 2, 3} );
  2. doInHibernate( this::sessionFactory, session -> {
  3. Product product = new Product( );
  4. product.setId( 1 );
  5. product.setBitSet( bitSet );
  6. session.persist( product );
  7. } );
  8. doInHibernate( this::sessionFactory, session -> {
  9. Product product = session.get( Product.class, 1 );
  10. assertEquals(bitSet, product.getBitSet());
  11. } );

When executing this unit test, Hibernate generates the following SQL statements:

Example 13. Persisting the custom BasicType

  1. DEBUG SQL:92 -
  2. insert
  3. into
  4. Product
  5. (bitSet, id)
  6. values
  7. (?, ?)
  8. TRACE BasicBinder:65 - binding parameter [1] as [VARCHAR] - [{0, 65, 128, 129}]
  9. TRACE BasicBinder:65 - binding parameter [2] as [INTEGER] - [1]
  10. DEBUG SQL:92 -
  11. select
  12. bitsettype0_.id as id1_0_0_,
  13. bitsettype0_.bitSet as bitSet2_0_0_
  14. from
  15. Product bitsettype0_
  16. where
  17. bitsettype0_.id=?
  18. TRACE BasicBinder:65 - binding parameter [1] as [INTEGER] - [1]
  19. TRACE BasicExtractor:61 - extracted value ([bitSet2_0_0_] : [VARCHAR]) - [{0, 65, 128, 129}]

As you can see, the BitSetType takes care of the Java-to-SQL and SQL-to-Java type conversion.

Implementing a UserType

The second approach is to implement the UserType interface.

Example 14. Custom UserType implementation

  1. public class BitSetUserType implements UserType {
  2. public static final BitSetUserType INSTANCE = new BitSetUserType();
  3. private static final Logger log = Logger.getLogger( BitSetUserType.class );
  4. @Override
  5. public int[] sqlTypes() {
  6. return new int[] {StringType.INSTANCE.sqlType()};
  7. }
  8. @Override
  9. public Class returnedClass() {
  10. return BitSet.class;
  11. }
  12. @Override
  13. public boolean equals(Object x, Object y)
  14. throws HibernateException {
  15. return Objects.equals( x, y );
  16. }
  17. @Override
  18. public int hashCode(Object x)
  19. throws HibernateException {
  20. return Objects.hashCode( x );
  21. }
  22. @Override
  23. public Object nullSafeGet(
  24. ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner)
  25. throws HibernateException, SQLException {
  26. String columnName = names[0];
  27. String columnValue = (String) rs.getObject( columnName );
  28. log.debugv("Result set column {0} value is {1}", columnName, columnValue);
  29. return columnValue == null ? null :
  30. BitSetTypeDescriptor.INSTANCE.fromString( columnValue );
  31. }
  32. @Override
  33. public void nullSafeSet(
  34. PreparedStatement st, Object value, int index, SharedSessionContractImplementor session)
  35. throws HibernateException, SQLException {
  36. if ( value == null ) {
  37. log.debugv("Binding null to parameter {0} ",index);
  38. st.setNull( index, Types.VARCHAR );
  39. }
  40. else {
  41. String stringValue = BitSetTypeDescriptor.INSTANCE.toString( (BitSet) value );
  42. log.debugv("Binding {0} to parameter {1} ", stringValue, index);
  43. st.setString( index, stringValue );
  44. }
  45. }
  46. @Override
  47. public Object deepCopy(Object value)
  48. throws HibernateException {
  49. return value == null ? null :
  50. BitSet.valueOf( BitSet.class.cast( value ).toLongArray() );
  51. }
  52. @Override
  53. public boolean isMutable() {
  54. return true;
  55. }
  56. @Override
  57. public Serializable disassemble(Object value)
  58. throws HibernateException {
  59. return (BitSet) deepCopy( value );
  60. }
  61. @Override
  62. public Object assemble(Serializable cached, Object owner)
  63. throws HibernateException {
  64. return deepCopy( cached );
  65. }
  66. @Override
  67. public Object replace(Object original, Object target, Object owner)
  68. throws HibernateException {
  69. return deepCopy( original );
  70. }
  71. }

The entity mapping looks as follows:

Example 15. Custom UserType mapping

  1. @Entity(name = "Product")
  2. public static class Product {
  3. @Id
  4. private Integer id;
  5. @Type( type = "bitset" )
  6. private BitSet bitSet;
  7. //Constructors, getters, and setters are omitted for brevity
  8. }

In this example, the UserType is registered under the bitset name, and this is done like this:

Example 16. Register a Custom UserType implementation

  1. configuration.registerTypeContributor( (typeContributions, serviceRegistry) -> {
  2. typeContributions.contributeType( BitSetUserType.INSTANCE, "bitset");
  3. } );

or using the MetadataBuilder

  1. ServiceRegistry standardRegistry =
  2. new StandardServiceRegistryBuilder().build();
  3. MetadataSources sources = new MetadataSources( standardRegistry );
  4. MetadataBuilder metadataBuilder = sources.getMetadataBuilder();
  5. metadataBuilder.applyBasicType( BitSetUserType.INSTANCE, "bitset" );

Like BasicType, you can also register the UserType using a simple name.

Without registering a name, the UserType mapping requires the fully qualified class name:

  1. @Type( type = org.hibernate.userguide.mapping.basic.BitSetUserType )

When running the previous test case against the BitSetUserType entity mapping, Hibernate executed the following SQL statements:

Example 17. Persisting the custom BasicType

  1. DEBUG SQL:92 -
  2. insert
  3. into
  4. Product
  5. (bitSet, id)
  6. values
  7. (?, ?)
  8. DEBUG BitSetUserType:71 - Binding 1,10,11 to parameter 1
  9. TRACE BasicBinder:65 - binding parameter [2] as [INTEGER] - [1]
  10. DEBUG SQL:92 -
  11. select
  12. bitsetuser0_.id as id1_0_0_,
  13. bitsetuser0_.bitSet as bitSet2_0_0_
  14. from
  15. Product bitsetuser0_
  16. where
  17. bitsetuser0_.id=?
  18. TRACE BasicBinder:65 - binding parameter [1] as [INTEGER] - [1]
  19. DEBUG BitSetUserType:56 - Result set column bitSet2_0_0_ value is 1,10,11

2.3.7. Mapping enums

Hibernate supports the mapping of Java enums as basic value types in a number of different ways.

@Enumerated

The original JPA-compliant way to map enums was via the @Enumerated or @MapKeyEnumerated for map keys annotations, working on the principle that the enum values are stored according to one of 2 strategies indicated by javax.persistence.EnumType:

ORDINAL

stored according to the enum value’s ordinal position within the enum class, as indicated by java.lang.Enum#ordinal

STRING

stored according to the enum value’s name, as indicated by java.lang.Enum#name

Assuming the following enumeration:

Example 18. PhoneType enumeration

  1. public enum PhoneType {
  2. LAND_LINE,
  3. MOBILE;
  4. }

In the ORDINAL example, the phone_type column is defined as a (nullable) INTEGER type and would hold:

NULL

For null values

0

For the LAND_LINE enum

1

For the MOBILE enum

Example 19. @Enumerated(ORDINAL) example

  1. @Entity(name = "Phone")
  2. public static class Phone {
  3. @Id
  4. private Long id;
  5. @Column(name = "phone_number")
  6. private String number;
  7. @Enumerated(EnumType.ORDINAL)
  8. @Column(name = "phone_type")
  9. private PhoneType type;
  10. //Getters and setters are omitted for brevity
  11. }

When persisting this entity, Hibernate generates the following SQL statement:

Example 20. Persisting an entity with an @Enumerated(ORDINAL) mapping

  1. Phone phone = new Phone( );
  2. phone.setId( 1L );
  3. phone.setNumber( "123-456-78990" );
  4. phone.setType( PhoneType.MOBILE );
  5. entityManager.persist( phone );
  1. INSERT INTO Phone (phone_number, phone_type, id)
  2. VALUES ('123-456-78990', 2, 1)

In the STRING example, the phone_type column is defined as a (nullable) VARCHAR type and would hold:

NULL

For null values

LAND_LINE

For the LAND_LINE enum

MOBILE

For the MOBILE enum

Example 21. @Enumerated(STRING) example

  1. @Entity(name = "Phone")
  2. public static class Phone {
  3. @Id
  4. private Long id;
  5. @Column(name = "phone_number")
  6. private String number;
  7. @Enumerated(EnumType.STRING)
  8. @Column(name = "phone_type")
  9. private PhoneType type;
  10. //Getters and setters are omitted for brevity
  11. }

Persisting the same entity as in the @Enumerated(ORDINAL) example, Hibernate generates the following SQL statement:

Example 22. Persisting an entity with an @Enumerated(STRING) mapping

  1. INSERT INTO Phone (phone_number, phone_type, id)
  2. VALUES ('123-456-78990', 'MOBILE', 1)
AttributeConverter

Let’s consider the following Gender enum which stores its values using the 'M' and 'F' codes.

Example 23. Enum with a custom constructor

  1. public enum Gender {
  2. MALE( 'M' ),
  3. FEMALE( 'F' );
  4. private final char code;
  5. Gender(char code) {
  6. this.code = code;
  7. }
  8. public static Gender fromCode(char code) {
  9. if ( code == 'M' || code == 'm' ) {
  10. return MALE;
  11. }
  12. if ( code == 'F' || code == 'f' ) {
  13. return FEMALE;
  14. }
  15. throw new UnsupportedOperationException(
  16. "The code " + code + " is not supported!"
  17. );
  18. }
  19. public char getCode() {
  20. return code;
  21. }
  22. }

You can map enums in a JPA compliant way using a JPA 2.1 AttributeConverter.

Example 24. Enum mapping with AttributeConverter example

  1. @Entity(name = "Person")
  2. public static class Person {
  3. @Id
  4. private Long id;
  5. private String name;
  6. @Convert( converter = GenderConverter.class )
  7. public Gender gender;
  8. //Getters and setters are omitted for brevity
  9. }
  10. @Converter
  11. public static class GenderConverter
  12. implements AttributeConverter<Gender, Character> {
  13. public Character convertToDatabaseColumn( Gender value ) {
  14. if ( value == null ) {
  15. return null;
  16. }
  17. return value.getCode();
  18. }
  19. public Gender convertToEntityAttribute( Character value ) {
  20. if ( value == null ) {
  21. return null;
  22. }
  23. return Gender.fromCode( value );
  24. }
  25. }

Here, the gender column is defined as a CHAR type and would hold:

NULL

For null values

'M'

For the MALE enum

'F'

For the FEMALE enum

For additional details on using AttributeConverters, see JPA 2.1 AttributeConverters section.

JPA explicitly disallows the use of an AttributeConverter with an attribute marked as @Enumerated.

So, when using the AttributeConverter approach, be sure not to mark the attribute as @Enumerated.

Using the AttributeConverter entity property as a query parameter

Assuming you have the following entity:

Example 25. Photo entity with AttributeConverter

  1. @Entity(name = "Photo")
  2. public static class Photo {
  3. @Id
  4. private Integer id;
  5. private String name;
  6. @Convert(converter = CaptionConverter.class)
  7. private Caption caption;
  8. //Getters and setters are omitted for brevity
  9. }

And the Caption class looks as follows:

Example 26. Caption Java object

  1. public static class Caption {
  2. private String text;
  3. public Caption(String text) {
  4. this.text = text;
  5. }
  6. public String getText() {
  7. return text;
  8. }
  9. public void setText(String text) {
  10. this.text = text;
  11. }
  12. @Override
  13. public boolean equals(Object o) {
  14. if ( this == o ) {
  15. return true;
  16. }
  17. if ( o == null || getClass() != o.getClass() ) {
  18. return false;
  19. }
  20. Caption caption = (Caption) o;
  21. return text != null ? text.equals( caption.text ) : caption.text == null;
  22. }
  23. @Override
  24. public int hashCode() {
  25. return text != null ? text.hashCode() : 0;
  26. }
  27. }

And we have an AttributeConverter to handle the Caption Java object:

Example 27. Caption Java object AttributeConverter

  1. public static class CaptionConverter
  2. implements AttributeConverter<Caption, String> {
  3. @Override
  4. public String convertToDatabaseColumn(Caption attribute) {
  5. return attribute.getText();
  6. }
  7. @Override
  8. public Caption convertToEntityAttribute(String dbData) {
  9. return new Caption( dbData );
  10. }
  11. }

Traditionally, you could only use the DB data Caption representation, which in our case is a String, when referencing the caption entity property.

Example 28. Filtering by the Caption property using the DB data representation

  1. Photo photo = entityManager.createQuery(
  2. "select p " +
  3. "from Photo p " +
  4. "where upper(caption) = upper(:caption) ", Photo.class )
  5. .setParameter( "caption", "Nicolae Grigorescu" )
  6. .getSingleResult();

In order to use the Java object Caption representation, you have to get the associated Hibernate Type.

Example 29. Filtering by the Caption property using the Java Object representation

  1. SessionFactory sessionFactory = entityManager.getEntityManagerFactory()
  2. .unwrap( SessionFactory.class );
  3. MetamodelImplementor metamodelImplementor = (MetamodelImplementor) sessionFactory.getMetamodel();
  4. Type captionType = metamodelImplementor
  5. .entityPersister( Photo.class.getName() )
  6. .getPropertyType( "caption" );
  7. Photo photo = (Photo) entityManager.createQuery(
  8. "select p " +
  9. "from Photo p " +
  10. "where upper(caption) = upper(:caption) ", Photo.class )
  11. .unwrap( Query.class )
  12. .setParameter( "caption", new Caption("Nicolae Grigorescu"), captionType)
  13. .getSingleResult();

By passing the associated Hibernate Type, you can use the Caption object when binding the query parameter value.

Mapping an AttributeConverter using HBM mappings

When using HBM mappings, you can still make use of the JPA AttributeConverter because Hibernate supports such mapping via the type attribute as demonstrated by the following example.

Let’s consider we have an application-specific Money type:

Example 30. Application-specific Money type

  1. public class Money {
  2. private long cents;
  3. public Money(long cents) {
  4. this.cents = cents;
  5. }
  6. public long getCents() {
  7. return cents;
  8. }
  9. public void setCents(long cents) {
  10. this.cents = cents;
  11. }
  12. }

Now, we want to use the Money type when mapping the Account entity:

Example 31. Account entity using the Money type

  1. public class Account {
  2. private Long id;
  3. private String owner;
  4. private Money balance;
  5. //Getters and setters are omitted for brevity
  6. }

Since Hibernate has no knowledge how to persist the Money type, we could use a JPA AttributeConverter to transform the Money type as a Long. For this purpose, we are going to use the following MoneyConverter utility:

Example 32. MoneyConverter implementing the JPA AttributeConverter interface

  1. public class MoneyConverter
  2. implements AttributeConverter<Money, Long> {
  3. @Override
  4. public Long convertToDatabaseColumn(Money attribute) {
  5. return attribute == null ? null : attribute.getCents();
  6. }
  7. @Override
  8. public Money convertToEntityAttribute(Long dbData) {
  9. return dbData == null ? null : new Money( dbData );
  10. }
  11. }

To map the MoneyConverter using HBM configuration files you need to use the converted:: prefix in the type attribute of the property element.

Example 33. HBM mapping for AttributeConverter

  1. <?xml version="1.0"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC
  3. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  4. "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
  5. <hibernate-mapping package="org.hibernate.userguide.mapping.converter.hbm">
  6. <class name="Account" table="account" >
  7. <id name="id"/>
  8. <property name="owner"/>
  9. <property name="balance"
  10. type="converted::org.hibernate.userguide.mapping.converter.hbm.MoneyConverter"/>
  11. </class>
  12. </hibernate-mapping>
Custom type

You can also map enums using a Hibernate custom type mapping. Let’s again revisit the Gender enum example, this time using a custom Type to store the more standardized 'M' and 'F' codes.

Example 34. Enum mapping with custom Type example

  1. @Entity(name = "Person")
  2. public static class Person {
  3. @Id
  4. private Long id;
  5. private String name;
  6. @Type( type = "org.hibernate.userguide.mapping.basic.GenderType" )
  7. public Gender gender;
  8. //Getters and setters are omitted for brevity
  9. }
  10. public class GenderType extends AbstractSingleColumnStandardBasicType<Gender> {
  11. public static final GenderType INSTANCE = new GenderType();
  12. public GenderType() {
  13. super(
  14. CharTypeDescriptor.INSTANCE,
  15. GenderJavaTypeDescriptor.INSTANCE
  16. );
  17. }
  18. public String getName() {
  19. return "gender";
  20. }
  21. @Override
  22. protected boolean registerUnderJavaType() {
  23. return true;
  24. }
  25. }
  26. public class GenderJavaTypeDescriptor extends AbstractTypeDescriptor<Gender> {
  27. public static final GenderJavaTypeDescriptor INSTANCE =
  28. new GenderJavaTypeDescriptor();
  29. protected GenderJavaTypeDescriptor() {
  30. super( Gender.class );
  31. }
  32. public String toString(Gender value) {
  33. return value == null ? null : value.name();
  34. }
  35. public Gender fromString(String string) {
  36. return string == null ? null : Gender.valueOf( string );
  37. }
  38. public <X> X unwrap(Gender value, Class<X> type, WrapperOptions options) {
  39. return CharacterTypeDescriptor.INSTANCE.unwrap(
  40. value == null ? null : value.getCode(),
  41. type,
  42. options
  43. );
  44. }
  45. public <X> Gender wrap(X value, WrapperOptions options) {
  46. return Gender.fromCode(
  47. CharacterTypeDescriptor.INSTANCE.wrap( value, options )
  48. );
  49. }
  50. }

Again, the gender column is defined as a CHAR type and would hold:

NULL

For null values

'M'

For the MALE enum

'F'

For the FEMALE enum

For additional details on using custom types, see Custom BasicTypes section.

2.3.8. Mapping LOBs

Mapping LOBs (database Large Objects) come in 2 forms, those using the JDBC locator types and those materializing the LOB data.

JDBC LOB locators exist to allow efficient access to the LOB data. They allow the JDBC driver to stream parts of the LOB data as needed, potentially freeing up memory space. However, they can be unnatural to deal with and have certain limitations. For example, a LOB locator is only portably valid during the duration of the transaction in which it was obtained.

The idea of materialized LOBs is to trade-off the potential efficiency (not all drivers handle LOB data efficiently) for a more natural programming paradigm using familiar Java types such as String or byte[], etc for these LOBs.

Materialized deals with the entire LOB contents in memory, whereas LOB locators (in theory) allow streaming parts of the LOB contents into memory as needed.

The JDBC LOB locator types include:

  • java.sql.Blob

  • java.sql.Clob

  • java.sql.NClob

Mapping materialized forms of these LOB values would use more familiar Java types such as String, char[], byte[], etc. The trade-off for more familiar is usually performance.

Mapping CLOB

For a first look, let’s assume we have a CLOB column that we would like to map (NCLOB character LOB data will be covered in Mapping Nationalized Character Data section).

Considering we have the following database table:

Example 35. CLOB - SQL

  1. CREATE TABLE Product (
  2. id INTEGER NOT NULL,
  3. name VARCHAR(255),
  4. warranty CLOB,
  5. PRIMARY KEY (id)
  6. )

Let’s first map this using the @Lob JPA annotation and the java.sql.Clob type:

Example 36. CLOB mapped to java.sql.Clob

  1. @Entity(name = "Product")
  2. public static class Product {
  3. @Id
  4. private Integer id;
  5. private String name;
  6. @Lob
  7. private Clob warranty;
  8. //Getters and setters are omitted for brevity
  9. }

To persist such an entity, you have to create a Clob using the ClobProxy Hibernate utility:

Example 37. Persisting a java.sql.Clob entity

  1. String warranty = "My product warranty";
  2. final Product product = new Product();
  3. product.setId( 1 );
  4. product.setName( "Mobile phone" );
  5. product.setWarranty( ClobProxy.generateProxy( warranty ) );
  6. entityManager.persist( product );

To retrieve the Clob content, you need to transform the underlying java.io.Reader:

Example 38. Returning a java.sql.Clob entity

  1. Product product = entityManager.find( Product.class, productId );
  2. try (Reader reader = product.getWarranty().getCharacterStream()) {
  3. assertEquals( "My product warranty", toString( reader ) );
  4. }

We could also map the CLOB in a materialized form. This way, we can either use a String or a char[].

Example 39. CLOB mapped to String

  1. @Entity(name = "Product")
  2. public static class Product {
  3. @Id
  4. private Integer id;
  5. private String name;
  6. @Lob
  7. private String warranty;
  8. //Getters and setters are omitted for brevity
  9. }

How JDBC deals with LOB data varies from driver to driver, and Hibernate tries to handle all these variances on your behalf.

However, some drivers are trickier (e.g. PostgreSQL), and, in such cases, you may have to do some extra steps to get LOBs working. Such discussions are beyond the scope of this guide.

We might even want the materialized data as a char array (although this might not be a very good idea).

Example 40. CLOB - materialized char[] mapping

  1. @Entity(name = "Product")
  2. public static class Product {
  3. @Id
  4. private Integer id;
  5. private String name;
  6. @Lob
  7. private char[] warranty;
  8. //Getters and setters are omitted for brevity
  9. }
Mapping BLOB

BLOB data is mapped in a similar fashion.

Considering we have the following database table:

Example 41. BLOB - SQL

  1. CREATE TABLE Product (
  2. id INTEGER NOT NULL ,
  3. image blob ,
  4. name VARCHAR(255) ,
  5. PRIMARY KEY ( id )
  6. )

Let’s first map this using the JDBC java.sql.Blob type.

Example 42. BLOB mapped to java.sql.Blob

  1. @Entity(name = "Product")
  2. public static class Product {
  3. @Id
  4. private Integer id;
  5. private String name;
  6. @Lob
  7. private Blob image;
  8. //Getters and setters are omitted for brevity
  9. }

To persist such an entity, you have to create a Blob using the BlobProxy Hibernate utility:

Example 43. Persisting a java.sql.Blob entity

  1. byte[] image = new byte[] {1, 2, 3};
  2. final Product product = new Product();
  3. product.setId( 1 );
  4. product.setName( "Mobile phone" );
  5. product.setImage( BlobProxy.generateProxy( image ) );
  6. entityManager.persist( product );

To retrieve the Blob content, you need to transform the underlying java.io.InputStream:

Example 44. Returning a java.sql.Blob entity

  1. Product product = entityManager.find( Product.class, productId );
  2. try (InputStream inputStream = product.getImage().getBinaryStream()) {
  3. assertArrayEquals(new byte[] {1, 2, 3}, toBytes( inputStream ) );
  4. }

We could also map the BLOB in a materialized form (e.g. byte[]).

Example 45. BLOB mapped to byte[]

  1. @Entity(name = "Product")
  2. public static class Product {
  3. @Id
  4. private Integer id;
  5. private String name;
  6. @Lob
  7. private byte[] image;
  8. //Getters and setters are omitted for brevity
  9. }

2.3.9. Mapping Nationalized Character Data

JDBC 4 added the ability to explicitly handle nationalized character data. To this end, it added specific nationalized character data types:

  • NCHAR

  • NVARCHAR

  • LONGNVARCHAR

  • NCLOB

Considering we have the following database table:

Example 46. NVARCHAR - SQL

  1. CREATE TABLE Product (
  2. id INTEGER NOT NULL ,
  3. name VARCHAR(255) ,
  4. warranty NVARCHAR(255) ,
  5. PRIMARY KEY ( id )
  6. )

To map a specific attribute to a nationalized variant data type, Hibernate defines the @Nationalized annotation.

Example 47. NVARCHAR mapping

  1. @Entity(name = "Product")
  2. public static class Product {
  3. @Id
  4. private Integer id;
  5. private String name;
  6. @Nationalized
  7. private String warranty;
  8. //Getters and setters are omitted for brevity
  9. }

Just like with CLOB, Hibernate can also deal with NCLOB SQL data types:

Example 48. NCLOB - SQL

  1. CREATE TABLE Product (
  2. id INTEGER NOT NULL ,
  3. name VARCHAR(255) ,
  4. warranty nclob ,
  5. PRIMARY KEY ( id )
  6. )

Hibernate can map the NCLOB to a java.sql.NClob

Example 49. NCLOB mapped to java.sql.NClob

  1. @Entity(name = "Product")
  2. public static class Product {
  3. @Id
  4. private Integer id;
  5. private String name;
  6. @Lob
  7. @Nationalized
  8. // Clob also works, because NClob extends Clob.
  9. // The database type is still NCLOB either way and handled as such.
  10. private NClob warranty;
  11. //Getters and setters are omitted for brevity
  12. }

To persist such an entity, you have to create an NClob using the NClobProxy Hibernate utility:

Example 50. Persisting a java.sql.NClob entity

  1. String warranty = "My product warranty";
  2. final Product product = new Product();
  3. product.setId( 1 );
  4. product.setName( "Mobile phone" );
  5. product.setWarranty( NClobProxy.generateProxy( warranty ) );
  6. entityManager.persist( product );

To retrieve the NClob content, you need to transform the underlying java.io.Reader:

Example 51. Returning a java.sql.NClob entity

  1. Product product = entityManager.find( Product.class, productId );
  2. try (Reader reader = product.getWarranty().getCharacterStream()) {
  3. assertEquals( "My product warranty", toString( reader ) );
  4. }

We could also map the NCLOB in a materialized form. This way, we can either use a String or a char[].

Example 52. NCLOB mapped to String

  1. @Entity(name = "Product")
  2. public static class Product {
  3. @Id
  4. private Integer id;
  5. private String name;
  6. @Lob
  7. @Nationalized
  8. private String warranty;
  9. //Getters and setters are omitted for brevity
  10. }

We might even want the materialized data as a char array.

Example 53. NCLOB - materialized char[] mapping

  1. @Entity(name = "Product")
  2. public static class Product {
  3. @Id
  4. private Integer id;
  5. private String name;
  6. @Lob
  7. @Nationalized
  8. private char[] warranty;
  9. //Getters and setters are omitted for brevity
  10. }

If your application and database use nationalization, you may instead want to enable nationalized character data as the default.

You can do this via the hibernate.use_nationalized_character_data setting or by calling MetadataBuilder#enableGlobalNationalizedCharacterDataSupport during bootstrap.

2.3.10. Mapping UUID Values

Hibernate also allows you to map UUID values, again in a number of ways.

The default UUID mapping is the binary one because it uses a more efficient column storage.

However, many applications prefer the readability of the character-based column storage. To switch the default mapping, simply call MetadataBuilder.applyBasicType( UUIDCharType.INSTANCE, UUID.class.getName() ).

2.3.11. UUID as binary

As mentioned, the default mapping for UUID attributes. Maps the UUID to a byte[] using java.util.UUID#getMostSignificantBits and java.util.UUID#getLeastSignificantBits and stores that as BINARY data.

Chosen as the default simply because it is generally more efficient from a storage perspective.

2.3.12. UUID as (var)char

Maps the UUID to a String using java.util.UUID#toString and java.util.UUID#fromString and stores that as CHAR or VARCHAR data.

2.3.13. PostgreSQL-specific UUID

When using one of the PostgreSQL Dialects, the PostgreSQL-specific UUID Hibernate type becomes the default UUID mapping.

Maps the UUID using the PostgreSQL-specific UUID data type. The PostgreSQL JDBC driver chooses to map its UUID type to the OTHER code. Note that this can cause difficulty as the driver chooses to map many different data types to OTHER.

2.3.14. UUID as identifier

Hibernate supports using UUID values as identifiers, and they can even be generated on the user’s behalf. For details, see the discussion of generators in Identifiers.

2.3.15. Mapping Date/Time Values

Hibernate allows various Java Date/Time classes to be mapped as persistent domain model entity properties. The SQL standard defines three Date/Time types:

DATE

Represents a calendar date by storing years, months and days. The JDBC equivalent is java.sql.Date

TIME

Represents the time of a day and it stores hours, minutes and seconds. The JDBC equivalent is java.sql.Time

TIMESTAMP

It stores both a DATE and a TIME plus nanoseconds. The JDBC equivalent is java.sql.Timestamp

To avoid dependencies on the java.sql package, it’s common to use the java.util or java.time Date/Time classes instead of the java.sql.Timestamp and java.sql.Time ones.

While the java.sql classes define a direct association to the SQL Date/Time data types, the java.util or java.time properties need to explicitly mark the SQL type correlation with the @Temporal annotation. This way, a java.util.Date or a java.util.Calendar can be mapped to either an SQL DATE, TIME or TIMESTAMP type.

Considering the following entity:

Example 54. java.util.Date mapped as DATE

  1. @Entity(name = "DateEvent")
  2. public static class DateEvent {
  3. @Id
  4. @GeneratedValue
  5. private Long id;
  6. @Column(name = "`timestamp`")
  7. @Temporal(TemporalType.DATE)
  8. private Date timestamp;
  9. //Getters and setters are omitted for brevity
  10. }

When persisting such entity:

Example 55. Persisting a java.util.Date mapping

  1. DateEvent dateEvent = new DateEvent( new Date() );
  2. entityManager.persist( dateEvent );

Hibernate generates the following INSERT statement:

  1. INSERT INTO DateEvent ( timestamp, id )
  2. VALUES ( '2015-12-29', 1 )

Only the year, month and the day fields were saved into the database.

If we change the @Temporal type to TIME:

Example 56. java.util.Date mapped as TIME

  1. @Column(name = "`timestamp`")
  2. @Temporal(TemporalType.TIME)
  3. private Date timestamp;

Hibernate will issue an INSERT statement containing the hour, minutes and seconds.

  1. INSERT INTO DateEvent ( timestamp, id )
  2. VALUES ( '16:51:58', 1 )

When the @Temporal type is set to TIMESTAMP:

Example 57. java.util.Date mapped as TIMESTAMP

  1. @Column(name = "`timestamp`")
  2. @Temporal(TemporalType.TIMESTAMP)
  3. private Date timestamp;

Hibernate will include both the DATE, the TIME and the nanoseconds in the INSERT statement:

  1. INSERT INTO DateEvent ( timestamp, id )
  2. VALUES ( '2015-12-29 16:54:04.544', 1 )

Just like the java.util.Date, the java.util.Calendar requires the @Temporal annotation in order to know what JDBC data type to be chosen: DATE, TIME or TIMESTAMP.

If the java.util.Date marks a point in time, the java.util.Calendar takes into consideration the default Time Zone.

Mapping Java 8 Date/Time Values

Java 8 came with a new Date/Time API, offering support for instant dates, intervals, local and zoned Date/Time immutable instances, bundled in the java.time package.

The mapping between the standard SQL Date/Time types and the supported Java 8 Date/Time class types looks as follows;

DATE

java.time.LocalDate

TIME

java.time.LocalTime, java.time.OffsetTime

TIMESTAMP

java.time.Instant, java.time.LocalDateTime, java.time.OffsetDateTime and java.time.ZonedDateTime

Because the mapping between the Java 8 Date/Time classes and the SQL types is implicit, there is not need to specify the @Temporal annotation.

Setting it on the java.time classes throws the following exception:

  1. org.hibernate.AnnotationException: @Temporal should only be set on a java.util.Date or java.util.Calendar property
Using a specific time zone

By default, Hibernate is going to use the PreparedStatement.setTimestamp(int parameterIndex, java.sql.Timestamp) or PreparedStatement.setTime(int parameterIndex, java.sql.Time x) when saving a java.sql.Timestamp or a java.sql.Time property.

When the time zone is not specified, the JDBC driver is going to use the underlying JVM default time zone, which might not be suitable if the application is used from all across the globe. For this reason, it is very common to use a single reference time zone (e.g. UTC) whenever saving/loading data from the database.

One alternative would be to configure all JVMs to use the reference time zone:

Declaratively

  1. java -Duser.timezone=UTC ...

Programmatically

  1. TimeZone.setDefault( TimeZone.getTimeZone( "UTC" ) );

However, as explained in this article, this is not always practical, especially for front-end nodes. For this reason, Hibernate offers the hibernate.jdbc.time_zone configuration property which can be configured:

Declaratively, at the SessionFactory level

  1. settings.put(
  2. AvailableSettings.JDBC_TIME_ZONE,
  3. TimeZone.getTimeZone( "UTC" )
  4. );

Programmatically, on a per Session basis

  1. Session session = sessionFactory()
  2. .withOptions()
  3. .jdbcTimeZone( TimeZone.getTimeZone( "UTC" ) )
  4. .openSession();

With this configuration property in place, Hibernate is going to call the PreparedStatement.setTimestamp(int parameterIndex, java.sql.Timestamp, Calendar cal) or PreparedStatement.setTime(int parameterIndex, java.sql.Time x, Calendar cal), where the java.util.Calendar references the time zone provided via the hibernate.jdbc.time_zone property.

2.3.16. JPA 2.1 AttributeConverters

Although Hibernate has long been offering custom types, as a JPA 2.1 provider, it also supports AttributeConverter as well.

With a custom AttributeConverter, the application developer can map a given JDBC type to an entity basic type.

In the following example, the java.time.Period is going to be mapped to a VARCHAR database column.

Example 58. java.time.Period custom AttributeConverter

  1. @Converter
  2. public class PeriodStringConverter
  3. implements AttributeConverter<Period, String> {
  4. @Override
  5. public String convertToDatabaseColumn(Period attribute) {
  6. return attribute.toString();
  7. }
  8. @Override
  9. public Period convertToEntityAttribute(String dbData) {
  10. return Period.parse( dbData );
  11. }
  12. }

To make use of this custom converter, the @Convert annotation must decorate the entity attribute.

Example 59. Entity using the custom java.time.Period AttributeConverter mapping

  1. @Entity(name = "Event")
  2. public static class Event {
  3. @Id
  4. @GeneratedValue
  5. private Long id;
  6. @Convert(converter = PeriodStringConverter.class)
  7. @Column(columnDefinition = "")
  8. private Period span;
  9. //Getters and setters are omitted for brevity
  10. }

When persisting such entity, Hibernate will do the type conversion based on the AttributeConverter logic:

Example 60. Persisting entity using the custom AttributeConverter

  1. INSERT INTO Event ( span, id )
  2. VALUES ( 'P1Y2M3D', 1 )
AttributeConverter Java and JDBC types

In cases when the Java type specified for the “database side” of the conversion (the second AttributeConverter bind parameter) is not known, Hibernate will fallback to a java.io.Serializable type.

If the Java type is not known to Hibernate, you will encounter the following message:

HHH000481: Encountered Java type for which we could not locate a JavaTypeDescriptor and which does not appear to implement equals and/or hashCode. This can lead to significant performance problems when performing equality/dirty checking involving this Java type. Consider registering a custom JavaTypeDescriptor or at least implementing equals/hashCode.

Whether a Java type is “known” means it has an entry in the JavaTypeDescriptorRegistry. While by default Hibernate loads many JDK types into the JavaTypeDescriptorRegistry, an application can also expand the JavaTypeDescriptorRegistry by adding new JavaTypeDescriptor entries.

This way, Hibernate will also know how to handle a specific Java Object type at the JDBC level.

JPA 2.1 AttributeConverter Mutability Plan

A basic type that’s converted by a JPA AttributeConverter is immutable if the underlying Java type is immutable and is mutable if the associated attribute type is mutable as well.

Therefore, mutability is given by the JavaTypeDescriptor#getMutabilityPlan of the associated entity attribute type.

Immutable types

If the entity attribute is a String, a primitive wrapper (e.g. Integer, Long) an Enum type, or any other immutable Object type, then you can only change the entity attribute value by reassigning it to a new value.

Considering we have the same Period entity attribute as illustrated in the JPA 2.1 AttributeConverters section:

  1. @Entity(name = "Event")
  2. public static class Event {
  3. @Id
  4. @GeneratedValue
  5. private Long id;
  6. @Convert(converter = PeriodStringConverter.class)
  7. @Column(columnDefinition = "")
  8. private Period span;
  9. //Getters and setters are omitted for brevity
  10. }

The only way to change the span attribute is to reassign it to a different value:

  1. Event event = entityManager.createQuery( "from Event", Event.class ).getSingleResult();
  2. event.setSpan(Period
  3. .ofYears( 3 )
  4. .plusMonths( 2 )
  5. .plusDays( 1 )
  6. );
Mutable types

On the other hand, consider the following example where the Money type is a mutable.

  1. public static class Money {
  2. private long cents;
  3. //Getters and setters are omitted for brevity
  4. }
  5. @Entity(name = "Account")
  6. public static class Account {
  7. @Id
  8. private Long id;
  9. private String owner;
  10. @Convert(converter = MoneyConverter.class)
  11. private Money balance;
  12. //Getters and setters are omitted for brevity
  13. }
  14. public static class MoneyConverter
  15. implements AttributeConverter<Money, Long> {
  16. @Override
  17. public Long convertToDatabaseColumn(Money attribute) {
  18. return attribute == null ? null : attribute.getCents();
  19. }
  20. @Override
  21. public Money convertToEntityAttribute(Long dbData) {
  22. return dbData == null ? null : new Money( dbData );
  23. }
  24. }

A mutable Object allows you to modify its internal structure, and Hibernate dirty checking mechanism is going to propagate the change to the database:

  1. Account account = entityManager.find( Account.class, 1L );
  2. account.getBalance().setCents( 150 * 100L );
  3. entityManager.persist( account );

Although the AttributeConverter types can be mutable so that dirty checking, deep copying, and second-level caching work properly, treating these as immutable (when they really are) is more efficient.

For this reason, prefer immutable types over mutable ones whenever possible.

2.3.17. SQL quoted identifiers

You can force Hibernate to quote an identifier in the generated SQL by enclosing the table or column name in backticks in the mapping document. While traditionally, Hibernate used backticks for escaping SQL reserved keywords, JPA uses double quotes instead.

Once the reserved keywords are escaped, Hibernate will use the correct quotation style for the SQL Dialect. This is usually double quotes, but SQL Server uses brackets and MySQL uses backticks.

Example 61. Hibernate legacy quoting

  1. @Entity(name = "Product")
  2. public static class Product {
  3. @Id
  4. private Long id;
  5. @Column(name = "`name`")
  6. private String name;
  7. @Column(name = "`number`")
  8. private String number;
  9. //Getters and setters are omitted for brevity
  10. }

Example 62. JPA quoting

  1. @Entity(name = "Product")
  2. public static class Product {
  3. @Id
  4. private Long id;
  5. @Column(name = "\"name\"")
  6. private String name;
  7. @Column(name = "\"number\"")
  8. private String number;
  9. //Getters and setters are omitted for brevity
  10. }

Because name and number are reserved words, the Product entity mapping uses backticks to quote these column names.

When saving the following Product entity, Hibernate generates the following SQL insert statement:

Example 63. Persisting a quoted column name

  1. Product product = new Product();
  2. product.setId( 1L );
  3. product.setName( "Mobile phone" );
  4. product.setNumber( "123-456-7890" );
  5. entityManager.persist( product );
  1. INSERT INTO Product ("name", "number", id)
  2. VALUES ('Mobile phone', '123-456-7890', 1)
Global quoting

Hibernate can also quote all identifiers (e.g. table, columns) using the following configuration property:

  1. <property
  2. name="hibernate.globally_quoted_identifiers"
  3. value="true"
  4. />

This way, we don’t need to manually quote any identifier:

Example 64. JPA quoting

  1. @Entity(name = "Product")
  2. public static class Product {
  3. @Id
  4. private Long id;
  5. private String name;
  6. private String number;
  7. //Getters and setters are omitted for brevity
  8. }

When persisting a Product entity, Hibernate is going to quote all identifiers as in the following example:

  1. INSERT INTO "Product" ("name", "number", "id")
  2. VALUES ('Mobile phone', '123-456-7890', 1)

As you can see, both the table name and all the column have been quoted.

For more about quoting-related configuration properties, check out the Mapping configurations section as well.

2.3.18. Generated properties

Generated properties are properties that have their values generated by the database. Typically, Hibernate applications needed to refresh objects that contain any properties for which the database was generating values. Marking properties as generated, however, lets the application delegate this responsibility to Hibernate. When Hibernate issues an SQL INSERT or UPDATE for an entity that has defined generated properties, it immediately issues a select to retrieve the generated values.

Properties marked as generated must additionally be non-insertable and non-updateable. Only @Version and @Basic types can be marked as generated.

NEVER (the default)

the given property value is not generated within the database.

INSERT

the given property value is generated on insert but is not regenerated on subsequent updates. Properties like creationTimestamp fall into this category.

ALWAYS

the property value is generated both on insert and update.

To mark a property as generated, use The Hibernate specific @Generated annotation.

@Generated annotation

The @Generated annotation is used so that Hibernate can fetch the currently annotated property after the entity has been persisted or updated. For this reason, the @Generated annotation accepts a GenerationTime enum value.

Considering the following entity:

Example 65. @Generated mapping example

  1. @Entity(name = "Person")
  2. public static class Person {
  3. @Id
  4. private Long id;
  5. private String firstName;
  6. private String lastName;
  7. private String middleName1;
  8. private String middleName2;
  9. private String middleName3;
  10. private String middleName4;
  11. private String middleName5;
  12. @Generated( value = GenerationTime.ALWAYS )
  13. @Column(columnDefinition =
  14. "AS CONCAT(" +
  15. " COALESCE(firstName, ''), " +
  16. " COALESCE(' ' + middleName1, ''), " +
  17. " COALESCE(' ' + middleName2, ''), " +
  18. " COALESCE(' ' + middleName3, ''), " +
  19. " COALESCE(' ' + middleName4, ''), " +
  20. " COALESCE(' ' + middleName5, ''), " +
  21. " COALESCE(' ' + lastName, '') " +
  22. ")")
  23. private String fullName;
  24. }

When the Person entity is persisted, Hibernate is going to fetch the calculated fullName column from the database, which concatenates the first, middle, and last name.

Example 66. @Generated persist example

  1. Person person = new Person();
  2. person.setId( 1L );
  3. person.setFirstName( "John" );
  4. person.setMiddleName1( "Flávio" );
  5. person.setMiddleName2( "André" );
  6. person.setMiddleName3( "Frederico" );
  7. person.setMiddleName4( "Rúben" );
  8. person.setMiddleName5( "Artur" );
  9. person.setLastName( "Doe" );
  10. entityManager.persist( person );
  11. entityManager.flush();
  12. assertEquals("John Flávio André Frederico Rúben Artur Doe", person.getFullName());
  1. INSERT INTO Person
  2. (
  3. firstName,
  4. lastName,
  5. middleName1,
  6. middleName2,
  7. middleName3,
  8. middleName4,
  9. middleName5,
  10. id
  11. )
  12. values
  13. (?, ?, ?, ?, ?, ?, ?, ?)
  14. -- binding parameter [1] as [VARCHAR] - [John]
  15. -- binding parameter [2] as [VARCHAR] - [Doe]
  16. -- binding parameter [3] as [VARCHAR] - [Flávio]
  17. -- binding parameter [4] as [VARCHAR] - [André]
  18. -- binding parameter [5] as [VARCHAR] - [Frederico]
  19. -- binding parameter [6] as [VARCHAR] - [Rúben]
  20. -- binding parameter [7] as [VARCHAR] - [Artur]
  21. -- binding parameter [8] as [BIGINT] - [1]
  22. SELECT
  23. p.fullName as fullName3_0_
  24. FROM
  25. Person p
  26. WHERE
  27. p.id=?
  28. -- binding parameter [1] as [BIGINT] - [1]
  29. -- extracted value ([fullName3_0_] : [VARCHAR]) - [John Flávio André Frederico Rúben Artur Doe]

The same goes when the Person entity is updated. Hibernate is going to fetch the calculated fullName column from the database after the entity is modified.

Example 67. @Generated update example

  1. Person person = entityManager.find( Person.class, 1L );
  2. person.setLastName( "Doe Jr" );
  3. entityManager.flush();
  4. assertEquals("John Flávio André Frederico Rúben Artur Doe Jr", person.getFullName());
  1. UPDATE
  2. Person
  3. SET
  4. firstName=?,
  5. lastName=?,
  6. middleName1=?,
  7. middleName2=?,
  8. middleName3=?,
  9. middleName4=?,
  10. middleName5=?
  11. WHERE
  12. id=?
  13. -- binding parameter [1] as [VARCHAR] - [John]
  14. -- binding parameter [2] as [VARCHAR] - [Doe Jr]
  15. -- binding parameter [3] as [VARCHAR] - [Flávio]
  16. -- binding parameter [4] as [VARCHAR] - [André]
  17. -- binding parameter [5] as [VARCHAR] - [Frederico]
  18. -- binding parameter [6] as [VARCHAR] - [Rúben]
  19. -- binding parameter [7] as [VARCHAR] - [Artur]
  20. -- binding parameter [8] as [BIGINT] - [1]
  21. SELECT
  22. p.fullName as fullName3_0_
  23. FROM
  24. Person p
  25. WHERE
  26. p.id=?
  27. -- binding parameter [1] as [BIGINT] - [1]
  28. -- extracted value ([fullName3_0_] : [VARCHAR]) - [John Flávio André Frederico Rúben Artur Doe Jr]
@GeneratorType annotation

The @GeneratorType annotation is used so that you can provide a custom generator to set the value of the currently annotated property.

For this reason, the @GeneratorType annotation accepts a GenerationTime enum value and a custom ValueGenerator class type.

Considering the following entity:

Example 68. @GeneratorType mapping example

  1. public static class CurrentUser {
  2. public static final CurrentUser INSTANCE = new CurrentUser();
  3. private static final ThreadLocal<String> storage = new ThreadLocal<>();
  4. public void logIn(String user) {
  5. storage.set( user );
  6. }
  7. public void logOut() {
  8. storage.remove();
  9. }
  10. public String get() {
  11. return storage.get();
  12. }
  13. }
  14. public static class LoggedUserGenerator implements ValueGenerator<String> {
  15. @Override
  16. public String generateValue(
  17. Session session, Object owner) {
  18. return CurrentUser.INSTANCE.get();
  19. }
  20. }
  21. @Entity(name = "Person")
  22. public static class Person {
  23. @Id
  24. private Long id;
  25. private String firstName;
  26. private String lastName;
  27. @GeneratorType( type = LoggedUserGenerator.class, when = GenerationTime.INSERT)
  28. private String createdBy;
  29. @GeneratorType( type = LoggedUserGenerator.class, when = GenerationTime.ALWAYS)
  30. private String updatedBy;
  31. }

When the Person entity is persisted, Hibernate is going to populate the createdBy column with the currently logged user.

Example 69. @Generated persist example

  1. CurrentUser.INSTANCE.logIn( "Alice" );
  2. doInJPA( this::entityManagerFactory, entityManager -> {
  3. Person person = new Person();
  4. person.setId( 1L );
  5. person.setFirstName( "John" );
  6. person.setLastName( "Doe" );
  7. entityManager.persist( person );
  8. } );
  9. CurrentUser.INSTANCE.logOut();
  1. INSERT INTO Person
  2. (
  3. createdBy,
  4. firstName,
  5. lastName,
  6. updatedBy,
  7. id
  8. )
  9. VALUES
  10. (?, ?, ?, ?, ?)
  11. -- binding parameter [1] as [VARCHAR] - [Alice]
  12. -- binding parameter [2] as [VARCHAR] - [John]
  13. -- binding parameter [3] as [VARCHAR] - [Doe]
  14. -- binding parameter [4] as [VARCHAR] - [Alice]
  15. -- binding parameter [5] as [BIGINT] - [1]

The same goes when the Person entity is updated. Hibernate is going to populate the updatedBy column with the currently logged user.

Example 70. @Generated update example

  1. CurrentUser.INSTANCE.logIn( "Bob" );
  2. doInJPA( this::entityManagerFactory, entityManager -> {
  3. Person person = entityManager.find( Person.class, 1L );
  4. person.setFirstName( "Mr. John" );
  5. } );
  6. CurrentUser.INSTANCE.logOut();
  1. UPDATE Person
  2. SET
  3. createdBy = ?,
  4. firstName = ?,
  5. lastName = ?,
  6. updatedBy = ?
  7. WHERE
  8. id = ?
  9. -- binding parameter [1] as [VARCHAR] - [Alice]
  10. -- binding parameter [2] as [VARCHAR] - [Mr. John]
  11. -- binding parameter [3] as [VARCHAR] - [Doe]
  12. -- binding parameter [4] as [VARCHAR] - [Bob]
  13. -- binding parameter [5] as [BIGINT] - [1]
@CreationTimestamp annotation

The @CreationTimestamp annotation instructs Hibernate to set the annotated entity attribute with the current timestamp value of the JVM when the entity is being persisted.

The supported property types are:

  • java.util.Date

  • java.util.Calendar

  • java.sql.Date

  • java.sql.Time

  • java.sql.Timestamp

Example 71. @CreationTimestamp mapping example

  1. @Entity(name = "Event")
  2. public static class Event {
  3. @Id
  4. @GeneratedValue
  5. private Long id;
  6. @Column(name = "`timestamp`")
  7. @CreationTimestamp
  8. private Date timestamp;
  9. //Constructors, getters, and setters are omitted for brevity
  10. }

When the Event entity is persisted, Hibernate is going to populate the underlying timestamp column with the current JVM timestamp value:

Example 72. @CreationTimestamp persist example

  1. Event dateEvent = new Event( );
  2. entityManager.persist( dateEvent );
  1. INSERT INTO Event ("timestamp", id)
  2. VALUES (?, ?)
  3. -- binding parameter [1] as [TIMESTAMP] - [Tue Nov 15 16:24:20 EET 2016]
  4. -- binding parameter [2] as [BIGINT] - [1]
@UpdateTimestamp annotation

The @UpdateTimestamp annotation instructs Hibernate to set the annotated entity attribute with the current timestamp value of the JVM when the entity is being persisted.

The supported property types are:

  • java.util.Date

  • java.util.Calendar

  • java.sql.Date

  • java.sql.Time

  • java.sql.Timestamp

Example 73. @UpdateTimestamp mapping example

  1. @Entity(name = "Bid")
  2. public static class Bid {
  3. @Id
  4. @GeneratedValue
  5. private Long id;
  6. @Column(name = "updated_on")
  7. @UpdateTimestamp
  8. private Date updatedOn;
  9. @Column(name = "updated_by")
  10. private String updatedBy;
  11. private Long cents;
  12. //Getters and setters are omitted for brevity
  13. }

When the Bid entity is persisted, Hibernate is going to populate the underlying updated_on column with the current JVM timestamp value:

Example 74. @UpdateTimestamp persist example

  1. Bid bid = new Bid();
  2. bid.setUpdatedBy( "John Doe" );
  3. bid.setCents( 150 * 100L );
  4. entityManager.persist( bid );
  1. INSERT INTO Bid (cents, updated_by, updated_on, id)
  2. VALUES (?, ?, ?, ?)
  3. -- binding parameter [1] as [BIGINT] - [15000]
  4. -- binding parameter [2] as [VARCHAR] - [John Doe]
  5. -- binding parameter [3] as [TIMESTAMP] - [Tue Apr 18 17:21:46 EEST 2017]
  6. -- binding parameter [4] as [BIGINT] - [1]

When updating the Bid entity, Hibernate is going to modify the updated_on column with the current JVM timestamp value:

Example 75. @UpdateTimestamp update example

  1. Bid bid = entityManager.find( Bid.class, 1L );
  2. bid.setUpdatedBy( "John Doe Jr." );
  3. bid.setCents( 160 * 100L );
  4. entityManager.persist( bid );
  1. UPDATE Bid SET
  2. cents = ?,
  3. updated_by = ?,
  4. updated_on = ?
  5. where
  6. id = ?
  7. -- binding parameter [1] as [BIGINT] - [16000]
  8. -- binding parameter [2] as [VARCHAR] - [John Doe Jr.]
  9. -- binding parameter [3] as [TIMESTAMP] - [Tue Apr 18 17:49:24 EEST 2017]
  10. -- binding parameter [4] as [BIGINT] - [1]
@ValueGenerationType meta-annotation

Hibernate 4.3 introduced the @ValueGenerationType meta-annotation, which is a new approach to declaring generated attributes or customizing generators.

@Generated has been retrofitted to use the @ValueGenerationType meta-annotation. But @ValueGenerationType exposes more features than what @Generated currently supports, and, to leverage some of those features, you’d simply wire up a new generator annotation.

As you’ll see in the following examples, the @ValueGenerationType meta-annotation is used when declaring the custom annotation used to mark the entity properties that need a specific generation strategy. The actual generation logic must be added to the class that implements the AnnotationValueGeneration interface.

Database-generated values

For example, let’s say we want the timestamps to be generated by calls to the standard ANSI SQL function current_timestamp (rather than triggers or DEFAULT values):

Example 76. A ValueGenerationType mapping for database generation

  1. @Entity(name = "Event")
  2. public static class Event {
  3. @Id
  4. @GeneratedValue
  5. private Long id;
  6. @Column(name = "`timestamp`")
  7. @FunctionCreationTimestamp
  8. private Date timestamp;
  9. //Constructors, getters, and setters are omitted for brevity
  10. }
  11. @ValueGenerationType(generatedBy = FunctionCreationValueGeneration.class)
  12. @Retention(RetentionPolicy.RUNTIME)
  13. public @interface FunctionCreationTimestamp {}
  14. public static class FunctionCreationValueGeneration
  15. implements AnnotationValueGeneration<FunctionCreationTimestamp> {
  16. @Override
  17. public void initialize(FunctionCreationTimestamp annotation, Class<?> propertyType) {
  18. }
  19. /**
  20. * Generate value on INSERT
  21. * @return when to generate the value
  22. */
  23. public GenerationTiming getGenerationTiming() {
  24. return GenerationTiming.INSERT;
  25. }
  26. /**
  27. * Returns null because the value is generated by the database.
  28. * @return null
  29. */
  30. public ValueGenerator<?> getValueGenerator() {
  31. return null;
  32. }
  33. /**
  34. * Returns true because the value is generated by the database.
  35. * @return true
  36. */
  37. public boolean referenceColumnInSql() {
  38. return true;
  39. }
  40. /**
  41. * Returns the database-generated value
  42. * @return database-generated value
  43. */
  44. public String getDatabaseGeneratedReferencedColumnValue() {
  45. return "current_timestamp";
  46. }
  47. }

When persisting an Event entity, Hibernate generates the following SQL statement:

  1. INSERT INTO Event ("timestamp", id)
  2. VALUES (current_timestamp, 1)

As you can see, the current_timestamp value was used for assigning the timestamp column value.

In-memory-generated values

If the timestamp value needs to be generated in-memory, the following mapping must be used instead:

Example 77. A ValueGenerationType mapping for in-memory value generation

  1. @Entity(name = "Event")
  2. public static class Event {
  3. @Id
  4. @GeneratedValue
  5. private Long id;
  6. @Column(name = "`timestamp`")
  7. @FunctionCreationTimestamp
  8. private Date timestamp;
  9. //Constructors, getters, and setters are omitted for brevity
  10. }
  11. @ValueGenerationType(generatedBy = FunctionCreationValueGeneration.class)
  12. @Retention(RetentionPolicy.RUNTIME)
  13. public @interface FunctionCreationTimestamp {}
  14. public static class FunctionCreationValueGeneration
  15. implements AnnotationValueGeneration<FunctionCreationTimestamp> {
  16. @Override
  17. public void initialize(FunctionCreationTimestamp annotation, Class<?> propertyType) {
  18. }
  19. /**
  20. * Generate value on INSERT
  21. * @return when to generate the value
  22. */
  23. public GenerationTiming getGenerationTiming() {
  24. return GenerationTiming.INSERT;
  25. }
  26. /**
  27. * Returns the in-memory generated value
  28. * @return {@code true}
  29. */
  30. public ValueGenerator<?> getValueGenerator() {
  31. return (session, owner) -> new Date( );
  32. }
  33. /**
  34. * Returns false because the value is generated by the database.
  35. * @return false
  36. */
  37. public boolean referenceColumnInSql() {
  38. return false;
  39. }
  40. /**
  41. * Returns null because the value is generated in-memory.
  42. * @return null
  43. */
  44. public String getDatabaseGeneratedReferencedColumnValue() {
  45. return null;
  46. }
  47. }

When persisting an Event entity, Hibernate generates the following SQL statement:

  1. INSERT INTO Event ("timestamp", id)
  2. VALUES ('Tue Mar 01 10:58:18 EET 2016', 1)

As you can see, the new Date() object value was used for assigning the timestamp column value.

2.3.19. Column transformers: read and write expressions

Hibernate allows you to customize the SQL it uses to read and write the values of columns mapped to @Basic types. For example, if your database provides a set of data encryption functions, you can invoke them for individual columns like in the following example.

Example 78. @ColumnTransformer example

  1. @Entity(name = "Employee")
  2. public static class Employee {
  3. @Id
  4. private Long id;
  5. @NaturalId
  6. private String username;
  7. @Column(name = "pswd")
  8. @ColumnTransformer(
  9. read = "decrypt( 'AES', '00', pswd )",
  10. write = "encrypt('AES', '00', ?)"
  11. )
  12. private String password;
  13. private int accessLevel;
  14. @ManyToOne(fetch = FetchType.LAZY)
  15. private Department department;
  16. @ManyToMany(mappedBy = "employees")
  17. private List<Project> projects = new ArrayList<>();
  18. //Getters and setters omitted for brevity
  19. }

If a property uses more than one column, you must use the forColumn attribute to specify which column the @ColumnTransformer read and write expressions are targeting.

Example 79. @ColumnTransformer forColumn attribute usage

  1. @Entity(name = "Savings")
  2. public static class Savings {
  3. @Id
  4. private Long id;
  5. @Type(type = "org.hibernate.userguide.mapping.basic.MonetaryAmountUserType")
  6. @Columns(columns = {
  7. @Column(name = "money"),
  8. @Column(name = "currency")
  9. })
  10. @ColumnTransformer(
  11. forColumn = "money",
  12. read = "money / 100",
  13. write = "? * 100"
  14. )
  15. private MonetaryAmount wallet;
  16. //Getters and setters omitted for brevity
  17. }

Hibernate applies the custom expressions automatically whenever the property is referenced in a query. This functionality is similar to a derived-property @Formula with two differences:

  • The property is backed by one or more columns that are exported as part of automatic schema generation.

  • The property is read-write, not read-only.

The write expression, if specified, must contain exactly one ‘?’ placeholder for the value.

Example 80. Persisting an entity with a @ColumnTransformer and a composite type

  1. doInJPA( this::entityManagerFactory, entityManager -> {
  2. Savings savings = new Savings( );
  3. savings.setId( 1L );
  4. savings.setWallet( new MonetaryAmount( BigDecimal.TEN, Currency.getInstance( Locale.US ) ) );
  5. entityManager.persist( savings );
  6. } );
  7. doInJPA( this::entityManagerFactory, entityManager -> {
  8. Savings savings = entityManager.find( Savings.class, 1L );
  9. assertEquals( 10, savings.getWallet().getAmount().intValue());
  10. } );
  1. INSERT INTO Savings (money, currency, id)
  2. VALUES (10 * 100, 'USD', 1)
  3. SELECT
  4. s.id as id1_0_0_,
  5. s.money / 100 as money2_0_0_,
  6. s.currency as currency3_0_0_
  7. FROM
  8. Savings s
  9. WHERE
  10. s.id = 1

2.3.20. @Formula

Sometimes, you want the Database to do some computation for you rather than in the JVM, you might also create some kind of virtual column. You can use a SQL fragment (aka formula) instead of mapping a property into a column. This kind of property is read-only (its value is calculated by your formula fragment)

You should be aware that the @Formula annotation takes a native SQL clause which may affect database portability.

Example 81. @Formula mapping usage

  1. @Entity(name = "Account")
  2. public static class Account {
  3. @Id
  4. private Long id;
  5. private Double credit;
  6. private Double rate;
  7. @Formula(value = "credit * rate")
  8. private Double interest;
  9. //Getters and setters omitted for brevity
  10. }

When loading the Account entity, Hibernate is going to calculate the interest property using the configured @Formula:

Example 82. Persisting an entity with a @Formula mapping

  1. doInJPA( this::entityManagerFactory, entityManager -> {
  2. Account account = new Account( );
  3. account.setId( 1L );
  4. account.setCredit( 5000d );
  5. account.setRate( 1.25 / 100 );
  6. entityManager.persist( account );
  7. } );
  8. doInJPA( this::entityManagerFactory, entityManager -> {
  9. Account account = entityManager.find( Account.class, 1L );
  10. assertEquals( Double.valueOf( 62.5d ), account.getInterest());
  11. } );
  1. INSERT INTO Account (credit, rate, id)
  2. VALUES (5000.0, 0.0125, 1)
  3. SELECT
  4. a.id as id1_0_0_,
  5. a.credit as credit2_0_0_,
  6. a.rate as rate3_0_0_,
  7. a.credit * a.rate as formula0_0_
  8. FROM
  9. Account a
  10. WHERE
  11. a.id = 1

The SQL fragment defined by the @Formula annotation can be as complex as you want, and it can even include subselects.

2.4. Embeddable types

Historically Hibernate called these components. JPA calls them embeddables. Either way, the concept is the same: a composition of values.

For example, we might have a Publisher class that is a composition of name and country, or a Location class that is a composition of country and city.

Usage of the word embeddable

To avoid any confusion with the annotation that marks a given embeddable type, the annotation will be further referred to as @Embeddable.

Throughout this chapter and thereafter, for brevity sake, embeddable types may also be referred to as embeddable.

Example 83. Embeddable type example

  1. @Embeddable
  2. public static class Publisher {
  3. private String name;
  4. private Location location;
  5. public Publisher(String name, Location location) {
  6. this.name = name;
  7. this.location = location;
  8. }
  9. private Publisher() {}
  10. //Getters and setters are omitted for brevity
  11. }
  12. @Embeddable
  13. public static class Location {
  14. private String country;
  15. private String city;
  16. public Location(String country, String city) {
  17. this.country = country;
  18. this.city = city;
  19. }
  20. private Location() {}
  21. //Getters and setters are omitted for brevity
  22. }

An embeddable type is another form of a value type, and its lifecycle is bound to a parent entity type, therefore inheriting the attribute access from its parent (for details on attribute access, see Access strategies).

Embeddable types can be made up of basic values as well as associations, with the caveat that, when used as collection elements, they cannot define collections themselves.

2.4.1. Component / Embedded

Most often, embeddable types are used to group multiple basic type mappings and reuse them across several entities.

Example 84. Simple Embeddable

  1. @Entity(name = "Book")
  2. public static class Book {
  3. @Id
  4. @GeneratedValue
  5. private Long id;
  6. private String title;
  7. private String author;
  8. private Publisher publisher;
  9. //Getters and setters are omitted for brevity
  10. }
  11. @Embeddable
  12. public static class Publisher {
  13. @Column(name = "publisher_name")
  14. private String name;
  15. @Column(name = "publisher_country")
  16. private String country;
  17. //Getters and setters, equals and hashCode methods omitted for brevity
  18. }
  1. create table Book (
  2. id bigint not null,
  3. author varchar(255),
  4. publisher_country varchar(255),
  5. publisher_name varchar(255),
  6. title varchar(255),
  7. primary key (id)
  8. )

JPA defines two terms for working with an embeddable type: @Embeddable and @Embedded.

@Embeddable is used to describe the mapping type itself (e.g. Publisher).

@Embedded is for referencing a given embeddable type (e.g. book.publisher).

So, the embeddable type is represented by the Publisher class and the parent entity makes use of it through the book#publisher object composition.

The composed values are mapped to the same table as the parent table. Composition is part of good object-oriented data modeling (idiomatic Java). In fact, that table could also be mapped by the following entity type instead.

Example 85. Alternative to embeddable type composition

  1. @Entity(name = "Book")
  2. public static class Book {
  3. @Id
  4. @GeneratedValue
  5. private Long id;
  6. private String title;
  7. private String author;
  8. @Column(name = "publisher_name")
  9. private String publisherName;
  10. @Column(name = "publisher_country")
  11. private String publisherCountry;
  12. //Getters and setters are omitted for brevity
  13. }

The composition form is certainly more object-oriented, and that becomes more evident as we work with multiple embeddable types.

2.4.2. Multiple embeddable types

Although from an object-oriented perspective, it’s much more convenient to work with embeddable types, this example doesn’t work as-is. When the same embeddable type is included multiple times in the same parent entity type, the JPA specification demands to set the associated column names explicitly.

This requirement is due to how object properties are mapped to database columns. By default, JPA expects a database column having the same name with its associated object property. When including multiple embeddables, the implicit name-based mapping rule doesn’t work anymore because multiple object properties could end-up being mapped to the same database column.

We have a few options to handle this issue.

2.4.3. Overriding Embeddable types

JPA defines the @AttributeOverride annotation to handle this scenario. This way, the mapping conflict is resolved by setting up explicit name-based property-column type mappings.

If an Embeddable type is used multiple times in some entity, you need to use the @AttributeOverride and @AssociationOverride annotations to override the default column names defined by the Embeddable.

Considering you have the following Publisher embeddable type which defines a @ManyToOne association with the Country entity:

Example 86. Embeddable type with a @ManyToOne association

  1. @Embeddable
  2. public static class Publisher {
  3. private String name;
  4. @ManyToOne(fetch = FetchType.LAZY)
  5. private Country country;
  6. //Getters and setters, equals and hashCode methods omitted for brevity
  7. }
  8. @Entity(name = "Country")
  9. public static class Country {
  10. @Id
  11. @GeneratedValue
  12. private Long id;
  13. @NaturalId
  14. private String name;
  15. //Getters and setters are omitted for brevity
  16. }
  1. create table Country (
  2. id bigint not null,
  3. name varchar(255),
  4. primary key (id)
  5. )
  6. alter table Country
  7. add constraint UK_p1n05aafu73sbm3ggsxqeditd
  8. unique (name)

Now, if you have a Book entity which declares two Publisher embeddable types for the ebook and paperback versions, you cannot use the default Publisher embeddable mapping since there will be a conflict between the two embeddable column mappings.

Therefore, the Book entity needs to override the embeddable type mappings for each Publisher attribute:

Example 87. Overriding embeddable type attributes

  1. @Entity(name = "Book")
  2. @AttributeOverrides({
  3. @AttributeOverride(
  4. name = "ebookPublisher.name",
  5. column = @Column(name = "ebook_publisher_name")
  6. ),
  7. @AttributeOverride(
  8. name = "paperBackPublisher.name",
  9. column = @Column(name = "paper_back_publisher_name")
  10. )
  11. })
  12. @AssociationOverrides({
  13. @AssociationOverride(
  14. name = "ebookPublisher.country",
  15. joinColumns = @JoinColumn(name = "ebook_publisher_country_id")
  16. ),
  17. @AssociationOverride(
  18. name = "paperBackPublisher.country",
  19. joinColumns = @JoinColumn(name = "paper_back_publisher_country_id")
  20. )
  21. })
  22. public static class Book {
  23. @Id
  24. @GeneratedValue
  25. private Long id;
  26. private String title;
  27. private String author;
  28. private Publisher ebookPublisher;
  29. private Publisher paperBackPublisher;
  30. //Getters and setters are omitted for brevity
  31. }
  1. create table Book (
  2. id bigint not null,
  3. author varchar(255),
  4. ebook_publisher_name varchar(255),
  5. paper_back_publisher_name varchar(255),
  6. title varchar(255),
  7. ebook_publisher_country_id bigint,
  8. paper_back_publisher_country_id bigint,
  9. primary key (id)
  10. )
  11. alter table Book
  12. add constraint FKm39ibh5jstybnslaoojkbac2g
  13. foreign key (ebook_publisher_country_id)
  14. references Country
  15. alter table Book
  16. add constraint FK7kqy9da323p7jw7wvqgs6aek7
  17. foreign key (paper_back_publisher_country_id)
  18. references Country

2.4.4. Embeddables and ImplicitNamingStrategy

The ImplicitNamingStrategyComponentPathImpl is a Hibernate-specific feature. Users concerned with JPA provider portability should instead prefer explicit column naming with @AttributeOverride.

Hibernate naming strategies are covered in detail in Naming. However, for the purposes of this discussion, Hibernate has the capability to interpret implicit column names in a way that is safe for use with multiple embeddable types.

Example 88. Implicit multiple embeddable type mapping

  1. @Entity(name = "Book")
  2. public static class Book {
  3. @Id
  4. @GeneratedValue
  5. private Long id;
  6. private String title;
  7. private String author;
  8. private Publisher ebookPublisher;
  9. private Publisher paperBackPublisher;
  10. //Getters and setters are omitted for brevity
  11. }
  12. @Embeddable
  13. public static class Publisher {
  14. private String name;
  15. @ManyToOne(fetch = FetchType.LAZY)
  16. private Country country;
  17. //Getters and setters, equals and hashCode methods omitted for brevity
  18. }
  19. @Entity(name = "Country")
  20. public static class Country {
  21. @Id
  22. @GeneratedValue
  23. private Long id;
  24. @NaturalId
  25. private String name;
  26. //Getters and setters are omitted for brevity
  27. }

To make it work, you need to use the ImplicitNamingStrategyComponentPathImpl naming strategy.

Example 89. Enabling implicit embeddable type mapping using the component path naming strategy

  1. metadataBuilder.applyImplicitNamingStrategy(
  2. ImplicitNamingStrategyComponentPathImpl.INSTANCE
  3. );

Now the “path” to attributes are used in the implicit column naming:

  1. create table Book (
  2. id bigint not null,
  3. author varchar(255),
  4. ebookPublisher_name varchar(255),
  5. paperBackPublisher_name varchar(255),
  6. title varchar(255),
  7. ebookPublisher_country_id bigint,
  8. paperBackPublisher_country_id bigint,
  9. primary key (id)
  10. )

You could even develop your own naming strategy to do other types of implicit naming strategies.

2.4.5. Collections of embeddable types

Collections of embeddable types are specifically valued collections (as embeddable types are value types). Value collections are covered in detail in Collections of value types.

2.4.6. Embeddable type as a Map key

Embeddable types can also be used as Map keys. This topic is converted in detail in Map - key.

2.4.7. Embeddable type as identifier

Embeddable types can also be used as entity type identifiers. This usage is covered in detail in Composite identifiers.

Embeddable types that are used as collection entries, map keys or entity type identifiers cannot include their own collection mappings.

2.4.8. @Target mapping

The @Target annotation is used to specify the implementation class of a given association that is mapped via an interface. The @ManyToOne, @OneToOne, @OneToMany, and @ManyToMany feature a targetEntity attribute to specify the actual class of the entity association when an interface is used for the mapping.

The @ElementCollection association has a targetClass attribute for the same purpose.

However, for simple embeddable types, there is no such construct and so you need to use the Hibernate-specific @Target annotation instead.

Example 90. @Target mapping usage

  1. public interface Coordinates {
  2. double x();
  3. double y();
  4. }
  5. @Embeddable
  6. public static class GPS implements Coordinates {
  7. private double latitude;
  8. private double longitude;
  9. private GPS() {
  10. }
  11. public GPS(double latitude, double longitude) {
  12. this.latitude = latitude;
  13. this.longitude = longitude;
  14. }
  15. @Override
  16. public double x() {
  17. return latitude;
  18. }
  19. @Override
  20. public double y() {
  21. return longitude;
  22. }
  23. }
  24. @Entity(name = "City")
  25. public static class City {
  26. @Id
  27. @GeneratedValue
  28. private Long id;
  29. private String name;
  30. @Embedded
  31. @Target( GPS.class )
  32. private Coordinates coordinates;
  33. //Getters and setters omitted for brevity
  34. }

The coordinates embeddable type is mapped as the Coordinates interface. However, Hibernate needs to know the actual implementation tye, which is GPS in this case, hence the @Target annotation is used to provide this information.

Assuming we have persisted the following City entity:

Example 91. @Target persist example

  1. doInJPA( this::entityManagerFactory, entityManager -> {
  2. City cluj = new City();
  3. cluj.setName( "Cluj" );
  4. cluj.setCoordinates( new GPS( 46.77120, 23.62360 ) );
  5. entityManager.persist( cluj );
  6. } );

When fetching the City entity, the coordinates property is mapped by the @Target expression:

Example 92. @Target fetching example

  1. doInJPA( this::entityManagerFactory, entityManager -> {
  2. City cluj = entityManager.find( City.class, 1L );
  3. assertEquals( 46.77120, cluj.getCoordinates().x(), 0.00001 );
  4. assertEquals( 23.62360, cluj.getCoordinates().y(), 0.00001 );
  5. } );
  1. SELECT
  2. c.id as id1_0_0_,
  3. c.latitude as latitude2_0_0_,
  4. c.longitude as longitud3_0_0_,
  5. c.name as name4_0_0_
  6. FROM
  7. City c
  8. WHERE
  9. c.id = ?
  10. -- binding parameter [1] as [BIGINT] - [1]
  11. -- extracted value ([latitude2_0_0_] : [DOUBLE]) - [46.7712]
  12. -- extracted value ([longitud3_0_0_] : [DOUBLE]) - [23.6236]
  13. -- extracted value ([name4_0_0_] : [VARCHAR]) - [Cluj]

Therefore, the @Target annotation is used to define a custom join association between the parent-child association.

2.4.9. @Parent mapping

The Hibernate-specific @Parent annotation allows you to reference the owner entity from within an embeddable.

Example 93. @Parent mapping usage

  1. @Embeddable
  2. public static class GPS {
  3. private double latitude;
  4. private double longitude;
  5. @Parent
  6. private City city;
  7. //Getters and setters omitted for brevity
  8. }
  9. @Entity(name = "City")
  10. public static class City {
  11. @Id
  12. @GeneratedValue
  13. private Long id;
  14. private String name;
  15. @Embedded
  16. @Target( GPS.class )
  17. private GPS coordinates;
  18. //Getters and setters omitted for brevity
  19. }

Assuming we have persisted the following City entity:

Example 94. @Parent persist example

  1. doInJPA( this::entityManagerFactory, entityManager -> {
  2. City cluj = new City();
  3. cluj.setName( "Cluj" );
  4. cluj.setCoordinates( new GPS( 46.77120, 23.62360 ) );
  5. entityManager.persist( cluj );
  6. } );

When fetching the City entity, the city property of the embeddable type acts as a back reference to the owning parent entity:

Example 95. @Parent fetching example

  1. doInJPA( this::entityManagerFactory, entityManager -> {
  2. City cluj = entityManager.find( City.class, 1L );
  3. assertSame( cluj, cluj.getCoordinates().getCity() );
  4. } );

Therefore, the @Parent annotation is used to define the association between an embeddable type and the owning entity.

2.5. Entity types

Usage of the word entity

The entity type describes the mapping between the actual persistable domain model object and a database table row. To avoid any confusion with the annotation that marks a given entity type, the annotation will be further referred to as @Entity.

Throughout this chapter and thereafter, entity types will be simply referred to as entity.

2.5.1. POJO Models

Section 2.1 The Entity Class of the JPA 2.1 specification defines its requirements for an entity class. Applications that wish to remain portable across JPA providers should adhere to these requirements:

  • The entity class must be annotated with the javax.persistence.Entity annotation (or be denoted as such in XML mapping).

  • The entity class must have a public or protected no-argument constructor. It may define additional constructors as well.

  • The entity class must be a top-level class.

  • An enum or interface may not be designated as an entity.

  • The entity class must not be final. No methods or persistent instance variables of the entity class may be final.

  • If an entity instance is to be used remotely as a detached object, the entity class must implement the Serializable interface.

  • Both abstract and concrete classes can be entities. Entities may extend non-entity classes as well as entity classes, and non-entity classes may extend entity classes.

  • The persistent state of an entity is represented by instance variables, which may correspond to JavaBean-style properties. An instance variable must be directly accessed only from within the methods of the entity by the entity instance itself. The state of the entity is available to clients only through the entity’s accessor methods (getter/setter methods) or other business methods.

Hibernate, however, is not as strict in its requirements. The differences from the list above include:

  • The entity class must have a no-argument constructor, which may be public, protected or package visibility. It may define additional constructors as well.

  • The entity class need not be a top-level class.

  • Technically Hibernate can persist final classes or classes with final persistent state accessor (getter/setter) methods. However, it is generally not a good idea as doing so will stop Hibernate from being able to generate proxies for lazy-loading the entity.

  • Hibernate does not restrict the application developer from exposing instance variables and referencing them from outside the entity class itself. The validity of such a paradigm, however, is debatable at best.

Let’s look at each requirement in detail.

2.5.2. Prefer non-final classes

A central feature of Hibernate is the ability to load lazily certain entity instance variables (attributes) via runtime proxies. This feature depends upon the entity class being non-final or else implementing an interface that declares all the attribute getters/setters. You can still persist final classes that do not implement such an interface with Hibernate, but you will not be able to use proxies for fetching lazy associations, therefore limiting your options for performance tuning. For the very same reason, you should also avoid declaring persistent attribute getters and setters as final.

Starting with 5.0, Hibernate offers a more robust version of bytecode enhancement as another means for handling lazy loading. Hibernate had some bytecode re-writing capabilities prior to 5.0 but they were very rudimentary. See the Bytecode Enhancement for additional information on fetching and on bytecode enhancement.

2.5.3. Implement a no-argument constructor

The entity class should have a no-argument constructor. Both Hibernate and JPA require this.

JPA requires that this constructor be defined as public or protected. Hibernate, for the most part, does not care about the constructor visibility, as long as the system SecurityManager allows overriding the visibility setting. That said, the constructor should be defined with at least package visibility if you wish to leverage runtime proxy generation.

2.5.4. Declare getters and setters for persistent attributes

The JPA specification requires this, otherwise, the model would prevent accessing the entity persistent state fields directly from outside the entity itself.

Although Hibernate does not require it, it is recommended to follow the JavaBean conventions and define getters and setters for entity persistent attributes. Nevertheless, you can still tell Hibernate to directly access the entity fields.

Attributes (whether fields or getters/setters) need not be declared public. Hibernate can deal with attributes declared with the public, protected, package or private visibility. Again, if wanting to use runtime proxy generation for lazy loading, the getter/setter should grant access to at least package visibility.

2.5.5. Providing identifier attribute(s)

Historically, providing identifier attributes was considered optional.

However, not defining identifier attributes on the entity should be considered a deprecated feature that will be removed in an upcoming release.

The identifier attribute does not necessarily need to be mapped to the column(s) that physically define the primary key. However, it should map to column(s) that can uniquely identify each row.

We recommend that you declare consistently-named identifier attributes on persistent classes and that you use a wrapper (i.e., non-primitive) type (e.g. Long or Integer).

The placement of the @Id annotation marks the persistence state access strategy.

Example 96. Identifier mapping

  1. @Id
  2. private Long id;

Hibernate offers multiple identifier generation strategies, see the Identifier Generators chapter for more about this topic.

2.5.6. Mapping the entity

The main piece in mapping the entity is the javax.persistence.Entity annotation.

The @Entity annotation defines just the name attribute which is used to give a specific entity name for use in JPQL queries.

By default, if the name attribute of the @Entity annotation is missing, the unqualified name of the entity class itself will be used as the entity name.

Because the entity name is given by the unqualified name of the class, Hibernate does not allow registering multiple entities with the same name even if the entity classes reside in different packages.

Without imposing this restriction, Hibernate would not know which entity class is referenced in a JPQL query if the unqualified entity name is associated with more then one entity classes.

In the following example, the entity name (e.g. Book) is given by the unqualified name of the entity class name.

Example 97. @Entity mapping with an implicit name

  1. @Entity
  2. public class Book {
  3. @Id
  4. private Long id;
  5. private String title;
  6. private String author;
  7. //Getters and setters are omitted for brevity
  8. }

However, the entity name can also be set explicitly as illustrated by the following example.

Example 98. @Entity mapping with an explicit name

  1. @Entity(name = "Book")
  2. public static class Book {
  3. @Id
  4. private Long id;
  5. private String title;
  6. private String author;
  7. //Getters and setters are omitted for brevity
  8. }

An entity models a database table. The identifier uniquely identifies each row in that table. By default, the name of the table is assumed to be the same as the name of the entity. To explicitly give the name of the table or to specify other information about the table, we would use the javax.persistence.Table annotation.

Example 99. Simple @Entity with @Table

  1. @Entity(name = "Book")
  2. @Table(
  3. catalog = "public",
  4. schema = "store",
  5. name = "book"
  6. )
  7. public static class Book {
  8. @Id
  9. private Long id;
  10. private String title;
  11. private String author;
  12. //Getters and setters are omitted for brevity
  13. }
Mapping the catalog of the associated table

Without specifying the catalog of the associated database table a given entity is mapped to, Hibernate will use the default catalog associated with the current database connection.

However, if your database hosts multiple catalogs, you can specify the catalog where a given table is located using the catalog attribute of the JPA @Table annotation.

Let’s assume we are using MySQL and want to map a Book entity to the book table located in the public catalog which looks as follows.

Example 100. The book table located in the public catalog

  1. create table public.book (
  2. id bigint not null,
  3. author varchar(255),
  4. title varchar(255),
  5. primary key (id)
  6. ) engine=InnoDB

Now, to map the Book entity to the book table in the public catalog we can use the catalog attribute of the @Table JPA annotation.

Example 101. Specifying the database catalog using the @Table annotation

  1. @Entity(name = "Book")
  2. @Table(
  3. catalog = "public",
  4. name = "book"
  5. )
  6. public static class Book {
  7. @Id
  8. private Long id;
  9. private String title;
  10. private String author;
  11. //Getters and setters are omitted for brevity
  12. }
Mapping the schema of the associated table

Without specifying the schema of the associated database table a given entity is mapped to, Hibernate will use the default schema associated with the current database connection.

However, if your database supports schemas, you can specify the schema where a given table is located using the schema attribute of the JPA @Table annotation.

Let’s assume we are using PostgreSQL and want to map a Book entity to the book table located in the library schema which looks as follows.

Example 102. The book table located in the library schema

  1. create table library.book (
  2. id int8 not null,
  3. author varchar(255),
  4. title varchar(255),
  5. primary key (id)
  6. )

Now, to map the Book entity to the book table in the library schema we can use the schema attribute of the @Table JPA annotation.

Example 103. Specifying the database schema using the @Table annotation

  1. @Entity(name = "Book")
  2. @Table(
  3. schema = "library",
  4. name = "book"
  5. )
  6. public static class Book {
  7. @Id
  8. private Long id;
  9. private String title;
  10. private String author;
  11. //Getters and setters are omitted for brevity
  12. }

The schema attribute of the @Table annotation works only if the underlying database supports schemas (e.g. PostgreSQL).

Therefore, if you’re using MySQL or MariaDB, which do not support schemas natively (schemas being just an alias for catalog), you need to use the catalog attribute, and not the schema one.

2.5.7. Implementing equals() and hashCode()

Much of the discussion in this section deals with the relation of an entity to a Hibernate Session, whether the entity is managed, transient or detached. If you are unfamiliar with these topics, they are explained in the Persistence Context chapter.

Whether to implement equals() and hashCode() methods in your domain model, let alone how to implement them, is a surprisingly tricky discussion when it comes to ORM.

There is really just one absolute case: a class that acts as an identifier must implement equals/hashCode based on the id value(s). Generally, this is pertinent for user-defined classes used as composite identifiers. Beyond this one very specific use case and few others we will discuss below, you may want to consider not implementing equals/hashCode altogether.

So what’s all the fuss? Normally, most Java objects provide a built-in equals() and hashCode() based on the object’s identity, so each new object will be different from all others. This is generally what you want in ordinary Java programming. Conceptually, however, this starts to break down when you start to think about the possibility of multiple instances of a class representing the same data.

This is, in fact, exactly the case when dealing with data coming from a database. Every time we load a specific Person from the database we would naturally get a unique instance. Hibernate, however, works hard to make sure that does not happen within a given Session. In fact, Hibernate guarantees equivalence of persistent identity (database row) and Java identity inside a particular session scope. So if we ask a Hibernate Session to load that specific Person multiple times we will actually get back the same instance:

Example 104. Scope of identity

  1. Book book1 = entityManager.find( Book.class, 1L );
  2. Book book2 = entityManager.find( Book.class, 1L );
  3. assertTrue( book1 == book2 );

Consider we have a Library parent entity which contains a java.util.Set of Book entities:

Example 105. Library entity mapping

  1. @Entity(name = "Library")
  2. public static class Library {
  3. @Id
  4. private Long id;
  5. private String name;
  6. @OneToMany(cascade = CascadeType.ALL)
  7. @JoinColumn(name = "book_id")
  8. private Set<Book> books = new HashSet<>();
  9. //Getters and setters are omitted for brevity
  10. }

Example 106. Set usage with Session-scoped identity

  1. Library library = entityManager.find( Library.class, 1L );
  2. Book book1 = entityManager.find( Book.class, 1L );
  3. Book book2 = entityManager.find( Book.class, 1L );
  4. library.getBooks().add( book1 );
  5. library.getBooks().add( book2 );
  6. assertEquals( 1, library.getBooks().size() );

However, the semantic changes when we mix instances loaded from different Sessions:

Example 107. Mixed Sessions

  1. Book book1 = doInJPA( this::entityManagerFactory, entityManager -> {
  2. return entityManager.find( Book.class, 1L );
  3. } );
  4. Book book2 = doInJPA( this::entityManagerFactory, entityManager -> {
  5. return entityManager.find( Book.class, 1L );
  6. } );
  7. assertFalse( book1 == book2 );
  1. doInJPA( this::entityManagerFactory, entityManager -> {
  2. Set<Book> books = new HashSet<>();
  3. books.add( book1 );
  4. books.add( book2 );
  5. assertEquals( 2, books.size() );
  6. } );

Specifically, the outcome in this last example will depend on whether the Book class implemented equals/hashCode, and, if so, how.

If the Book class did not override the default equals/hashCode, then the two Book object references are not going to be equal since their references are different.

Consider yet another case:

Example 108. Sets with transient entities

  1. Library library = entityManager.find( Library.class, 1L );
  2. Book book1 = new Book();
  3. book1.setId( 100L );
  4. book1.setTitle( "High-Performance Java Persistence" );
  5. Book book2 = new Book();
  6. book2.setId( 101L );
  7. book2.setTitle( "Java Persistence with Hibernate" );
  8. library.getBooks().add( book1 );
  9. library.getBooks().add( book2 );
  10. assertEquals( 2, library.getBooks().size() );

In cases where you will be dealing with entities outside of a Session (whether they be transient or detached), especially in cases where you will be using them in Java collections, you should consider implementing equals/hashCode.

A common initial approach is to use the entity’s identifier attribute as the basis for equals/hashCode calculations:

Example 109. Naive equals/hashCode implementation

  1. @Entity(name = "Library")
  2. public static class Library {
  3. @Id
  4. private Long id;
  5. private String name;
  6. @OneToMany(cascade = CascadeType.ALL)
  7. @JoinColumn(name = "book_id")
  8. private Set<Book> books = new HashSet<>();
  9. //Getters and setters are omitted for brevity
  10. }
  11. @Entity(name = "Book")
  12. public static class Book {
  13. @Id
  14. @GeneratedValue
  15. private Long id;
  16. private String title;
  17. private String author;
  18. //Getters and setters are omitted for brevity
  19. @Override
  20. public boolean equals(Object o) {
  21. if ( this == o ) {
  22. return true;
  23. }
  24. if ( o == null || getClass() != o.getClass() ) {
  25. return false;
  26. }
  27. Book book = (Book) o;
  28. return Objects.equals( id, book.id );
  29. }
  30. @Override
  31. public int hashCode() {
  32. return Objects.hash( id );
  33. }
  34. }

It turns out that this still breaks when adding transient instance of Book to a set as we saw in the last example:

Example 110. Auto-generated identifiers with Sets and naive equals/hashCode

  1. Book book1 = new Book();
  2. book1.setTitle( "High-Performance Java Persistence" );
  3. Book book2 = new Book();
  4. book2.setTitle( "Java Persistence with Hibernate" );
  5. Library library = doInJPA( this::entityManagerFactory, entityManager -> {
  6. Library _library = entityManager.find( Library.class, 1L );
  7. _library.getBooks().add( book1 );
  8. _library.getBooks().add( book2 );
  9. return _library;
  10. } );
  11. assertFalse( library.getBooks().contains( book1 ) );
  12. assertFalse( library.getBooks().contains( book2 ) );

The issue here is a conflict between the use of the generated identifier, the contract of Set, and the equals/hashCode implementations. Set says that the equals/hashCode value for an object should not change while the object is part of the Set. But that is exactly what happened here because the equals/hasCode are based on the (generated) id, which was not set until the JPA transaction is committed.

Note that this is just a concern when using generated identifiers. If you are using assigned identifiers this will not be a problem, assuming the identifier value is assigned prior to adding to the Set.

Another option is to force the identifier to be generated and set prior to adding to the Set:

Example 111. Forcing the flush before adding to the Set

  1. Book book1 = new Book();
  2. book1.setTitle( "High-Performance Java Persistence" );
  3. Book book2 = new Book();
  4. book2.setTitle( "Java Persistence with Hibernate" );
  5. Library library = doInJPA( this::entityManagerFactory, entityManager -> {
  6. Library _library = entityManager.find( Library.class, 1L );
  7. entityManager.persist( book1 );
  8. entityManager.persist( book2 );
  9. entityManager.flush();
  10. _library.getBooks().add( book1 );
  11. _library.getBooks().add( book2 );
  12. return _library;
  13. } );
  14. assertTrue( library.getBooks().contains( book1 ) );
  15. assertTrue( library.getBooks().contains( book2 ) );

But this is often not feasible.

The final approach is to use a “better” equals/hashCode implementation, making use of a natural-id or business-key.

Example 112. Natural Id equals/hashCode

  1. @Entity(name = "Library")
  2. public static class Library {
  3. @Id
  4. private Long id;
  5. private String name;
  6. @OneToMany(cascade = CascadeType.ALL)
  7. @JoinColumn(name = "book_id")
  8. private Set<Book> books = new HashSet<>();
  9. //Getters and setters are omitted for brevity
  10. }
  11. @Entity(name = "Book")
  12. public static class Book {
  13. @Id
  14. @GeneratedValue
  15. private Long id;
  16. private String title;
  17. private String author;
  18. @NaturalId
  19. private String isbn;
  20. //Getters and setters are omitted for brevity
  21. @Override
  22. public boolean equals(Object o) {
  23. if ( this == o ) {
  24. return true;
  25. }
  26. if ( o == null || getClass() != o.getClass() ) {
  27. return false;
  28. }
  29. Book book = (Book) o;
  30. return Objects.equals( isbn, book.isbn );
  31. }
  32. @Override
  33. public int hashCode() {
  34. return Objects.hash( isbn );
  35. }
  36. }

This time, when adding a Book to the Library Set, you can retrieve the Book even after it’s being persisted:

Example 113. Natural Id equals/hashCode persist example

  1. Book book1 = new Book();
  2. book1.setTitle( "High-Performance Java Persistence" );
  3. book1.setIsbn( "978-9730228236" );
  4. Library library = doInJPA( this::entityManagerFactory, entityManager -> {
  5. Library _library = entityManager.find( Library.class, 1L );
  6. _library.getBooks().add( book1 );
  7. return _library;
  8. } );
  9. assertTrue( library.getBooks().contains( book1 ) );

As you can see the question of equals/hashCode is not trivial, nor is there a one-size-fits-all solution.

Although using a natural-id is best for equals and hashCode, sometimes you only have the entity identifier that provides a unique constraint.

It’s possible to use the entity identifier for equality check, but it needs a workaround:

  • you need to provide a constant value for hashCode so that the hash code value does not change before and after the entity is flushed.

  • you need to compare the entity identifier equality only for non-transient entities.

For details on mapping the identifier, see the Identifiers chapter.

2.5.8. Mapping the entity to a SQL query

You can map an entity to a SQL query using the @Subselect annotation.

Example 114. @Subselect entity mapping

  1. @Entity(name = "Client")
  2. @Table(name = "client")
  3. public static class Client {
  4. @Id
  5. private Long id;
  6. @Column(name = "first_name")
  7. private String firstName;
  8. @Column(name = "last_name")
  9. private String lastName;
  10. //Getters and setters omitted for brevity
  11. }
  12. @Entity(name = "Account")
  13. @Table(name = "account")
  14. public static class Account {
  15. @Id
  16. private Long id;
  17. @ManyToOne
  18. private Client client;
  19. private String description;
  20. //Getters and setters omitted for brevity
  21. }
  22. @Entity(name = "AccountTransaction")
  23. @Table(name = "account_transaction")
  24. public static class AccountTransaction {
  25. @Id
  26. @GeneratedValue
  27. private Long id;
  28. @ManyToOne
  29. private Account account;
  30. private Integer cents;
  31. private String description;
  32. //Getters and setters omitted for brevity
  33. }
  34. @Entity(name = "AccountSummary")
  35. @Subselect(
  36. "select " +
  37. " a.id as id, " +
  38. " concat(concat(c.first_name, ' '), c.last_name) as clientName, " +
  39. " sum(atr.cents) as balance " +
  40. "from account a " +
  41. "join client c on c.id = a.client_id " +
  42. "join account_transaction atr on a.id = atr.account_id " +
  43. "group by a.id, concat(concat(c.first_name, ' '), c.last_name)"
  44. )
  45. @Synchronize( {"client", "account", "account_transaction"} )
  46. public static class AccountSummary {
  47. @Id
  48. private Long id;
  49. private String clientName;
  50. private int balance;
  51. //Getters and setters omitted for brevity
  52. }

In the example above, the Account entity does not retain any balance since every account operation is registered as an AccountTransaction. To find the Account balance, we need to query the AccountSummary which shares the same identifier with the Account entity.

However, the AccountSummary is not mapped to a physical table, but to an SQL query.

So, if we have the following AccountTransaction record, the AccountSummary balance will match the proper amount of money in this Account.

Example 115. Finding a @Subselect entity

  1. doInJPA( this::entityManagerFactory, entityManager -> {
  2. Client client = new Client();
  3. client.setId( 1L );
  4. client.setFirstName( "John" );
  5. client.setLastName( "Doe" );
  6. entityManager.persist( client );
  7. Account account = new Account();
  8. account.setId( 1L );
  9. account.setClient( client );
  10. account.setDescription( "Checking account" );
  11. entityManager.persist( account );
  12. AccountTransaction transaction = new AccountTransaction();
  13. transaction.setAccount( account );
  14. transaction.setDescription( "Salary" );
  15. transaction.setCents( 100 * 7000 );
  16. entityManager.persist( transaction );
  17. AccountSummary summary = entityManager.createQuery(
  18. "select s " +
  19. "from AccountSummary s " +
  20. "where s.id = :id", AccountSummary.class)
  21. .setParameter( "id", account.getId() )
  22. .getSingleResult();
  23. assertEquals( "John Doe", summary.getClientName() );
  24. assertEquals( 100 * 7000, summary.getBalance() );
  25. } );

If we add a new AccountTransaction entity and refresh the AccountSummary entity, the balance is updated accordingly:

Example 116. Refreshing a @Subselect entity

  1. doInJPA( this::entityManagerFactory, entityManager -> {
  2. AccountSummary summary = entityManager.find( AccountSummary.class, 1L );
  3. assertEquals( "John Doe", summary.getClientName() );
  4. assertEquals( 100 * 7000, summary.getBalance() );
  5. AccountTransaction transaction = new AccountTransaction();
  6. transaction.setAccount( entityManager.getReference( Account.class, 1L ) );
  7. transaction.setDescription( "Shopping" );
  8. transaction.setCents( -100 * 2200 );
  9. entityManager.persist( transaction );
  10. entityManager.flush();
  11. entityManager.refresh( summary );
  12. assertEquals( 100 * 4800, summary.getBalance() );
  13. } );

The goal of the @Synchronize annotation in the AccountSummary entity mapping is to instruct Hibernate which database tables are needed by the underlying @Subselect SQL query. This is because, unlike JPQL and HQL queries, Hibernate cannot parse the underlying native SQL query.

With the @Synchronize annotation in place, when executing an HQL or JPQL which selects from the AccountSummary entity, Hibernate will trigger a Persistence Context flush if there are pending Account, Client or AccountTransaction entity state transitions.

2.5.9. Define a custom entity proxy

By default, when it needs to use a proxy instead of the actual POJO, Hibernate is going to use a Bytecode manipulation library like Javassist or Byte Buddy.

However, if the entity class is final, Javassist will not create a proxy and you will get a POJO even when you only need a proxy reference. In this case, you could proxy an interface that this particular entity implements, as illustrated by the following example.

Example 117. Final entity class implementing the Identifiable interface

  1. public interface Identifiable {
  2. Long getId();
  3. void setId(Long id);
  4. }
  5. @Entity( name = "Book" )
  6. @Proxy(proxyClass = Identifiable.class)
  7. public static final class Book implements Identifiable {
  8. @Id
  9. private Long id;
  10. private String title;
  11. private String author;
  12. @Override
  13. public Long getId() {
  14. return id;
  15. }
  16. @Override
  17. public void setId(Long id) {
  18. this.id = id;
  19. }
  20. //Other getters and setters omitted for brevity
  21. }

The @Proxy annotation is used to specify a custom proxy implementation for the current annotated entity.

When loading the Book entity proxy, Hibernate is going to proxy the Identifiable interface instead as illustrated by the following example:

Example 118. Proxying the final entity class implementing the Identifiable interface

  1. doInHibernate( this::sessionFactory, session -> {
  2. Book book = new Book();
  3. book.setId( 1L );
  4. book.setTitle( "High-Performance Java Persistence" );
  5. book.setAuthor( "Vlad Mihalcea" );
  6. session.persist( book );
  7. } );
  8. doInHibernate( this::sessionFactory, session -> {
  9. Identifiable book = session.getReference( Book.class, 1L );
  10. assertTrue(
  11. "Loaded entity is not an instance of the proxy interface",
  12. book instanceof Identifiable
  13. );
  14. assertFalse(
  15. "Proxy class was not created",
  16. book instanceof Book
  17. );
  18. } );
  1. insert
  2. into
  3. Book
  4. (author, title, id)
  5. values
  6. (?, ?, ?)
  7. -- binding parameter [1] as [VARCHAR] - [Vlad Mihalcea]
  8. -- binding parameter [2] as [VARCHAR] - [High-Performance Java Persistence]
  9. -- binding parameter [3] as [BIGINT] - [1]

As you can see in the associated SQL snippet, Hibernate issues no SQL SELECT query since the proxy can be constructed without needing to fetch the actual entity POJO.

2.5.10. Dynamic entity proxies using the @Tuplizer annotation

It is possible to map your entities as dynamic proxies using the @Tuplizer annotation.

In the following entity mapping, both the embeddable and the entity are mapped as interfaces, not POJOs.

Example 119. Dynamic entity proxy mapping

  1. @Entity
  2. @Tuplizer(impl = DynamicEntityTuplizer.class)
  3. public interface Cuisine {
  4. @Id
  5. @GeneratedValue
  6. Long getId();
  7. void setId(Long id);
  8. String getName();
  9. void setName(String name);
  10. @Tuplizer(impl = DynamicEmbeddableTuplizer.class)
  11. Country getCountry();
  12. void setCountry(Country country);
  13. }
  1. @Embeddable
  2. public interface Country {
  3. @Column(name = "CountryName")
  4. String getName();
  5. void setName(String name);
  6. }

The @Tuplizer instructs Hibernate to use the DynamicEntityTuplizer and DynamicEmbeddableTuplizer to handle the associated entity and embeddable object types.

Both the Cuisine entity and the Country embeddable types are going to be instantiated as Java dynamic proxies, as you can see in the following DynamicInstantiator example:

Example 120. Instantiating entities and embeddables as dynamic proxies

  1. public class DynamicEntityTuplizer extends PojoEntityTuplizer {
  2. public DynamicEntityTuplizer(
  3. EntityMetamodel entityMetamodel,
  4. PersistentClass mappedEntity) {
  5. super( entityMetamodel, mappedEntity );
  6. }
  7. @Override
  8. protected Instantiator buildInstantiator(
  9. EntityMetamodel entityMetamodel,
  10. PersistentClass persistentClass) {
  11. return new DynamicInstantiator(
  12. persistentClass.getClassName()
  13. );
  14. }
  15. @Override
  16. protected ProxyFactory buildProxyFactory(
  17. PersistentClass persistentClass,
  18. Getter idGetter,
  19. Setter idSetter) {
  20. return super.buildProxyFactory(
  21. persistentClass, idGetter,
  22. idSetter
  23. );
  24. }
  25. }
  1. public class DynamicEmbeddableTuplizer
  2. extends PojoComponentTuplizer {
  3. public DynamicEmbeddableTuplizer(Component embeddable) {
  4. super( embeddable );
  5. }
  6. protected Instantiator buildInstantiator(Component embeddable) {
  7. return new DynamicInstantiator(
  8. embeddable.getComponentClassName()
  9. );
  10. }
  11. }
  1. public class DynamicInstantiator
  2. implements Instantiator {
  3. private final Class targetClass;
  4. public DynamicInstantiator(String targetClassName) {
  5. try {
  6. this.targetClass = Class.forName( targetClassName );
  7. }
  8. catch (ClassNotFoundException e) {
  9. throw new HibernateException( e );
  10. }
  11. }
  12. public Object instantiate(Serializable id) {
  13. return ProxyHelper.newProxy( targetClass, id );
  14. }
  15. public Object instantiate() {
  16. return instantiate( null );
  17. }
  18. public boolean isInstance(Object object) {
  19. try {
  20. return targetClass.isInstance( object );
  21. }
  22. catch( Throwable t ) {
  23. throw new HibernateException(
  24. "could not get handle to entity as interface : " + t
  25. );
  26. }
  27. }
  28. }
  1. public class ProxyHelper {
  2. public static <T> T newProxy(Class<T> targetClass, Serializable id) {
  3. return ( T ) Proxy.newProxyInstance(
  4. targetClass.getClassLoader(),
  5. new Class[] {
  6. targetClass
  7. },
  8. new DataProxyHandler(
  9. targetClass.getName(),
  10. id
  11. )
  12. );
  13. }
  14. public static String extractEntityName(Object object) {
  15. if ( Proxy.isProxyClass( object.getClass() ) ) {
  16. InvocationHandler handler = Proxy.getInvocationHandler(
  17. object
  18. );
  19. if ( DataProxyHandler.class.isAssignableFrom( handler.getClass() ) ) {
  20. DataProxyHandler myHandler = (DataProxyHandler) handler;
  21. return myHandler.getEntityName();
  22. }
  23. }
  24. return null;
  25. }
  26. }
  1. public final class DataProxyHandler implements InvocationHandler {
  2. private String entityName;
  3. private Map<String, Object> data = new HashMap<>();
  4. public DataProxyHandler(String entityName, Serializable id) {
  5. this.entityName = entityName;
  6. data.put( "Id", id );
  7. }
  8. public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  9. String methodName = method.getName();
  10. if ( methodName.startsWith( "set" ) ) {
  11. String propertyName = methodName.substring( 3 );
  12. data.put( propertyName, args[0] );
  13. }
  14. else if ( methodName.startsWith( "get" ) ) {
  15. String propertyName = methodName.substring( 3 );
  16. return data.get( propertyName );
  17. }
  18. else if ( "toString".equals( methodName ) ) {
  19. return entityName + "#" + data.get( "Id" );
  20. }
  21. else if ( "hashCode".equals( methodName ) ) {
  22. return this.hashCode();
  23. }
  24. return null;
  25. }
  26. public String getEntityName() {
  27. return entityName;
  28. }
  29. }

With the DynamicInstantiator in place, we can work with the dynamic proxy entities just like with POJO entities.

Example 121. Persisting entities and embeddables as dynamic proxies

  1. Cuisine _cuisine = doInHibernateSessionBuilder(
  2. () -> sessionFactory()
  3. .withOptions()
  4. .interceptor( new EntityNameInterceptor() ),
  5. session -> {
  6. Cuisine cuisine = ProxyHelper.newProxy( Cuisine.class, null );
  7. cuisine.setName( "Française" );
  8. Country country = ProxyHelper.newProxy( Country.class, null );
  9. country.setName( "France" );
  10. cuisine.setCountry( country );
  11. session.persist( cuisine );
  12. return cuisine;
  13. } );
  14. doInHibernateSessionBuilder(
  15. () -> sessionFactory()
  16. .withOptions()
  17. .interceptor( new EntityNameInterceptor() ),
  18. session -> {
  19. Cuisine cuisine = session.get( Cuisine.class, _cuisine.getId() );
  20. assertEquals( "Française", cuisine.getName() );
  21. assertEquals( "France", cuisine.getCountry().getName() );
  22. } );

2.5.11. Define a custom entity persister

The @Persister annotation is used to specify a custom entity or collection persister.

For entities, the custom persister must implement the EntityPersister interface.

For collections, the custom persister must implement the CollectionPersister interface.

Example 122. Entity persister mapping

  1. @Entity
  2. @Persister( impl = EntityPersister.class )
  3. public class Author {
  4. @Id
  5. public Integer id;
  6. @OneToMany( mappedBy = "author" )
  7. @Persister( impl = CollectionPersister.class )
  8. public Set<Book> books = new HashSet<>();
  9. //Getters and setters omitted for brevity
  10. public void addBook(Book book) {
  11. this.books.add( book );
  12. book.setAuthor( this );
  13. }
  14. }
  1. @Entity
  2. @Persister( impl = EntityPersister.class )
  3. public class Book {
  4. @Id
  5. public Integer id;
  6. private String title;
  7. @ManyToOne(fetch = FetchType.LAZY)
  8. public Author author;
  9. //Getters and setters omitted for brevity
  10. }

By providing your own EntityPersister and CollectionPersister implementations, you can control how entities and collections are persisted into the database.

2.5.12. Access strategies

As a JPA provider, Hibernate can introspect both the entity attributes (instance fields) or the accessors (instance properties). By default, the placement of the @Id annotation gives the default access strategy. When placed on a field, Hibernate will assume field-based access. When placed on the identifier getter, Hibernate will use property-based access.

To avoid issues such as HCANN-63 - Property name beginning with at least two uppercase characters has odd functionality in HQL, you should pay attention to Java Bean specification in regard to naming properties.

Embeddable types inherit the access strategy from their parent entities.

Field-based access

Example 123. Field-based access

  1. @Entity(name = "Book")
  2. public static class Book {
  3. @Id
  4. private Long id;
  5. private String title;
  6. private String author;
  7. //Getters and setters are omitted for brevity
  8. }

When using field-based access, adding other entity-level methods is much more flexible because Hibernate won’t consider those part of the persistence state. To exclude a field from being part of the entity persistent state, the field must be marked with the @Transient annotation.

Another advantage of using field-based access is that some entity attributes can be hidden from outside the entity.

An example of such attribute is the entity @Version field, which, usually, does not need to be manipulated by the data access layer.

With field-based access, we can simply omit the getter and the setter for this version field, and Hibernate can still leverage the optimistic concurrency control mechanism.

Property-based access

Example 124. Property-based access

  1. @Entity(name = "Book")
  2. public static class Book {
  3. private Long id;
  4. private String title;
  5. private String author;
  6. @Id
  7. public Long getId() {
  8. return id;
  9. }
  10. public void setId(Long id) {
  11. this.id = id;
  12. }
  13. public String getTitle() {
  14. return title;
  15. }
  16. public void setTitle(String title) {
  17. this.title = title;
  18. }
  19. public String getAuthor() {
  20. return author;
  21. }
  22. public void setAuthor(String author) {
  23. this.author = author;
  24. }
  25. }

When using property-based access, Hibernate uses the accessors for both reading and writing the entity state. Every other method that will be added to the entity (e.g. helper methods for synchronizing both ends of a bidirectional one-to-many association) will have to be marked with the @Transient annotation.

Overriding the default access strategy

The default access strategy mechanism can be overridden with the JPA @Access annotation. In the following example, the @Version attribute is accessed by its field and not by its getter, like the rest of entity attributes.

Example 125. Overriding access strategy

  1. @Entity(name = "Book")
  2. public static class Book {
  3. private Long id;
  4. private String title;
  5. private String author;
  6. @Access( AccessType.FIELD )
  7. @Version
  8. private int version;
  9. @Id
  10. public Long getId() {
  11. return id;
  12. }
  13. public void setId(Long id) {
  14. this.id = id;
  15. }
  16. public String getTitle() {
  17. return title;
  18. }
  19. public void setTitle(String title) {
  20. this.title = title;
  21. }
  22. public String getAuthor() {
  23. return author;
  24. }
  25. public void setAuthor(String author) {
  26. this.author = author;
  27. }
  28. }
Embeddable types and access strategy

Because embeddables are managed by their owning entities, the access strategy is therefore inherited from the entity too. This applies to both simple embeddable types as well as for collection of embeddables.

The embeddable types can overrule the default implicit access strategy (inherited from the owning entity). In the following example, the embeddable uses property-based access, no matter what access strategy the owning entity is choosing:

Example 126. Embeddable with exclusive access strategy

  1. @Embeddable
  2. @Access( AccessType.PROPERTY )
  3. public static class Author {
  4. private String firstName;
  5. private String lastName;
  6. public Author() {
  7. }
  8. public Author(String firstName, String lastName) {
  9. this.firstName = firstName;
  10. this.lastName = lastName;
  11. }
  12. public String getFirstName() {
  13. return firstName;
  14. }
  15. public void setFirstName(String firstName) {
  16. this.firstName = firstName;
  17. }
  18. public String getLastName() {
  19. return lastName;
  20. }
  21. public void setLastName(String lastName) {
  22. this.lastName = lastName;
  23. }
  24. }

The owning entity can use field-based access while the embeddable uses property-based access as it has chosen explicitly:

Example 127. Entity including a single embeddable type

  1. @Entity(name = "Book")
  2. public static class Book {
  3. @Id
  4. private Long id;
  5. private String title;
  6. @Embedded
  7. private Author author;
  8. //Getters and setters are omitted for brevity
  9. }

This works also for collection of embeddable types:

Example 128. Entity including a collection of embeddable types

  1. @Entity(name = "Book")
  2. public static class Book {
  3. @Id
  4. private Long id;
  5. private String title;
  6. @ElementCollection
  7. @CollectionTable(
  8. name = "book_author",
  9. joinColumns = @JoinColumn(name = "book_id")
  10. )
  11. private List<Author> authors = new ArrayList<>();
  12. //Getters and setters are omitted for brevity
  13. }

2.6. Identifiers

Identifiers model the primary key of an entity. They are used to uniquely identify each specific entity.

Hibernate and JPA both make the following assumptions about the corresponding database column(s):

UNIQUE

The values must uniquely identify each row.

NOT NULL

The values cannot be null. For composite ids, no part can be null.

IMMUTABLE

The values, once inserted, can never be changed. This is more a general guide, than a hard-fast rule as opinions vary. JPA defines the behavior of changing the value of the identifier attribute to be undefined; Hibernate simply does not support that. In cases where the values for the PK you have chosen will be updated, Hibernate recommends mapping the mutable value as a natural id, and use a surrogate id for the PK. See Natural Ids.

Technically the identifier does not have to map to the column(s) physically defined as the table primary key. They just need to map to column(s) that uniquely identify each row. However, this documentation will continue to use the terms identifier and primary key interchangeably.

Every entity must define an identifier. For entity inheritance hierarchies, the identifier must be defined just on the entity that is the root of the hierarchy.

An identifier might be simple (single value) or composite (multiple values).

2.6.1. Simple identifiers

Simple identifiers map to a single basic attribute, and are denoted using the javax.persistence.Id annotation.

According to JPA only the following types should be used as identifier attribute types:

  • any Java primitive type

  • any primitive wrapper type

  • java.lang.String

  • java.util.Date (TemporalType#DATE)

  • java.sql.Date

  • java.math.BigDecimal

  • java.math.BigInteger

Any types used for identifier attributes beyond this list will not be portable.

Assigned identifiers

Values for simple identifiers can be assigned, as we have seen in the examples above. The expectation for assigned identifier values is that the application assigns (sets them on the entity attribute) prior to calling save/persist.

Example 129. Simple assigned entity identifier

  1. @Entity(name = "Book")
  2. public static class Book {
  3. @Id
  4. private Long id;
  5. private String title;
  6. private String author;
  7. //Getters and setters are omitted for brevity
  8. }
Generated identifiers

Values for simple identifiers can be generated. To denote that an identifier attribute is generated, it is annotated with javax.persistence.GeneratedValue

Example 130. Simple generated identifier

  1. @Entity(name = "Book")
  2. public static class Book {
  3. @Id
  4. @GeneratedValue
  5. private Long id;
  6. private String title;
  7. private String author;
  8. //Getters and setters are omitted for brevity
  9. }

Additionally, to the type restriction list above, JPA says that if using generated identifier values (see below) only integer types (short, int, long) will be portably supported.

The expectation for generated identifier values is that Hibernate will generate the value when the save/persist occurs.

Identifier value generations strategies are discussed in detail in the Generated identifier values section.

2.6.2. Composite identifiers

Composite identifiers correspond to one or more persistent attributes. Here are the rules governing composite identifiers, as defined by the JPA specification:

  • The composite identifier must be represented by a “primary key class”. The primary key class may be defined using the javax.persistence.EmbeddedId annotation (see Composite identifiers with @EmbeddedId), or defined using the javax.persistence.IdClass annotation (see Composite identifiers with @IdClass).

  • The primary key class must be public and must have a public no-arg constructor.

  • The primary key class must be serializable.

  • The primary key class must define equals and hashCode methods, consistent with equality for the underlying database types to which the primary key is mapped.

The restriction that a composite identifier has to be represented by a “primary key class” (e.g. @EmbeddedId or @IdClass) is only JPA-specific.

Hibernate does allow composite identifiers to be defined without a “primary key class” via multiple @Id attributes.

The attributes making up the composition can be either basic, composite, @ManyToOne. Note especially that collection and one-to-one are never appropriate.

2.6.3. Composite identifiers with @EmbeddedId

Modeling a composite identifier using an EmbeddedId simply means defining an embeddable to be a composition for the one or more attributes making up the identifier, and then exposing an attribute of that embeddable type on the entity.

Example 131. Basic @EmbeddedId

  1. @Entity(name = "SystemUser")
  2. public static class SystemUser {
  3. @EmbeddedId
  4. private PK pk;
  5. private String name;
  6. //Getters and setters are omitted for brevity
  7. }
  8. @Embeddable
  9. public static class PK implements Serializable {
  10. private String subsystem;
  11. private String username;
  12. public PK(String subsystem, String username) {
  13. this.subsystem = subsystem;
  14. this.username = username;
  15. }
  16. private PK() {
  17. }
  18. @Override
  19. public boolean equals(Object o) {
  20. if ( this == o ) {
  21. return true;
  22. }
  23. if ( o == null || getClass() != o.getClass() ) {
  24. return false;
  25. }
  26. PK pk = (PK) o;
  27. return Objects.equals( subsystem, pk.subsystem ) &&
  28. Objects.equals( username, pk.username );
  29. }
  30. @Override
  31. public int hashCode() {
  32. return Objects.hash( subsystem, username );
  33. }
  34. }

As mentioned before, EmbeddedIds can even contain @ManyToOne attributes:

Example 132. @EmbeddedId with @ManyToOne

  1. @Entity(name = "SystemUser")
  2. public static class SystemUser {
  3. @EmbeddedId
  4. private PK pk;
  5. private String name;
  6. //Getters and setters are omitted for brevity
  7. }
  8. @Entity(name = "Subsystem")
  9. public static class Subsystem {
  10. @Id
  11. private String id;
  12. private String description;
  13. //Getters and setters are omitted for brevity
  14. }
  15. @Embeddable
  16. public static class PK implements Serializable {
  17. @ManyToOne(fetch = FetchType.LAZY)
  18. private Subsystem subsystem;
  19. private String username;
  20. public PK(Subsystem subsystem, String username) {
  21. this.subsystem = subsystem;
  22. this.username = username;
  23. }
  24. private PK() {
  25. }
  26. @Override
  27. public boolean equals(Object o) {
  28. if ( this == o ) {
  29. return true;
  30. }
  31. if ( o == null || getClass() != o.getClass() ) {
  32. return false;
  33. }
  34. PK pk = (PK) o;
  35. return Objects.equals( subsystem, pk.subsystem ) &&
  36. Objects.equals( username, pk.username );
  37. }
  38. @Override
  39. public int hashCode() {
  40. return Objects.hash( subsystem, username );
  41. }
  42. }

Hibernate supports directly modeling @ManyToOne associations in the Primary Key class, whether @EmbeddedId or @IdClass.

However, that is not portably supported by the JPA specification. In JPA terms, one would use “derived identifiers”. For more details, see Derived Identifiers.

2.6.4. Composite identifiers with @IdClass

Modeling a composite identifier using an IdClass differs from using an EmbeddedId in that the entity defines each individual attribute making up the composition. The IdClass simply acts as a “shadow”.

Example 133. Basic @IdClass

  1. @Entity(name = "SystemUser")
  2. @IdClass( PK.class )
  3. public static class SystemUser {
  4. @Id
  5. private String subsystem;
  6. @Id
  7. private String username;
  8. private String name;
  9. public PK getId() {
  10. return new PK(
  11. subsystem,
  12. username
  13. );
  14. }
  15. public void setId(PK id) {
  16. this.subsystem = id.getSubsystem();
  17. this.username = id.getUsername();
  18. }
  19. //Getters and setters are omitted for brevity
  20. }
  21. public static class PK implements Serializable {
  22. private String subsystem;
  23. private String username;
  24. public PK(String subsystem, String username) {
  25. this.subsystem = subsystem;
  26. this.username = username;
  27. }
  28. private PK() {
  29. }
  30. //Getters and setters are omitted for brevity
  31. @Override
  32. public boolean equals(Object o) {
  33. if ( this == o ) {
  34. return true;
  35. }
  36. if ( o == null || getClass() != o.getClass() ) {
  37. return false;
  38. }
  39. PK pk = (PK) o;
  40. return Objects.equals( subsystem, pk.subsystem ) &&
  41. Objects.equals( username, pk.username );
  42. }
  43. @Override
  44. public int hashCode() {
  45. return Objects.hash( subsystem, username );
  46. }
  47. }

Non-aggregated composite identifiers can also contain ManyToOne attributes as we saw with aggregated ones (still non-portably).

Example 134. IdClass with @ManyToOne

  1. @Entity(name = "SystemUser")
  2. @IdClass( PK.class )
  3. public static class SystemUser {
  4. @Id
  5. @ManyToOne(fetch = FetchType.LAZY)
  6. private Subsystem subsystem;
  7. @Id
  8. private String username;
  9. private String name;
  10. //Getters and setters are omitted for brevity
  11. }
  12. @Entity(name = "Subsystem")
  13. public static class Subsystem {
  14. @Id
  15. private String id;
  16. private String description;
  17. //Getters and setters are omitted for brevity
  18. }
  19. public static class PK implements Serializable {
  20. private Subsystem subsystem;
  21. private String username;
  22. public PK(Subsystem subsystem, String username) {
  23. this.subsystem = subsystem;
  24. this.username = username;
  25. }
  26. private PK() {
  27. }
  28. //Getters and setters are omitted for brevity
  29. }

With non-aggregated composite identifiers, Hibernate also supports “partial” generation of the composite values.

Example 135. @IdClass with partial identifier generation using @GeneratedValue

  1. @Entity(name = "SystemUser")
  2. @IdClass( PK.class )
  3. public static class SystemUser {
  4. @Id
  5. private String subsystem;
  6. @Id
  7. private String username;
  8. @Id
  9. @GeneratedValue
  10. private Integer registrationId;
  11. private String name;
  12. public PK getId() {
  13. return new PK(
  14. subsystem,
  15. username,
  16. registrationId
  17. );
  18. }
  19. public void setId(PK id) {
  20. this.subsystem = id.getSubsystem();
  21. this.username = id.getUsername();
  22. this.registrationId = id.getRegistrationId();
  23. }
  24. //Getters and setters are omitted for brevity
  25. }
  26. public static class PK implements Serializable {
  27. private String subsystem;
  28. private String username;
  29. private Integer registrationId;
  30. public PK(String subsystem, String username) {
  31. this.subsystem = subsystem;
  32. this.username = username;
  33. }
  34. public PK(String subsystem, String username, Integer registrationId) {
  35. this.subsystem = subsystem;
  36. this.username = username;
  37. this.registrationId = registrationId;
  38. }
  39. private PK() {
  40. }
  41. //Getters and setters are omitted for brevity
  42. @Override
  43. public boolean equals(Object o) {
  44. if ( this == o ) {
  45. return true;
  46. }
  47. if ( o == null || getClass() != o.getClass() ) {
  48. return false;
  49. }
  50. PK pk = (PK) o;
  51. return Objects.equals( subsystem, pk.subsystem ) &&
  52. Objects.equals( username, pk.username ) &&
  53. Objects.equals( registrationId, pk.registrationId );
  54. }
  55. @Override
  56. public int hashCode() {
  57. return Objects.hash( subsystem, username, registrationId );
  58. }
  59. }

This feature which allows auto-generated values in composite identifiers exists because of a highly questionable interpretation of the JPA specification made by the SpecJ committee.

Hibernate does not feel that JPA defines support for this, but added the feature simply to be usable in SpecJ benchmarks. Use of this feature may or may not be portable from a JPA perspective.

2.6.5. Composite identifiers with associations

Hibernate allows defining a composite identifier out of entity associations. In the following example, the PersonAddress entity identifier is formed of two @ManyToOne associations.

Example 136. Composite identifiers with associations

  1. @Entity(name = "Book")
  2. public static class Book implements Serializable {
  3. @Id
  4. @ManyToOne(fetch = FetchType.LAZY)
  5. private Author author;
  6. @Id
  7. @ManyToOne(fetch = FetchType.LAZY)
  8. private Publisher publisher;
  9. @Id
  10. private String title;
  11. public Book(Author author, Publisher publisher, String title) {
  12. this.author = author;
  13. this.publisher = publisher;
  14. this.title = title;
  15. }
  16. private Book() {
  17. }
  18. //Getters and setters are omitted for brevity
  19. @Override
  20. public boolean equals(Object o) {
  21. if ( this == o ) {
  22. return true;
  23. }
  24. if ( o == null || getClass() != o.getClass() ) {
  25. return false;
  26. }
  27. Book book = (Book) o;
  28. return Objects.equals( author, book.author ) &&
  29. Objects.equals( publisher, book.publisher ) &&
  30. Objects.equals( title, book.title );
  31. }
  32. @Override
  33. public int hashCode() {
  34. return Objects.hash( author, publisher, title );
  35. }
  36. }
  37. @Entity(name = "Author")
  38. public static class Author implements Serializable {
  39. @Id
  40. private String name;
  41. //Getters and setters are omitted for brevity
  42. @Override
  43. public boolean equals(Object o) {
  44. if ( this == o ) {
  45. return true;
  46. }
  47. if ( o == null || getClass() != o.getClass() ) {
  48. return false;
  49. }
  50. Author author = (Author) o;
  51. return Objects.equals( name, author.name );
  52. }
  53. @Override
  54. public int hashCode() {
  55. return Objects.hash( name );
  56. }
  57. }
  58. @Entity(name = "Publisher")
  59. public static class Publisher implements Serializable {
  60. @Id
  61. private String name;
  62. //Getters and setters are omitted for brevity
  63. @Override
  64. public boolean equals(Object o) {
  65. if ( this == o ) {
  66. return true;
  67. }
  68. if ( o == null || getClass() != o.getClass() ) {
  69. return false;
  70. }
  71. Publisher publisher = (Publisher) o;
  72. return Objects.equals( name, publisher.name );
  73. }
  74. @Override
  75. public int hashCode() {
  76. return Objects.hash( name );
  77. }
  78. }

Although the mapping is much simpler than using an @EmbeddedId or an @IdClass, there’s no separation between the entity instance and the actual identifier. To query this entity, an instance of the entity itself must be supplied to the persistence context.

Example 137. Fetching with composite identifiers

  1. Book book = entityManager.find( Book.class, new Book(
  2. author,
  3. publisher,
  4. "High-Performance Java Persistence"
  5. ) );
  6. assertEquals( "Vlad Mihalcea", book.getAuthor().getName() );

2.6.6. Composite identifiers with generated properties

When using composite identifiers, the underlying identifier properties must be manually assigned by the user.

Automatically generated properties are not supported to be used to generate the value of an underlying property that makes the composite identifier.

Therefore, you cannot use any of the automatic property generator described by the generated properties section like @Generated, @CreationTimestamp or @ValueGenerationType or database-generated values.

Nevertheless, you can still generate the identifier properties prior to constructing the composite identifier, as illustrated by the following examples.

Assuming we have the following EventId composite identifier and an Event entity which uses the aforementioned composite identifier.

Example 138. The Event entity and EventId composite identifier

  1. @Entity
  2. class Event {
  3. @Id
  4. private EventId id;
  5. @Column(name = "event_key")
  6. private String key;
  7. @Column(name = "event_value")
  8. private String value;
  9. //Getters and setters are omitted for brevity
  10. }
  1. @Embeddable
  2. class EventId implements Serializable {
  3. private Integer category;
  4. private Timestamp createdOn;
  5. //Getters and setters are omitted for brevity
  6. @Override
  7. public boolean equals(Object o) {
  8. if ( this == o ) {
  9. return true;
  10. }
  11. if ( o == null || getClass() != o.getClass() ) {
  12. return false;
  13. }
  14. EventId that = (EventId) o;
  15. return Objects.equals( category, that.category ) &&
  16. Objects.equals( createdOn, that.createdOn );
  17. }
  18. @Override
  19. public int hashCode() {
  20. return Objects.hash( category, createdOn );
  21. }
  22. }
In-memory generated composite identifier properties

If you want to generate the composite identifier properties in-memory, you need to do that as follows:

Example 139. In-memory generated composite identifier properties example

  1. EventId id = new EventId();
  2. id.setCategory( 1 );
  3. id.setCreatedOn( new Timestamp( System.currentTimeMillis() ) );
  4. Event event = new Event();
  5. event.setId( id );
  6. event.setKey( "Temperature" );
  7. event.setValue( "9" );
  8. entityManager.persist( event );

Notice that the createdOn property of the EventId composite identifier was generated by the data access code and assigned to the identifier prior to persisting the Event entity.

Database generated composite identifier properties

If you want to generate the composite identifier properties using a database function or stored procedure, you could to do it as illustrated by the following example.

Example 140. Database generated composite identifier properties example

  1. Timestamp currentTimestamp = (Timestamp) entityManager
  2. .createNativeQuery(
  3. "SELECT CURRENT_TIMESTAMP" )
  4. .getSingleResult();
  5. EventId id = new EventId();
  6. id.setCategory( 1 );
  7. id.setCreatedOn( currentTimestamp );
  8. Event event = new Event();
  9. event.setId( id );
  10. event.setKey( "Temperature" );
  11. event.setValue( "9" );
  12. entityManager.persist( event );

Notice that the createdOn property of the EventId composite identifier was generated by calling the CURRENT_TIMESTAMP database function, and we assigned it to the composite identifier prior to persisting the Event entity.

2.6.7. Generated identifier values

You can also auto-generate values for non-identifier attributes. For more details, see the Generated properties section.

Hibernate supports identifier value generation across a number of different types. Remember that JPA portably defines identifier value generation just for integer types.

Identifier value generation is indicated using the javax.persistence.GeneratedValue annotation. The most important piece of information here is the specified javax.persistence.GenerationType which indicates how values will be generated.

The discussions below assume that the application is using Hibernate’s “new generator mappings” as indicated by the hibernate.id.new_generator_mappings setting or MetadataBuilder.enableNewIdentifierGeneratorSupport method during bootstrap.

Starting with Hibernate 5, this is set to true by default. In applications where the hibernate.id.new_generator_mappings configuration is set to false the resolutions discussed here will be very different. The rest of the discussion here assumes this setting is enabled (true).

In Hibernate 5.3, Hibernate attempts to delay the insert of entities if the flush-mode does not equal AUTO. This was slightly problematic for entities that used IDENTITY or SEQUENCE generated identifiers that were also involved in some form of association with another entity in the same transaction.

In Hibernate 5.4, Hibernate attempts to remedy the problem using an algorithm to decide if the insert should be delayed or if it requires immediate insertion. We wanted to restore the behavior prior to 5.3 only for very specific use cases where it made sense.

Entity mappings can sometimes be complex and it is possible a corner case was overlooked. Hibernate offers a way to completely disable the 5.3 behavior in the event problems occur with DelayedPostInsertIdentifier. To enable the legacy behavior, set hibernate.id.disable_delayed_identity_inserts=true.

This configuration option is meant to act as a temporary fix and bridge the gap between the changes in this behavior across Hibernate 5.x releases. If this configuration setting is necessary for a mapping, please open a JIRA and report the mapping so that the algorithm can be reviewed.

AUTO (the default)

Indicates that the persistence provider (Hibernate) should choose an appropriate generation strategy. See Interpreting AUTO.

IDENTITY

Indicates that database IDENTITY columns will be used for primary key value generation. See Using IDENTITY columns.

SEQUENCE

Indicates that database sequence should be used for obtaining primary key values. See Using sequences.

TABLE

Indicates that a database table should be used for obtaining primary key values. See Using the table identifier generator.

2.6.8. Interpreting AUTO

How a persistence provider interprets the AUTO generation type is left up to the provider.

The default behavior is to look at the Java type of the identifier attribute.

If the identifier type is UUID, Hibernate is going to use a UUID identifier.

If the identifier type is numerical (e.g. Long, Integer), then Hibernate is going to use the IdGeneratorStrategyInterpreter to resolve the identifier generator strategy. The IdGeneratorStrategyInterpreter has two implementations:

FallbackInterpreter

This is the default strategy since Hibernate 5.0. For older versions, this strategy is enabled through the hibernate.id.new_generator_mappings configuration property. When using this strategy, AUTO always resolves to SequenceStyleGenerator. If the underlying database supports sequences, then a SEQUENCE generator is used. Otherwise, a TABLE generator is going to be used instead.

LegacyFallbackInterpreter

This is a legacy mechanism that was used by Hibernate prior to version 5.0 or when the hibernate.id.new_generator_mappings configuration property is false. The legacy strategy maps AUTO to the native generator strategy which uses the Dialect#getNativeIdentifierGeneratorStrategy to resolve the actual identifier generator (e.g. identity or sequence).

2.6.9. Using sequences

For implementing database sequence-based identifier value generation Hibernate makes use of its org.hibernate.id.enhanced.SequenceStyleGenerator id generator. It is important to note that SequenceStyleGenerator is capable of working against databases that do not support sequences by switching to a table as the underlying backing. This gives Hibernate a huge degree of portability across databases while still maintaining consistent id generation behavior (versus say choosing between SEQUENCE and IDENTITY). This backing storage is completely transparent to the user.

The preferred (and portable) way to configure this generator is using the JPA-defined javax.persistence.SequenceGenerator annotation.

The simplest form is to simply request sequence generation; Hibernate will use a single, implicitly-named sequence (hibernate_sequence) for all such unnamed definitions.

Example 141. Unnamed sequence

  1. @Entity(name = "Product")
  2. public static class Product {
  3. @Id
  4. @GeneratedValue(
  5. strategy = GenerationType.SEQUENCE
  6. )
  7. private Long id;
  8. @Column(name = "product_name")
  9. private String name;
  10. //Getters and setters are omitted for brevity
  11. }

Using javax.persistence.SequenceGenerator, you can specify a specific database sequence name.

Example 142. Named sequence

  1. @Entity(name = "Product")
  2. public static class Product {
  3. @Id
  4. @GeneratedValue(
  5. strategy = GenerationType.SEQUENCE,
  6. generator = "sequence-generator"
  7. )
  8. @SequenceGenerator(
  9. name = "sequence-generator",
  10. sequenceName = "product_sequence"
  11. )
  12. private Long id;
  13. @Column(name = "product_name")
  14. private String name;
  15. //Getters and setters are omitted for brevity
  16. }

The javax.persistence.SequenceGenerator annotation allows you to specify additional configurations as well.

Example 143. Configured sequence

  1. @Entity(name = "Product")
  2. public static class Product {
  3. @Id
  4. @GeneratedValue(
  5. strategy = GenerationType.SEQUENCE,
  6. generator = "sequence-generator"
  7. )
  8. @SequenceGenerator(
  9. name = "sequence-generator",
  10. sequenceName = "product_sequence",
  11. allocationSize = 5
  12. )
  13. private Long id;
  14. @Column(name = "product_name")
  15. private String name;
  16. //Getters and setters are omitted for brevity
  17. }

2.6.10. Using IDENTITY columns

For implementing identifier value generation based on IDENTITY columns, Hibernate makes use of its org.hibernate.id.IdentityGenerator id generator which expects the identifier to be generated by INSERT into the table. IdentityGenerator understands 3 different ways that the INSERT-generated value might be retrieved:

  • If Hibernate believes the JDBC environment supports java.sql.Statement#getGeneratedKeys, then that approach will be used for extracting the IDENTITY generated keys.

  • Otherwise, if Dialect#supportsInsertSelectIdentity reports true, Hibernate will use the Dialect specific INSERT+SELECT statement syntax.

  • Otherwise, Hibernate will expect that the database supports some form of asking for the most recently inserted IDENTITY value via a separate SQL command as indicated by Dialect#getIdentitySelectString.

It is important to realize that using IDENTITY columns imposes a runtime behavior where the entity row must be physically inserted prior to the identifier value being known.

This can mess up extended persistence contexts (long conversations). Because of the runtime imposition/inconsistency, Hibernate suggests other forms of identifier value generation be used (e.g. SEQUENCE).

There is yet another important runtime impact of choosing IDENTITY generation: Hibernate will not be able to batch INSERT statements for the entities using the IDENTITY generation.

The importance of this depends on the application-specific use cases. If the application is not usually creating many new instances of a given entity type using the IDENTITY generator, then this limitation will be less important since batching would not have been very helpful anyway.

2.6.11. Using the table identifier generator

Hibernate achieves table-based identifier generation based on its org.hibernate.id.enhanced.TableGenerator which defines a table capable of holding multiple named value segments for any number of entities.

The basic idea is that a given table-generator table (hibernate_sequences for example) can hold multiple segments of identifier generation values.

Example 144. Unnamed table generator

  1. @Entity(name = "Product")
  2. public static class Product {
  3. @Id
  4. @GeneratedValue(
  5. strategy = GenerationType.TABLE
  6. )
  7. private Long id;
  8. @Column(name = "product_name")
  9. private String name;
  10. //Getters and setters are omitted for brevity
  11. }
  1. create table hibernate_sequences (
  2. sequence_name varchar2(255 char) not null,
  3. next_val number(19,0),
  4. primary key (sequence_name)
  5. )

If no table name is given Hibernate assumes an implicit name of hibernate_sequences.

Additionally, because no javax.persistence.TableGenerator#pkColumnValue is specified, Hibernate will use the default segment (sequence_name='default') from the hibernate_sequences table.

However, you can configure the table identifier generator using the @TableGenerator annotation.

Example 145. Configured table generator

  1. @Entity(name = "Product")
  2. public static class Product {
  3. @Id
  4. @GeneratedValue(
  5. strategy = GenerationType.TABLE,
  6. generator = "table-generator"
  7. )
  8. @TableGenerator(
  9. name = "table-generator",
  10. table = "table_identifier",
  11. pkColumnName = "table_name",
  12. valueColumnName = "product_id",
  13. allocationSize = 5
  14. )
  15. private Long id;
  16. @Column(name = "product_name")
  17. private String name;
  18. //Getters and setters are omitted for brevity
  19. }
  1. create table table_identifier (
  2. table_name varchar2(255 char) not null,
  3. product_id number(19,0),
  4. primary key (table_name)
  5. )

Now, when inserting 3 Product entities, Hibernate generates the following statements:

Example 146. Configured table generator persist example

  1. for ( long i = 1; i <= 3; i++ ) {
  2. Product product = new Product();
  3. product.setName( String.format( "Product %d", i ) );
  4. entityManager.persist( product );
  5. }
  1. select
  2. tbl.product_id
  3. from
  4. table_identifier tbl
  5. where
  6. tbl.table_name = ?
  7. for update
  8. -- binding parameter [1] - [Product]
  9. insert
  10. into
  11. table_identifier
  12. (table_name, product_id)
  13. values
  14. (?, ?)
  15. -- binding parameter [1] - [Product]
  16. -- binding parameter [2] - [1]
  17. update
  18. table_identifier
  19. set
  20. product_id= ?
  21. where
  22. product_id= ?
  23. and table_name= ?
  24. -- binding parameter [1] - [6]
  25. -- binding parameter [2] - [1]
  26. select
  27. tbl.product_id
  28. from
  29. table_identifier tbl
  30. where
  31. tbl.table_name= ? for update
  32. update
  33. table_identifier
  34. set
  35. product_id= ?
  36. where
  37. product_id= ?
  38. and table_name= ?
  39. -- binding parameter [1] - [11]
  40. -- binding parameter [2] - [6]
  41. insert
  42. into
  43. Product
  44. (product_name, id)
  45. values
  46. (?, ?)
  47. -- binding parameter [1] as [VARCHAR] - [Product 1]
  48. -- binding parameter [2] as [BIGINT] - [1]
  49. insert
  50. into
  51. Product
  52. (product_name, id)
  53. values
  54. (?, ?)
  55. -- binding parameter [1] as [VARCHAR] - [Product 2]
  56. -- binding parameter [2] as [BIGINT] - [2]
  57. insert
  58. into
  59. Product
  60. (product_name, id)
  61. values
  62. (?, ?)
  63. -- binding parameter [1] as [VARCHAR] - [Product 3]
  64. -- binding parameter [2] as [BIGINT] - [3]

2.6.12. Using UUID generation

As mentioned above, Hibernate supports UUID identifier value generation. This is supported through its org.hibernate.id.UUIDGenerator id generator.

UUIDGenerator supports pluggable strategies for exactly how the UUID is generated. These strategies are defined by the org.hibernate.id.UUIDGenerationStrategy contract. The default strategy is a version 4 (random) strategy according to IETF RFC 4122. Hibernate does ship with an alternative strategy which is a RFC 4122 version 1 (time-based) strategy (using IP address rather than mac address).

Example 147. Implicitly using the random UUID strategy

  1. @Entity(name = "Book")
  2. public static class Book {
  3. @Id
  4. @GeneratedValue
  5. private UUID id;
  6. private String title;
  7. private String author;
  8. //Getters and setters are omitted for brevity
  9. }

To specify an alternative generation strategy, we’d have to define some configuration via @GenericGenerator. Here we choose the RFC 4122 version 1 compliant strategy named org.hibernate.id.uuid.CustomVersionOneStrategy.

Example 148. Implicitly using the random UUID strategy

  1. @Entity(name = "Book")
  2. public static class Book {
  3. @Id
  4. @GeneratedValue( generator = "custom-uuid" )
  5. @GenericGenerator(
  6. name = "custom-uuid",
  7. strategy = "org.hibernate.id.UUIDGenerator",
  8. parameters = {
  9. @Parameter(
  10. name = "uuid_gen_strategy_class",
  11. value = "org.hibernate.id.uuid.CustomVersionOneStrategy"
  12. )
  13. }
  14. )
  15. private UUID id;
  16. private String title;
  17. private String author;
  18. //Getters and setters are omitted for brevity
  19. }

2.6.13. Optimizers

Most of the Hibernate generators that separately obtain identifier values from database structures support the use of pluggable optimizers. Optimizers help manage the number of times Hibernate has to talk to the database in order to generate identifier values. For example, with no optimizer applied to a sequence-generator, every time the application asked Hibernate to generate an identifier it would need to grab the next sequence value from the database. But if we can minimize the number of times we need to communicate with the database here, the application will be able to perform better, which is, in fact, the role of these optimizers.

none

No optimization is performed. We communicate with the database each and every time an identifier value is needed from the generator.

pooled-lo

The pooled-lo optimizer works on the principle that the increment-value is encoded into the database table/sequence structure. In sequence-terms, this means that the sequence is defined with a greater-than-1 increment size.

For example, consider a brand new sequence defined as create sequence m_sequence start with 1 increment by 20. This sequence essentially defines a “pool” of 20 usable id values each and every time we ask it for its next-value. The pooled-lo optimizer interprets the next-value as the low end of that pool.

So when we first ask it for next-value, we’d get 1. We then assume that the valid pool would be the values from 1-20 inclusive.

The next call to the sequence would result in 21, which would define 21-40 as the valid range. And so on. The “lo” part of the name indicates that the value from the database table/sequence is interpreted as the pool lo(w) end.

pooled

Just like pooled-lo, except that here the value from the table/sequence is interpreted as the high end of the value pool.

hilo; legacy-hilo

Define a custom algorithm for generating pools of values based on a single value from a table or sequence.

These optimizers are not recommended for use. They are maintained (and mentioned) here simply for use by legacy applications that used these strategies previously.

Applications can also implement and use their own optimizer strategies, as defined by the org.hibernate.id.enhanced.Optimizer contract.

2.6.14. Using @GenericGenerator

@GenericGenerator allows integration of any Hibernate org.hibernate.id.IdentifierGenerator implementation, including any of the specific ones discussed here and any custom ones.

To make use of the pooled or pooled-lo optimizers, the entity mapping must use the @GenericGenerator annotation:

Example 149. Pooled-lo optimizer mapping using @GenericGenerator mapping

  1. @Entity(name = "Product")
  2. public static class Product {
  3. @Id
  4. @GeneratedValue(
  5. strategy = GenerationType.SEQUENCE,
  6. generator = "product_generator"
  7. )
  8. @GenericGenerator(
  9. name = "product_generator",
  10. strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator",
  11. parameters = {
  12. @Parameter(name = "sequence_name", value = "product_sequence"),
  13. @Parameter(name = "initial_value", value = "1"),
  14. @Parameter(name = "increment_size", value = "3"),
  15. @Parameter(name = "optimizer", value = "pooled-lo")
  16. }
  17. )
  18. private Long id;
  19. @Column(name = "p_name")
  20. private String name;
  21. @Column(name = "p_number")
  22. private String number;
  23. //Getters and setters are omitted for brevity
  24. }

Now, when saving 5 Person entities and flushing the Persistence Context after every 3 entities:

Example 150. Pooled-lo optimizer mapping using @GenericGenerator mapping

  1. for ( long i = 1; i <= 5; i++ ) {
  2. if(i % 3 == 0) {
  3. entityManager.flush();
  4. }
  5. Product product = new Product();
  6. product.setName( String.format( "Product %d", i ) );
  7. product.setNumber( String.format( "P_100_%d", i ) );
  8. entityManager.persist( product );
  9. }
  1. CALL NEXT VALUE FOR product_sequence
  2. INSERT INTO Product (p_name, p_number, id)
  3. VALUES (?, ?, ?)
  4. -- binding parameter [1] as [VARCHAR] - [Product 1]
  5. -- binding parameter [2] as [VARCHAR] - [P_100_1]
  6. -- binding parameter [3] as [BIGINT] - [1]
  7. INSERT INTO Product (p_name, p_number, id)
  8. VALUES (?, ?, ?)
  9. -- binding parameter [1] as [VARCHAR] - [Product 2]
  10. -- binding parameter [2] as [VARCHAR] - [P_100_2]
  11. -- binding parameter [3] as [BIGINT] - [2]
  12. CALL NEXT VALUE FOR product_sequence
  13. INSERT INTO Product (p_name, p_number, id)
  14. VALUES (?, ?, ?)
  15. -- binding parameter [1] as [VARCHAR] - [Product 3]
  16. -- binding parameter [2] as [VARCHAR] - [P_100_3]
  17. -- binding parameter [3] as [BIGINT] - [3]
  18. INSERT INTO Product (p_name, p_number, id)
  19. VALUES (?, ?, ?)
  20. -- binding parameter [1] as [VARCHAR] - [Product 4]
  21. -- binding parameter [2] as [VARCHAR] - [P_100_4]
  22. -- binding parameter [3] as [BIGINT] - [4]
  23. INSERT INTO Product (p_name, p_number, id)
  24. VALUES (?, ?, ?)
  25. -- binding parameter [1] as [VARCHAR] - [Product 5]
  26. -- binding parameter [2] as [VARCHAR] - [P_100_5]
  27. -- binding parameter [3] as [BIGINT] - [5]

As you can see from the list of generated SQL statements, you can insert 3 entities with just one database sequence call. This way, the pooled and the pooled-lo optimizers allow you to reduce the number of database roundtrips, therefore reducing the overall transaction response time.

2.6.15. Derived Identifiers

JPA 2.0 added support for derived identifiers which allow an entity to borrow the identifier from a many-to-one or one-to-one association.

Example 151. Derived identifier with @MapsId

  1. @Entity(name = "Person")
  2. public static class Person {
  3. @Id
  4. private Long id;
  5. @NaturalId
  6. private String registrationNumber;
  7. public Person() {}
  8. public Person(String registrationNumber) {
  9. this.registrationNumber = registrationNumber;
  10. }
  11. //Getters and setters are omitted for brevity
  12. }
  13. @Entity(name = "PersonDetails")
  14. public static class PersonDetails {
  15. @Id
  16. private Long id;
  17. private String nickName;
  18. @OneToOne
  19. @MapsId
  20. private Person person;
  21. //Getters and setters are omitted for brevity
  22. }

In the example above, the PersonDetails entity uses the id column for both the entity identifier and for the one-to-one association to the Person entity. The value of the PersonDetails entity identifier is “derived” from the identifier of its parent Person entity.

Example 152. Derived identifier with @MapsId persist example

doInJPA( this::entityManagerFactory, entityManager -> {
    Person person = new Person( "ABC-123" );
    person.setId( 1L );
    entityManager.persist( person );

    PersonDetails personDetails = new PersonDetails();
    personDetails.setNickName( "John Doe" );
    personDetails.setPerson( person );

    entityManager.persist( personDetails );
} );

doInJPA( this::entityManagerFactory, entityManager -> {
    PersonDetails personDetails = entityManager.find( PersonDetails.class, 1L );

    assertEquals("John Doe", personDetails.getNickName());
} );

The @MapsId annotation can also reference columns from an @EmbeddedId identifier as well.

The previous example can also be mapped using @PrimaryKeyJoinColumn.

Example 153. Derived identifier @PrimaryKeyJoinColumn

@Entity(name = "Person")
public static class Person  {

    @Id
    private Long id;

    @NaturalId
    private String registrationNumber;

    public Person() {}

    public Person(String registrationNumber) {
        this.registrationNumber = registrationNumber;
    }

    //Getters and setters are omitted for brevity
}

@Entity(name = "PersonDetails")
public static class PersonDetails  {

    @Id
    private Long id;

    private String nickName;

    @OneToOne
    @PrimaryKeyJoinColumn
    private Person person;

    public void setPerson(Person person) {
        this.person = person;
        this.id = person.getId();
    }

    //Other getters and setters are omitted for brevity
}

Unlike @MapsId, the application developer is responsible for ensuring that the entity identifier and the many-to-one (or one-to-one) association are in sync, as you can see in the PersonDetails#setPerson method.

2.6.16. @RowId

If you annotate a given entity with the @RowId annotation and the underlying database supports fetching a record by ROWID (e.g. Oracle), then Hibernate can use the ROWID pseudo-column for CRUD operations.

Example 154. @RowId entity mapping

@Entity(name = "Product")
@RowId("ROWID")
public static class Product {

    @Id
    private Long id;

    @Column(name = "`name`")
    private String name;

    @Column(name = "`number`")
    private String number;

    //Getters and setters are omitted for brevity

}

Now, when fetching an entity and modifying it, Hibernate uses the ROWID pseudo-column for the UPDATE SQL statement.

Example 155. @RowId example

Product product = entityManager.find( Product.class, 1L );

product.setName( "Smart phone" );
SELECT
    p.id as id1_0_0_,
    p."name" as name2_0_0_,
    p."number" as number3_0_0_,
    p.ROWID as rowid_0_
FROM
    Product p
WHERE
    p.id = ?

-- binding parameter [1] as [BIGINT] - [1]

-- extracted value ([name2_0_0_] : [VARCHAR]) - [Mobile phone]
-- extracted value ([number3_0_0_] : [VARCHAR]) - [123-456-7890]
-- extracted ROWID value: AAAwkBAAEAAACP3AAA

UPDATE
    Product
SET
    "name" = ?,
    "number" = ?
WHERE
    ROWID = ?

-- binding parameter [1] as [VARCHAR] - [Smart phone]
-- binding parameter [2] as [VARCHAR] - [123-456-7890]
-- binding parameter [3] as ROWID     - [AAAwkBAAEAAACP3AAA]

2.7. Associations

Associations describe how two or more entities form a relationship based on a database joining semantics.

2.7.1. @ManyToOne

@ManyToOne is the most common association, having a direct equivalent in the relational database as well (e.g. foreign key), and so it establishes a relationship between a child entity and a parent.

Example 156. @ManyToOne association

@Entity(name = "Person")
public static class Person {

    @Id
    @GeneratedValue
    private Long id;

    //Getters and setters are omitted for brevity

}

@Entity(name = "Phone")
public static class Phone {

    @Id
    @GeneratedValue
    private Long id;

    @Column(name = "`number`")
    private String number;

    @ManyToOne
    @JoinColumn(name = "person_id",
            foreignKey = @ForeignKey(name = "PERSON_ID_FK")
    )
    private Person person;

    //Getters and setters are omitted for brevity

}
CREATE TABLE Person (
    id BIGINT NOT NULL ,
    PRIMARY KEY ( id )
)

CREATE TABLE Phone (
    id BIGINT NOT NULL ,
    number VARCHAR(255) ,
    person_id BIGINT ,
    PRIMARY KEY ( id )
 )

ALTER TABLE Phone
ADD CONSTRAINT PERSON_ID_FK
FOREIGN KEY (person_id) REFERENCES Person

Each entity has a lifecycle of its own. Once the @ManyToOne association is set, Hibernate will set the associated database foreign key column.

Example 157. @ManyToOne association lifecycle

Person person = new Person();
entityManager.persist( person );

Phone phone = new Phone( "123-456-7890" );
phone.setPerson( person );
entityManager.persist( phone );

entityManager.flush();
phone.setPerson( null );
INSERT INTO Person ( id )
VALUES ( 1 )

INSERT INTO Phone ( number, person_id, id )
VALUES ( '123-456-7890', 1, 2 )

UPDATE Phone
SET    number = '123-456-7890',
       person_id = NULL
WHERE  id = 2

2.7.2. @OneToMany

The @OneToMany association links a parent entity with one or more child entities. If the @OneToMany doesn’t have a mirroring @ManyToOne association on the child side, the @OneToMany association is unidirectional. If there is a @ManyToOne association on the child side, the @OneToMany association is bidirectional and the application developer can navigate this relationship from both ends.

Unidirectional @OneToMany

When using a unidirectional @OneToMany association, Hibernate resorts to using a link table between the two joining entities.

Example 158. Unidirectional @OneToMany association

@Entity(name = "Person")
public static class Person {

    @Id
    @GeneratedValue
    private Long id;

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    private List<Phone> phones = new ArrayList<>();

    //Getters and setters are omitted for brevity

}

@Entity(name = "Phone")
public static class Phone {

    @Id
    @GeneratedValue
    private Long id;

    @Column(name = "`number`")
    private String number;

    //Getters and setters are omitted for brevity

}
CREATE TABLE Person (
    id BIGINT NOT NULL ,
    PRIMARY KEY ( id )
)

CREATE TABLE Person_Phone (
    Person_id BIGINT NOT NULL ,
    phones_id BIGINT NOT NULL
)

CREATE TABLE Phone (
    id BIGINT NOT NULL ,
    number VARCHAR(255) ,
    PRIMARY KEY ( id )
)

ALTER TABLE Person_Phone
ADD CONSTRAINT UK_9uhc5itwc9h5gcng944pcaslf
UNIQUE (phones_id)

ALTER TABLE Person_Phone
ADD CONSTRAINT FKr38us2n8g5p9rj0b494sd3391
FOREIGN KEY (phones_id) REFERENCES Phone

ALTER TABLE Person_Phone
ADD CONSTRAINT FK2ex4e4p7w1cj310kg2woisjl2
FOREIGN KEY (Person_id) REFERENCES Person

The @OneToMany association is by definition a parent association, regardless of whether it’s a unidirectional or a bidirectional one. Only the parent side of an association makes sense to cascade its entity state transitions to children.

Example 159. Cascading @OneToMany association

Person person = new Person();
Phone phone1 = new Phone( "123-456-7890" );
Phone phone2 = new Phone( "321-654-0987" );

person.getPhones().add( phone1 );
person.getPhones().add( phone2 );
entityManager.persist( person );
entityManager.flush();

person.getPhones().remove( phone1 );
INSERT INTO Person
       ( id )
VALUES ( 1 )

INSERT INTO Phone
       ( number, id )
VALUES ( '123-456-7890', 2 )

INSERT INTO Phone
       ( number, id )
VALUES ( '321-654-0987', 3 )

INSERT INTO Person_Phone
       ( Person_id, phones_id )
VALUES ( 1, 2 )

INSERT INTO Person_Phone
       ( Person_id, phones_id )
VALUES ( 1, 3 )

DELETE FROM Person_Phone
WHERE  Person_id = 1

INSERT INTO Person_Phone
       ( Person_id, phones_id )
VALUES ( 1, 3 )

DELETE FROM Phone
WHERE  id = 2

When persisting the Person entity, the cascade will propagate the persist operation to the underlying Phone children as well. Upon removing a Phone from the phones collection, the association row is deleted from the link table, and the orphanRemoval attribute will trigger a Phone removal as well.

The unidirectional associations are not very efficient when it comes to removing child entities. In the example above, upon flushing the persistence context, Hibernate deletes all database rows from the link table (e.g. Person_Phone) that are associated with the parent Person entity and reinserts the ones that are still found in the @OneToMany collection.

On the other hand, a bidirectional @OneToMany association is much more efficient because the child entity controls the association.

Bidirectional @OneToMany

The bidirectional @OneToMany association also requires a @ManyToOne association on the child side. Although the Domain Model exposes two sides to navigate this association, behind the scenes, the relational database has only one foreign key for this relationship.

Every bidirectional association must have one owning side only (the child side), the other one being referred to as the inverse (or the mappedBy) side.

Example 160. @OneToMany association mappedBy the @ManyToOne side

@Entity(name = "Person")
public static class Person {

    @Id
    @GeneratedValue
    private Long id;

    @OneToMany(mappedBy = "person", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<Phone> phones = new ArrayList<>();

    //Getters and setters are omitted for brevity

    public void addPhone(Phone phone) {
        phones.add( phone );
        phone.setPerson( this );
    }

    public void removePhone(Phone phone) {
        phones.remove( phone );
        phone.setPerson( null );
    }
}

@Entity(name = "Phone")
public static class Phone {

    @Id
    @GeneratedValue
    private Long id;

    @NaturalId
    @Column(name = "`number`", unique = true)
    private String number;

    @ManyToOne
    private Person person;

    //Getters and setters are omitted for brevity

    @Override
    public boolean equals(Object o) {
        if ( this == o ) {
            return true;
        }
        if ( o == null || getClass() != o.getClass() ) {
            return false;
        }
        Phone phone = (Phone) o;
        return Objects.equals( number, phone.number );
    }

    @Override
    public int hashCode() {
        return Objects.hash( number );
    }
}
CREATE TABLE Person (
    id BIGINT NOT NULL ,
    PRIMARY KEY ( id )
)

CREATE TABLE Phone (
    id BIGINT NOT NULL ,
    number VARCHAR(255) ,
    person_id BIGINT ,
    PRIMARY KEY ( id )
)

ALTER TABLE Phone
ADD CONSTRAINT UK_l329ab0g4c1t78onljnxmbnp6
UNIQUE (number)

ALTER TABLE Phone
ADD CONSTRAINT FKmw13yfsjypiiq0i1osdkaeqpg
FOREIGN KEY (person_id) REFERENCES Person

Whenever a bidirectional association is formed, the application developer must make sure both sides are in-sync at all times.

The addPhone() and removePhone() are utility methods that synchronize both ends whenever a child element is added or removed.

Because the Phone class has a @NaturalId column (the phone number being unique), the equals() and the hashCode() can make use of this property, and so the removePhone() logic is reduced to the remove() Java Collection method.

Example 161. Bidirectional @OneToMany with an owner @ManyToOne side lifecycle

Person person = new Person();
Phone phone1 = new Phone( "123-456-7890" );
Phone phone2 = new Phone( "321-654-0987" );

person.addPhone( phone1 );
person.addPhone( phone2 );
entityManager.persist( person );
entityManager.flush();

person.removePhone( phone1 );
INSERT INTO Person
       ( id )
VALUES ( 1 )

INSERT INTO Phone
       ( "number", person_id, id )
VALUES ( '123-456-7890', 1, 2 )

INSERT INTO Phone
       ( "number", person_id, id )
VALUES ( '321-654-0987', 1, 3 )

DELETE FROM Phone
WHERE  id = 2

Unlike the unidirectional @OneToMany, the bidirectional association is much more efficient when managing the collection persistence state. Every element removal only requires a single update (in which the foreign key column is set to NULL), and, if the child entity lifecycle is bound to its owning parent so that the child cannot exist without its parent, then we can annotate the association with the orphanRemoval attribute and dissociating the child will trigger a delete statement on the actual child table row as well.

2.7.3. @OneToOne

The @OneToOne association can either be unidirectional or bidirectional. A unidirectional association follows the relational database foreign key semantics, the client-side owning the relationship. A bidirectional association features a mappedBy @OneToOne parent side too.

Unidirectional @OneToOne

Example 162. Unidirectional @OneToOne

@Entity(name = "Phone")
public static class Phone {

    @Id
    @GeneratedValue
    private Long id;

    @Column(name = "`number`")
    private String number;

    @OneToOne
    @JoinColumn(name = "details_id")
    private PhoneDetails details;

    //Getters and setters are omitted for brevity

}

@Entity(name = "PhoneDetails")
public static class PhoneDetails {

    @Id
    @GeneratedValue
    private Long id;

    private String provider;

    private String technology;

    //Getters and setters are omitted for brevity

}
CREATE TABLE Phone (
    id BIGINT NOT NULL ,
    number VARCHAR(255) ,
    details_id BIGINT ,
    PRIMARY KEY ( id )
)

CREATE TABLE PhoneDetails (
    id BIGINT NOT NULL ,
    provider VARCHAR(255) ,
    technology VARCHAR(255) ,
    PRIMARY KEY ( id )
)

ALTER TABLE Phone
ADD CONSTRAINT FKnoj7cj83ppfqbnvqqa5kolub7
FOREIGN KEY (details_id) REFERENCES PhoneDetails

From a relational database point of view, the underlying schema is identical to the unidirectional @ManyToOne association, as the client-side controls the relationship based on the foreign key column.

But then, it’s unusual to consider the Phone as a client-side and the PhoneDetails as the parent-side because the details cannot exist without an actual phone. A much more natural mapping would be the Phone were the parent-side, therefore pushing the foreign key into the PhoneDetails table. This mapping requires a bidirectional @OneToOne association as you can see in the following example:

Bidirectional @OneToOne

Example 163. Bidirectional @OneToOne

@Entity(name = "Phone")
public static class Phone {

    @Id
    @GeneratedValue
    private Long id;

    @Column(name = "`number`")
    private String number;

    @OneToOne(
        mappedBy = "phone",
        cascade = CascadeType.ALL,
        orphanRemoval = true,
        fetch = FetchType.LAZY
    )
    private PhoneDetails details;

    //Getters and setters are omitted for brevity

    public void addDetails(PhoneDetails details) {
        details.setPhone( this );
        this.details = details;
    }

    public void removeDetails() {
        if ( details != null ) {
            details.setPhone( null );
            this.details = null;
        }
    }
}

@Entity(name = "PhoneDetails")
public static class PhoneDetails {

    @Id
    @GeneratedValue
    private Long id;

    private String provider;

    private String technology;

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "phone_id")
    private Phone phone;

    //Getters and setters are omitted for brevity

}
CREATE TABLE Phone (
    id BIGINT NOT NULL ,
    number VARCHAR(255) ,
    PRIMARY KEY ( id )
)

CREATE TABLE PhoneDetails (
    id BIGINT NOT NULL ,
    provider VARCHAR(255) ,
    technology VARCHAR(255) ,
    phone_id BIGINT ,
    PRIMARY KEY ( id )
)

ALTER TABLE PhoneDetails
ADD CONSTRAINT FKeotuev8ja8v0sdh29dynqj05p
FOREIGN KEY (phone_id) REFERENCES Phone

This time, the PhoneDetails owns the association, and, like any bidirectional association, the parent-side can propagate its lifecycle to the child-side through cascading.

Example 164. Bidirectional @OneToOne lifecycle

Phone phone = new Phone( "123-456-7890" );
PhoneDetails details = new PhoneDetails( "T-Mobile", "GSM" );

phone.addDetails( details );
entityManager.persist( phone );
INSERT INTO Phone ( number, id )
VALUES ( '123-456-7890', 1 )

INSERT INTO PhoneDetails ( phone_id, provider, technology, id )
VALUES ( 1, 'T-Mobile', 'GSM', 2 )

When using a bidirectional @OneToOne association, Hibernate enforces the unique constraint upon fetching the child-side. If there are more than one children associated with the same parent, Hibernate will throw a org.hibernate.exception.ConstraintViolationException. Continuing the previous example, when adding another PhoneDetails, Hibernate validates the uniqueness constraint when reloading the Phone object.

Example 165. Bidirectional @OneToOne unique constraint

PhoneDetails otherDetails = new PhoneDetails( "T-Mobile", "CDMA" );
otherDetails.setPhone( phone );
entityManager.persist( otherDetails );
entityManager.flush();
entityManager.clear();

//throws javax.persistence.PersistenceException: org.hibernate.HibernateException: More than one row with the given identifier was found: 1
phone = entityManager.find( Phone.class, phone.getId() );
Bidirectional @OneToOne lazy association

Although you might annotate the parent-side association to be fetched lazily, Hibernate cannot honor this request since it cannot know whether the association is null or not.

The only way to figure out whether there is an associated record on the child side is to fetch the child association using a secondary query. Because this can lead to N+1 query issues, it’s much more efficient to use unidirectional @OneToOne associations with the @MapsId annotation in place.

However, if you really need to use a bidirectional association and want to make sure that this is always going to be fetched lazily, then you need to enable lazy state initialization bytecode enhancement and use the @LazyToOne annotation as well.

Example 166. Bidirectional @OneToOne lazy parent-side association

@Entity(name = "Phone")
public static class Phone {

    @Id
    @GeneratedValue
    private Long id;

    @Column(name = "`number`")
    private String number;

    @OneToOne(
        mappedBy = "phone",
        cascade = CascadeType.ALL,
        orphanRemoval = true,
        fetch = FetchType.LAZY
    )
    @LazyToOne( LazyToOneOption.NO_PROXY )
    private PhoneDetails details;

    //Getters and setters are omitted for brevity

    public void addDetails(PhoneDetails details) {
        details.setPhone( this );
        this.details = details;
    }

    public void removeDetails() {
        if ( details != null ) {
            details.setPhone( null );
            this.details = null;
        }
    }
}

@Entity(name = "PhoneDetails")
public static class PhoneDetails {

    @Id
    @GeneratedValue
    private Long id;

    private String provider;

    private String technology;

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "phone_id")
    private Phone phone;

    //Getters and setters are omitted for brevity

}

For more about how to enable Bytecode enhancement, see the Bytecode Enhancement chapter.

2.7.4. @ManyToMany

The @ManyToMany association requires a link table that joins two entities. Like the @OneToMany association, @ManyToMany can be either unidirectional or bidirectional.

Unidirectional @ManyToMany

Example 167. Unidirectional @ManyToMany

@Entity(name = "Person")
public static class Person {

    @Id
    @GeneratedValue
    private Long id;

    @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    private List<Address> addresses = new ArrayList<>();

    //Getters and setters are omitted for brevity

}

@Entity(name = "Address")
public static class Address {

    @Id
    @GeneratedValue
    private Long id;

    private String street;

    @Column(name = "`number`")
    private String number;

    //Getters and setters are omitted for brevity

}
CREATE TABLE Address (
    id BIGINT NOT NULL ,
    number VARCHAR(255) ,
    street VARCHAR(255) ,
    PRIMARY KEY ( id )
)

CREATE TABLE Person (
    id BIGINT NOT NULL ,
    PRIMARY KEY ( id )
)

CREATE TABLE Person_Address (
    Person_id BIGINT NOT NULL ,
    addresses_id BIGINT NOT NULL
)

ALTER TABLE Person_Address
ADD CONSTRAINT FKm7j0bnabh2yr0pe99il1d066u
FOREIGN KEY (addresses_id) REFERENCES Address

ALTER TABLE Person_Address
ADD CONSTRAINT FKba7rc9qe2vh44u93u0p2auwti
FOREIGN KEY (Person_id) REFERENCES Person

Just like with unidirectional @OneToMany associations, the link table is controlled by the owning side.

When an entity is removed from the @ManyToMany collection, Hibernate simply deletes the joining record in the link table. Unfortunately, this operation requires removing all entries associated with a given parent and recreating the ones that are listed in the current running persistent context.

Example 168. Unidirectional @ManyToMany lifecycle

Person person1 = new Person();
Person person2 = new Person();

Address address1 = new Address( "12th Avenue", "12A" );
Address address2 = new Address( "18th Avenue", "18B" );

person1.getAddresses().add( address1 );
person1.getAddresses().add( address2 );

person2.getAddresses().add( address1 );

entityManager.persist( person1 );
entityManager.persist( person2 );

entityManager.flush();

person1.getAddresses().remove( address1 );
INSERT INTO Person ( id )
VALUES ( 1 )

INSERT INTO Address ( number, street, id )
VALUES ( '12A', '12th Avenue', 2 )

INSERT INTO Address ( number, street, id )
VALUES ( '18B', '18th Avenue', 3 )

INSERT INTO Person ( id )
VALUES ( 4 )

INSERT INTO Person_Address ( Person_id, addresses_id )
VALUES ( 1, 2 )
INSERT INTO Person_Address ( Person_id, addresses_id )
VALUES ( 1, 3 )
INSERT INTO Person_Address ( Person_id, addresses_id )
VALUES ( 4, 2 )

DELETE FROM Person_Address
WHERE  Person_id = 1

INSERT INTO Person_Address ( Person_id, addresses_id )
VALUES ( 1, 3 )

For @ManyToMany associations, the REMOVE entity state transition doesn’t make sense to be cascaded because it will propagate beyond the link table. Since the other side might be referenced by other entities on the parent-side, the automatic removal might end up in a ConstraintViolationException.

For example, if @ManyToMany(cascade = CascadeType.ALL) was defined and the first person would be deleted, Hibernate would throw an exception because another person is still associated with the address that’s being deleted.

Person person1 = entityManager.find(Person.class, personId);
entityManager.remove(person1);

Caused by: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement
Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement
Caused by: java.sql.SQLIntegrityConstraintViolationException: integrity constraint violation: foreign key no action; FKM7J0BNABH2YR0PE99IL1D066U table: PERSON_ADDRESS

By simply removing the parent-side, Hibernate can safely remove the associated link records as you can see in the following example:

Example 169. Unidirectional @ManyToMany entity removal

Person person1 = entityManager.find( Person.class, personId );
entityManager.remove( person1 );
DELETE FROM Person_Address
WHERE  Person_id = 1

DELETE FROM Person
WHERE  id = 1
Bidirectional @ManyToMany

A bidirectional @ManyToMany association has an owning and a mappedBy side. To preserve synchronicity between both sides, it’s good practice to provide helper methods for adding or removing child entities.

Example 170. Bidirectional @ManyToMany

@Entity(name = "Person")
public static class Person {

    @Id
    @GeneratedValue
    private Long id;

    @NaturalId
    private String registrationNumber;

    @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    private List<Address> addresses = new ArrayList<>();

    //Getters and setters are omitted for brevity

    public void addAddress(Address address) {
        addresses.add( address );
        address.getOwners().add( this );
    }

    public void removeAddress(Address address) {
        addresses.remove( address );
        address.getOwners().remove( this );
    }

    @Override
    public boolean equals(Object o) {
        if ( this == o ) {
            return true;
        }
        if ( o == null || getClass() != o.getClass() ) {
            return false;
        }
        Person person = (Person) o;
        return Objects.equals( registrationNumber, person.registrationNumber );
    }

    @Override
    public int hashCode() {
        return Objects.hash( registrationNumber );
    }
}

@Entity(name = "Address")
public static class Address {

    @Id
    @GeneratedValue
    private Long id;

    private String street;

    @Column(name = "`number`")
    private String number;

    private String postalCode;

    @ManyToMany(mappedBy = "addresses")
    private List<Person> owners = new ArrayList<>();

    //Getters and setters are omitted for brevity

    @Override
    public boolean equals(Object o) {
        if ( this == o ) {
            return true;
        }
        if ( o == null || getClass() != o.getClass() ) {
            return false;
        }
        Address address = (Address) o;
        return Objects.equals( street, address.street ) &&
                Objects.equals( number, address.number ) &&
                Objects.equals( postalCode, address.postalCode );
    }

    @Override
    public int hashCode() {
        return Objects.hash( street, number, postalCode );
    }
}
CREATE TABLE Address (
    id BIGINT NOT NULL ,
    number VARCHAR(255) ,
    postalCode VARCHAR(255) ,
    street VARCHAR(255) ,
    PRIMARY KEY ( id )
)

CREATE TABLE Person (
    id BIGINT NOT NULL ,
    registrationNumber VARCHAR(255) ,
    PRIMARY KEY ( id )
)

CREATE TABLE Person_Address (
    owners_id BIGINT NOT NULL ,
    addresses_id BIGINT NOT NULL
)

ALTER TABLE Person
ADD CONSTRAINT UK_23enodonj49jm8uwec4i7y37f
UNIQUE (registrationNumber)

ALTER TABLE Person_Address
ADD CONSTRAINT FKm7j0bnabh2yr0pe99il1d066u
FOREIGN KEY (addresses_id) REFERENCES Address

ALTER TABLE Person_Address
ADD CONSTRAINT FKbn86l24gmxdv2vmekayqcsgup
FOREIGN KEY (owners_id) REFERENCES Person

With the helper methods in place, the synchronicity management can be simplified, as you can see in the following example:

Example 171. Bidirectional @ManyToMany lifecycle

Person person1 = new Person( "ABC-123" );
Person person2 = new Person( "DEF-456" );

Address address1 = new Address( "12th Avenue", "12A", "4005A" );
Address address2 = new Address( "18th Avenue", "18B", "4007B" );

person1.addAddress( address1 );
person1.addAddress( address2 );

person2.addAddress( address1 );

entityManager.persist( person1 );
entityManager.persist( person2 );

entityManager.flush();

person1.removeAddress( address1 );
INSERT INTO Person ( registrationNumber, id )
VALUES ( 'ABC-123', 1 )

INSERT INTO Address ( number, postalCode, street, id )
VALUES ( '12A', '4005A', '12th Avenue', 2 )

INSERT INTO Address ( number, postalCode, street, id )
VALUES ( '18B', '4007B', '18th Avenue', 3 )

INSERT INTO Person ( registrationNumber, id )
VALUES ( 'DEF-456', 4 )

INSERT INTO Person_Address ( owners_id, addresses_id )
VALUES ( 1, 2 )

INSERT INTO Person_Address ( owners_id, addresses_id )
VALUES ( 1, 3 )

INSERT INTO Person_Address ( owners_id, addresses_id )
VALUES ( 4, 2 )

DELETE FROM Person_Address
WHERE  owners_id = 1

INSERT INTO Person_Address ( owners_id, addresses_id )
VALUES ( 1, 3 )

If a bidirectional @OneToMany association performs better when removing or changing the order of child elements, the @ManyToMany relationship cannot benefit from such an optimization because the foreign key side is not in control. To overcome this limitation, the link table must be directly exposed and the @ManyToMany association split into two bidirectional @OneToMany relationships.

Bidirectional many-to-many with a link entity

To most natural @ManyToMany association follows the same logic employed by the database schema, and the link table has an associated entity which controls the relationship for both sides that need to be joined.

Example 172. Bidirectional many-to-many with link entity

@Entity(name = "Person")
public static class Person implements Serializable {

    @Id
    @GeneratedValue
    private Long id;

    @NaturalId
    private String registrationNumber;

    @OneToMany(
        mappedBy = "person",
        cascade = CascadeType.ALL,
        orphanRemoval = true
    )
    private List<PersonAddress> addresses = new ArrayList<>();

    //Getters and setters are omitted for brevity

    public void addAddress(Address address) {
        PersonAddress personAddress = new PersonAddress( this, address );
        addresses.add( personAddress );
        address.getOwners().add( personAddress );
    }

    public void removeAddress(Address address) {
        PersonAddress personAddress = new PersonAddress( this, address );
        address.getOwners().remove( personAddress );
        addresses.remove( personAddress );
        personAddress.setPerson( null );
        personAddress.setAddress( null );
    }

    @Override
    public boolean equals(Object o) {
        if ( this == o ) {
            return true;
        }
        if ( o == null || getClass() != o.getClass() ) {
            return false;
        }
        Person person = (Person) o;
        return Objects.equals( registrationNumber, person.registrationNumber );
    }

    @Override
    public int hashCode() {
        return Objects.hash( registrationNumber );
    }
}

@Entity(name = "PersonAddress")
public static class PersonAddress implements Serializable {

    @Id
    @ManyToOne
    private Person person;

    @Id
    @ManyToOne
    private Address address;

    //Getters and setters are omitted for brevity

    @Override
    public boolean equals(Object o) {
        if ( this == o ) {
            return true;
        }
        if ( o == null || getClass() != o.getClass() ) {
            return false;
        }
        PersonAddress that = (PersonAddress) o;
        return Objects.equals( person, that.person ) &&
                Objects.equals( address, that.address );
    }

    @Override
    public int hashCode() {
        return Objects.hash( person, address );
    }
}

@Entity(name = "Address")
public static class Address implements Serializable {

    @Id
    @GeneratedValue
    private Long id;

    private String street;

    @Column(name = "`number`")
    private String number;

    private String postalCode;

    @OneToMany(
        mappedBy = "address",
        cascade = CascadeType.ALL,
        orphanRemoval = true
    )
    private List<PersonAddress> owners = new ArrayList<>();

    //Getters and setters are omitted for brevity

    @Override
    public boolean equals(Object o) {
        if ( this == o ) {
            return true;
        }
        if ( o == null || getClass() != o.getClass() ) {
            return false;
        }
        Address address = (Address) o;
        return Objects.equals( street, address.street ) &&
                Objects.equals( number, address.number ) &&
                Objects.equals( postalCode, address.postalCode );
    }

    @Override
    public int hashCode() {
        return Objects.hash( street, number, postalCode );
    }
}
CREATE TABLE Address (
    id BIGINT NOT NULL ,
    number VARCHAR(255) ,
    postalCode VARCHAR(255) ,
    street VARCHAR(255) ,
    PRIMARY KEY ( id )
)

CREATE TABLE Person (
    id BIGINT NOT NULL ,
    registrationNumber VARCHAR(255) ,
    PRIMARY KEY ( id )
)

CREATE TABLE PersonAddress (
    person_id BIGINT NOT NULL ,
    address_id BIGINT NOT NULL ,
    PRIMARY KEY ( person_id, address_id )
)

ALTER TABLE Person
ADD CONSTRAINT UK_23enodonj49jm8uwec4i7y37f
UNIQUE (registrationNumber)

ALTER TABLE PersonAddress
ADD CONSTRAINT FK8b3lru5fyej1aarjflamwghqq
FOREIGN KEY (person_id) REFERENCES Person

ALTER TABLE PersonAddress
ADD CONSTRAINT FK7p69mgialumhegyl4byrh65jk
FOREIGN KEY (address_id) REFERENCES Address

Both the Person and the Address have a mappedBy @OneToMany side, while the PersonAddress owns the person and the address @ManyToOne associations. Because this mapping is formed out of two bidirectional associations, the helper methods are even more relevant.

The aforementioned example uses a Hibernate-specific mapping for the link entity since JPA doesn’t allow building a composite identifier out of multiple @ManyToOne associations.

For more details, see the composite identifiers with associations section.

The entity state transitions are better managed than in the previous bidirectional @ManyToMany case.

Example 173. Bidirectional many-to-many with link entity lifecycle

Person person1 = new Person( "ABC-123" );
Person person2 = new Person( "DEF-456" );

Address address1 = new Address( "12th Avenue", "12A", "4005A" );
Address address2 = new Address( "18th Avenue", "18B", "4007B" );

entityManager.persist( person1 );
entityManager.persist( person2 );

entityManager.persist( address1 );
entityManager.persist( address2 );

person1.addAddress( address1 );
person1.addAddress( address2 );

person2.addAddress( address1 );

entityManager.flush();

log.info( "Removing address" );
person1.removeAddress( address1 );
INSERT  INTO Person ( registrationNumber, id )
VALUES  ( 'ABC-123', 1 )

INSERT  INTO Person ( registrationNumber, id )
VALUES  ( 'DEF-456', 2 )

INSERT  INTO Address ( number, postalCode, street, id )
VALUES  ( '12A', '4005A', '12th Avenue', 3 )

INSERT  INTO Address ( number, postalCode, street, id )
VALUES  ( '18B', '4007B', '18th Avenue', 4 )

INSERT  INTO PersonAddress ( person_id, address_id )
VALUES  ( 1, 3 )

INSERT  INTO PersonAddress ( person_id, address_id )
VALUES  ( 1, 4 )

INSERT  INTO PersonAddress ( person_id, address_id )
VALUES  ( 2, 3 )

DELETE  FROM PersonAddress
WHERE   person_id = 1 AND address_id = 3

There is only one delete statement executed because, this time, the association is controlled by the @ManyToOne side which only has to monitor the state of the underlying foreign key relationship to trigger the right DML statement.

2.7.5. @NotFound association mapping

When dealing with associations which are not enforced by a Foreign Key, it’s possible to bump into inconsistencies if the child record cannot reference a parent entity.

By default, Hibernate will complain whenever a child association references a non-existing parent record. However, you can configure this behavior so that Hibernate can ignore such an Exception and simply assign null as a parent object referenced.

To ignore non-existing parent entity references, even though not really recommended, it’s possible to use the annotation org.hibernate.annotation.NotFound annotation with a value of org.hibernate.annotations.NotFoundAction.IGNORE.

The @ManyToOne and @OneToOne associations that are annotated with @NotFound(action = NotFoundAction.IGNORE) are always fetched eagerly even if the fetch strategy is set to FetchType.LAZY.

Considering the following City and Person entity mappings:

Example 174. @NotFound mapping example

@Entity
@Table( name = "Person" )
public static class Person {

    @Id
    private Long id;

    private String name;

    private String cityName;

    @ManyToOne
    @NotFound ( action = NotFoundAction.IGNORE )
    @JoinColumn(
        name = "cityName",
        referencedColumnName = "name",
        insertable = false,
        updatable = false
    )
    private City city;

    //Getters and setters are omitted for brevity

}

@Entity
@Table( name = "City" )
public static class City implements Serializable {

    @Id
    @GeneratedValue
    private Long id;

    private String name;

    //Getters and setters are omitted for brevity

}

If we have the following entities in our database:

Example 175. @NotFound persist example

City _NewYork = new City();
_NewYork.setName( "New York" );
entityManager.persist( _NewYork );

Person person = new Person();
person.setId( 1L );
person.setName( "John Doe" );
person.setCityName( "New York" );
entityManager.persist( person );

When loading the Person entity, Hibernate is able to locate the associated City parent entity:

Example 176. @NotFound find existing entity example

Person person = entityManager.find( Person.class, 1L );
assertEquals( "New York", person.getCity().getName() );

However, if we change the cityName attribute to a non-existing city’s name:

Example 177. @NotFound change to non-existing City example

person.setCityName( "Atlantis" );

Hibernate is not going to throw any exception, and it will assign a value of null for the non-existing City entity reference:

Example 178. @NotFound find non-existing City example

Person person = entityManager.find( Person.class, 1L );

assertEquals( "Atlantis", person.getCityName() );
assertNull( null, person.getCity() );

2.7.6. @Any mapping

The @Any mapping is useful to emulate a unidirectional @ManyToOne association when there can be multiple target entities.

Because the @Any mapping defines a polymorphic association to classes from multiple tables, this association type requires the FK column which provides the associated parent identifier and a metadata information for the associated entity type.

This is not the usual way of mapping polymorphic associations and you should use this only in special cases (e.g. audit logs, user session data, etc).

The @Any annotation describes the column holding the metadata information. To link the value of the metadata information and an actual entity type, the @AnyDef and @AnyDefs annotations are used. The metaType attribute allows the application to specify a custom type that maps database column values to persistent classes that have identifier properties of the type specified by idType. You must specify the mapping from values of the metaType to class names.

For the next examples, consider the following Property class hierarchy:

Example 179. Property class hierarchy

public interface Property<T> {

    String getName();

    T getValue();
}


@Entity
@Table(name="integer_property")
public class IntegerProperty implements Property<Integer> {

    @Id
    private Long id;

    @Column(name = "`name`")
    private String name;

    @Column(name = "`value`")
    private Integer value;

    @Override
    public String getName() {
        return name;
    }

    @Override
    public Integer getValue() {
        return value;
    }

    //Getters and setters omitted for brevity
}


@Entity
@Table(name="string_property")
public class StringProperty implements Property<String> {

    @Id
    private Long id;

    @Column(name = "`name`")
    private String name;

    @Column(name = "`value`")
    private String value;

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String getValue() {
        return value;
    }

    //Getters and setters omitted for brevity
}

A PropertyHolder can reference any such property, and, because each Property belongs to a separate table, the @Any annotation is, therefore, required.

Example 180. @Any mapping usage

@Entity
@Table( name = "property_holder" )
public class PropertyHolder {

    @Id
    private Long id;

    @Any(
        metaDef = "PropertyMetaDef",
        metaColumn = @Column( name = "property_type" )
    )
    @JoinColumn( name = "property_id" )
    private Property property;

    //Getters and setters are omitted for brevity

}
CREATE TABLE property_holder (
    id BIGINT NOT NULL,
    property_type VARCHAR(255),
    property_id BIGINT,
    PRIMARY KEY ( id )
)

As you can see, there are two columns used to reference a Property instance: property_id and property_type. The property_id is used to match the id column of either the string_property or integer_property tables, while the property_type is used to match the string_property or the integer_property table.

The table resolving mapping is defined by the metaDef attribute which references an @AnyMetaDef mapping.

The package-info.java contains the @AnyMetaDef mapping:

Example 181. @AnyMetaDef mapping usage

@AnyMetaDef( name= "PropertyMetaDef", metaType = "string", idType = "long",
    metaValues = {
            @MetaValue(value = "S", targetEntity = StringProperty.class),
            @MetaValue(value = "I", targetEntity = IntegerProperty.class)
        }
    )
package org.hibernate.userguide.associations.any;

import org.hibernate.annotations.AnyMetaDef;
import org.hibernate.annotations.MetaValue;

Although the @AnyMetaDef mapping could be set right next to the @Any annotation, it is good practice to configure it at the class or package level, especially if you need to reuse it for multiple @Any mappings.

To see the @Any annotation in action, consider the next examples.

If we persist an IntegerProperty as well as a StringProperty entity, and associate the StringProperty entity with a PropertyHolder, Hibernate will generate the following SQL queries:

Example 182. @Any mapping persist example

IntegerProperty ageProperty = new IntegerProperty();
ageProperty.setId( 1L );
ageProperty.setName( "age" );
ageProperty.setValue( 23 );

session.persist( ageProperty );

StringProperty nameProperty = new StringProperty();
nameProperty.setId( 1L );
nameProperty.setName( "name" );
nameProperty.setValue( "John Doe" );

session.persist( nameProperty );

PropertyHolder namePropertyHolder = new PropertyHolder();
namePropertyHolder.setId( 1L );
namePropertyHolder.setProperty( nameProperty );

session.persist( namePropertyHolder );
INSERT INTO integer_property
       ( "name", "value", id )
VALUES ( 'age', 23, 1 )

INSERT INTO string_property
       ( "name", "value", id )
VALUES ( 'name', 'John Doe', 1 )

INSERT INTO property_holder
       ( property_type, property_id, id )
VALUES ( 'S', 1, 1 )

When fetching the PropertyHolder entity and navigating its property association, Hibernate will fetch the associated StringProperty entity like this:

Example 183. @Any mapping query example

PropertyHolder propertyHolder = session.get( PropertyHolder.class, 1L );

assertEquals("name", propertyHolder.getProperty().getName());
assertEquals("John Doe", propertyHolder.getProperty().getValue());
SELECT ph.id AS id1_1_0_,
       ph.property_type AS property2_1_0_,
       ph.property_id AS property3_1_0_
FROM   property_holder ph
WHERE  ph.id = 1


SELECT sp.id AS id1_2_0_,
       sp."name" AS name2_2_0_,
       sp."value" AS value3_2_0_
FROM   string_property sp
WHERE  sp.id = 1
@ManyToAny mapping

While the @Any mapping is useful to emulate a @ManyToOne association when there can be multiple target entities, to emulate a @OneToMany association, the @ManyToAny annotation must be used.

In the following example, the PropertyRepository entity has a collection of Property entities.

The repository_properties link table holds the associations between PropertyRepository and Property entities.

Example 184. @ManyToAny mapping usage

@Entity
@Table( name = "property_repository" )
public class PropertyRepository {

    @Id
    private Long id;

    @ManyToAny(
        metaDef = "PropertyMetaDef",
        metaColumn = @Column( name = "property_type" )
    )
    @Cascade( { org.hibernate.annotations.CascadeType.ALL })
    @JoinTable(name = "repository_properties",
        joinColumns = @JoinColumn(name = "repository_id"),
        inverseJoinColumns = @JoinColumn(name = "property_id")
    )
    private List<Property<?>> properties = new ArrayList<>(  );

    //Getters and setters are omitted for brevity

}
CREATE TABLE property_repository (
    id BIGINT NOT NULL,
    PRIMARY KEY ( id )
)

CREATE TABLE repository_properties (
    repository_id BIGINT NOT NULL,
    property_type VARCHAR(255),
    property_id BIGINT NOT NULL
)

To see the @ManyToAny annotation in action, consider the next examples.

If we persist an IntegerProperty as well as a StringProperty entity, and associate both of them with a PropertyRepository parent entity, Hibernate will generate the following SQL queries:

Example 185. @ManyToAny mapping persist example

IntegerProperty ageProperty = new IntegerProperty();
ageProperty.setId( 1L );
ageProperty.setName( "age" );
ageProperty.setValue( 23 );

session.persist( ageProperty );

StringProperty nameProperty = new StringProperty();
nameProperty.setId( 1L );
nameProperty.setName( "name" );
nameProperty.setValue( "John Doe" );

session.persist( nameProperty );

PropertyRepository propertyRepository = new PropertyRepository();
propertyRepository.setId( 1L );

propertyRepository.getProperties().add( ageProperty );
propertyRepository.getProperties().add( nameProperty );

session.persist( propertyRepository );
INSERT INTO integer_property
       ( "name", "value", id )
VALUES ( 'age', 23, 1 )

INSERT INTO string_property
       ( "name", "value", id )
VALUES ( 'name', 'John Doe', 1 )

INSERT INTO property_repository ( id )
VALUES ( 1 )

INSERT INTO repository_properties
    ( repository_id , property_type , property_id )
VALUES
    ( 1 , 'I' , 1 )

When fetching the PropertyRepository entity and navigating its properties association, Hibernate will fetch the associated IntegerProperty and StringProperty entities like this:

Example 186. @ManyToAny mapping query example

PropertyRepository propertyRepository = session.get( PropertyRepository.class, 1L );

assertEquals(2, propertyRepository.getProperties().size());

for(Property property : propertyRepository.getProperties()) {
    assertNotNull( property.getValue() );
}
SELECT pr.id AS id1_1_0_
FROM   property_repository pr
WHERE  pr.id = 1

SELECT ip.id AS id1_0_0_ ,
       ip."name" AS name2_0_0_ ,
       ip."value" AS value3_0_0_
FROM   integer_property ip
WHERE  ip.id = 1

SELECT sp.id AS id1_3_0_ ,
       sp."name" AS name2_3_0_ ,
       sp."value" AS value3_3_0_
FROM   string_property sp
WHERE  sp.id = 1

2.7.7. @JoinFormula mapping

The @JoinFormula annotation is used to customize the join between a child Foreign Key and a parent row Primary Key.

Example 187. @JoinFormula mapping usage

@Entity(name = "User")
@Table(name = "users")
public static class User {

    @Id
    private Long id;

    private String firstName;

    private String lastName;

    private String phoneNumber;

    @ManyToOne
    @JoinFormula( "REGEXP_REPLACE(phoneNumber, '\\+(\\d+)-.*', '\\1')::int" )
    private Country country;

    //Getters and setters omitted for brevity

}

@Entity(name = "Country")
@Table(name = "countries")
public static class Country {

    @Id
    private Integer id;

    private String name;

    //Getters and setters, equals and hashCode methods omitted for brevity

}
CREATE TABLE countries (
    id int4 NOT NULL,
    name VARCHAR(255),
    PRIMARY KEY ( id )
)

CREATE TABLE users (
    id int8 NOT NULL,
    firstName VARCHAR(255),
    lastName VARCHAR(255),
    phoneNumber VARCHAR(255),
    PRIMARY KEY ( id )
)

The country association in the User entity is mapped by the country identifier provided by the phoneNumber property.

Considering we have the following entities:

Example 188. @JoinFormula mapping usage

Country US = new Country();
US.setId( 1 );
US.setName( "United States" );

Country Romania = new Country();
Romania.setId( 40 );
Romania.setName( "Romania" );

doInJPA( this::entityManagerFactory, entityManager -> {
    entityManager.persist( US );
    entityManager.persist( Romania );
} );

doInJPA( this::entityManagerFactory, entityManager -> {
    User user1 = new User( );
    user1.setId( 1L );
    user1.setFirstName( "John" );
    user1.setLastName( "Doe" );
    user1.setPhoneNumber( "+1-234-5678" );
    entityManager.persist( user1 );

    User user2 = new User( );
    user2.setId( 2L );
    user2.setFirstName( "Vlad" );
    user2.setLastName( "Mihalcea" );
    user2.setPhoneNumber( "+40-123-4567" );
    entityManager.persist( user2 );
} );

When fetching the User entities, the country property is mapped by the @JoinFormula expression:

Example 189. @JoinFormula mapping usage

doInJPA( this::entityManagerFactory, entityManager -> {
    log.info( "Fetch User entities" );

    User john = entityManager.find( User.class, 1L );
    assertEquals( US, john.getCountry());

    User vlad = entityManager.find( User.class, 2L );
    assertEquals( Romania, vlad.getCountry());
} );
-- Fetch User entities

SELECT
    u.id as id1_1_0_,
    u.firstName as firstNam2_1_0_,
    u.lastName as lastName3_1_0_,
    u.phoneNumber as phoneNum4_1_0_,
    REGEXP_REPLACE(u.phoneNumber, '\+(\d+)-.*', '\1')::int as formula1_0_,
    c.id as id1_0_1_,
    c.name as name2_0_1_
FROM
    users u
LEFT OUTER JOIN
    countries c
        ON REGEXP_REPLACE(u.phoneNumber, '\+(\d+)-.*', '\1')::int = c.id
WHERE
    u.id=?

-- binding parameter [1] as [BIGINT] - [1]

SELECT
    u.id as id1_1_0_,
    u.firstName as firstNam2_1_0_,
    u.lastName as lastName3_1_0_,
    u.phoneNumber as phoneNum4_1_0_,
    REGEXP_REPLACE(u.phoneNumber, '\+(\d+)-.*', '\1')::int as formula1_0_,
    c.id as id1_0_1_,
    c.name as name2_0_1_
FROM
    users u
LEFT OUTER JOIN
    countries c
        ON REGEXP_REPLACE(u.phoneNumber, '\+(\d+)-.*', '\1')::int = c.id
WHERE
    u.id=?

-- binding parameter [1] as [BIGINT] - [2]

Therefore, the @JoinFormula annotation is used to define a custom join association between the parent-child association.

2.7.8. @JoinColumnOrFormula mapping

The @JoinColumnOrFormula annotation is used to customize the join between a child Foreign Key and a parent row Primary Key when we need to take into consideration a column value as well as a @JoinFormula.

Example 190. @JoinColumnOrFormula mapping usage

@Entity(name = "User")
@Table(name = "users")
public static class User {

    @Id
    private Long id;

    private String firstName;

    private String lastName;

    private String language;

    @ManyToOne
    @JoinColumnOrFormula( column =
        @JoinColumn(
            name = "language",
            referencedColumnName = "primaryLanguage",
            insertable = false,
            updatable = false
        )
    )
    @JoinColumnOrFormula( formula =
        @JoinFormula(
            value = "true",
            referencedColumnName = "is_default"
        )
    )
    private Country country;

    //Getters and setters omitted for brevity

}

@Entity(name = "Country")
@Table(name = "countries")
public static class Country implements Serializable {

    @Id
    private Integer id;

    private String name;

    private String primaryLanguage;

    @Column(name = "is_default")
    private boolean _default;

    //Getters and setters, equals and hashCode methods omitted for brevity

}
CREATE TABLE countries (
    id INTEGER NOT NULL,
    is_default boolean,
    name VARCHAR(255),
    primaryLanguage VARCHAR(255),
    PRIMARY KEY ( id )
)

CREATE TABLE users (
    id BIGINT NOT NULL,
    firstName VARCHAR(255),
    language VARCHAR(255),
    lastName VARCHAR(255),
    PRIMARY KEY ( id )
)

The country association in the User entity is mapped by the language property value and the associated Country is_default column value.

Considering we have the following entities:

Example 191. @JoinColumnOrFormula persist example

Country US = new Country();
US.setId( 1 );
US.setDefault( true );
US.setPrimaryLanguage( "English" );
US.setName( "United States" );

Country Romania = new Country();
Romania.setId( 40 );
Romania.setDefault( true );
Romania.setName( "Romania" );
Romania.setPrimaryLanguage( "Romanian" );

doInJPA( this::entityManagerFactory, entityManager -> {
    entityManager.persist( US );
    entityManager.persist( Romania );
} );

doInJPA( this::entityManagerFactory, entityManager -> {
    User user1 = new User( );
    user1.setId( 1L );
    user1.setFirstName( "John" );
    user1.setLastName( "Doe" );
    user1.setLanguage( "English" );
    entityManager.persist( user1 );

    User user2 = new User( );
    user2.setId( 2L );
    user2.setFirstName( "Vlad" );
    user2.setLastName( "Mihalcea" );
    user2.setLanguage( "Romanian" );
    entityManager.persist( user2 );

} );

When fetching the User entities, the country property is mapped by the @JoinColumnOrFormula expression:

Example 192. @JoinColumnOrFormula fetching example

doInJPA( this::entityManagerFactory, entityManager -> {
    log.info( "Fetch User entities" );

    User john = entityManager.find( User.class, 1L );
    assertEquals( US, john.getCountry());

    User vlad = entityManager.find( User.class, 2L );
    assertEquals( Romania, vlad.getCountry());
} );
SELECT
    u.id as id1_1_0_,
    u.language as language3_1_0_,
    u.firstName as firstNam2_1_0_,
    u.lastName as lastName4_1_0_,
    1 as formula1_0_,
    c.id as id1_0_1_,
    c.is_default as is_defau2_0_1_,
    c.name as name3_0_1_,
    c.primaryLanguage as primaryL4_0_1_
FROM
    users u
LEFT OUTER JOIN
    countries c
        ON u.language = c.primaryLanguage
        AND 1 = c.is_default
WHERE
    u.id = ?

-- binding parameter [1] as [BIGINT] - [1]

SELECT
    u.id as id1_1_0_,
    u.language as language3_1_0_,
    u.firstName as firstNam2_1_0_,
    u.lastName as lastName4_1_0_,
    1 as formula1_0_,
    c.id as id1_0_1_,
    c.is_default as is_defau2_0_1_,
    c.name as name3_0_1_,
    c.primaryLanguage as primaryL4_0_1_
FROM
    users u
LEFT OUTER JOIN
    countries c
        ON u.language = c.primaryLanguage
        AND 1 = c.is_default
WHERE
    u.id = ?

-- binding parameter [1] as [BIGINT] - [2]

Therefore, the @JoinColumnOrFormula annotation is used to define a custom join association between the parent-child association.

2.8. Collections

Naturally Hibernate also allows persisting collections. These persistent collections can contain almost any other Hibernate type, including basic types, custom types, embeddables, and references to other entities. In this context, the distinction between value and reference semantics is very important. An object in a collection might be handled with value semantics (its lifecycle being fully dependant on the collection owner), or it might be a reference to another entity with its own lifecycle. In the latter case, only the link between the two objects is considered to be a state held by the collection.

The owner of the collection is always an entity, even if the collection is defined by an embeddable type. Collections form one/many-to-many associations between types so there can be:

  • value type collections

  • embeddable type collections

  • entity collections

Hibernate uses its own collection implementations which are enriched with lazy-loading, caching or state change detection semantics. For this reason, persistent collections must be declared as an interface type. The actual interface might be java.util.Collection, java.util.List, java.util.Set, java.util.Map, java.util.SortedSet, java.util.SortedMap or even other object types (meaning you will have to write an implementation of org.hibernate.usertype.UserCollectionType).

As the following example demonstrates, it’s important to use the interface type and not the collection implementation, as declared in the entity mapping.

Example 193. Hibernate uses its own collection implementations

@Entity(name = "Person")
public static class Person {

    @Id
    private Long id;

    @ElementCollection
    private List<String> phones = new ArrayList<>();

    //Getters and setters are omitted for brevity

}

Person person = entityManager.find( Person.class, 1L );
//Throws java.lang.ClassCastException: org.hibernate.collection.internal.PersistentBag cannot be cast to java.util.ArrayList
ArrayList<String> phones = (ArrayList<String>) person.getPhones();

It is important that collections be defined using the appropriate Java Collections Framework interface rather than a specific implementation.

From a theoretical perspective, this just follows good design principles. From a practical perspective, Hibernate (like other persistence providers) will use their own collection implementations which conform to the Java Collections Framework interfaces.

The persistent collections injected by Hibernate behave like ArrayList, HashSet, TreeSet, HashMap or TreeMap, depending on the interface type.

2.8.1. Collections as a value type

Value and embeddable type collections have a similar behavior to basic types since they are automatically persisted when referenced by a persistent object and automatically deleted when unreferenced. If a collection is passed from one persistent object to another, its elements might be moved from one table to another.

Two entities cannot share a reference to the same collection instance. Collection-valued properties do not support null value semantics because Hibernate does not distinguish between a null collection reference and an empty collection.

2.8.2. Collections of value types

Collections of value type include basic and embeddable types. Collections cannot be nested, and, when used in collections, embeddable types are not allowed to define other collections.

For collections of value types, JPA 2.0 defines the @ElementCollection annotation. The lifecycle of the value-type collection is entirely controlled by its owning entity.

Considering the previous example mapping, when clearing the phone collection, Hibernate deletes all the associated phones. When adding a new element to the value type collection, Hibernate issues a new insert statement.

Example 194. Value type collection lifecycle

person.getPhones().clear();
person.getPhones().add( "123-456-7890" );
person.getPhones().add( "456-000-1234" );
DELETE FROM Person_phones WHERE   Person_id = 1

INSERT INTO Person_phones ( Person_id, phones )
VALUES ( 1, '123-456-7890' )

INSERT INTO Person_phones  (Person_id, phones)
VALUES  ( 1, '456-000-1234' )

If removing all elements or adding new ones is rather straightforward, removing a certain entry actually requires reconstructing the whole collection from scratch.

Example 195. Removing collection elements

person.getPhones().remove( 0 );
DELETE FROM Person_phones WHERE Person_id = 1

INSERT INTO Person_phones ( Person_id, phones )
VALUES ( 1, '456-000-1234' )

Depending on the number of elements, this behavior might not be efficient, if many elements need to be deleted and reinserted back into the database table. A workaround is to use an @OrderColumn, which, although not as efficient as when using the actual link table primary key, might improve the efficiency of the remove operations.

Example 196. Removing collection elements using @OrderColumn

@ElementCollection
@OrderColumn(name = "order_id")
private List<String> phones = new ArrayList<>();

person.getPhones().remove( 0 );
DELETE FROM Person_phones
WHERE  Person_id = 1
       AND order_id = 1

UPDATE Person_phones
SET    phones = '456-000-1234'
WHERE  Person_id = 1
       AND order_id = 0

The @OrderColumn column works best when removing from the tail of the collection, as it only requires a single delete statement. Removing from the head or the middle of the collection requires deleting the extra elements and updating the remaining ones to preserve element order.

Embeddable type collections behave the same way as value type collections. Adding embeddables to the collection triggers the associated insert statements and removing elements from the collection will generate delete statements.

Example 197. Embeddable type collections

@Entity(name = "Person")
public static class Person {

    @Id
    private Long id;

    @ElementCollection
    private List<Phone> phones = new ArrayList<>();

    //Getters and setters are omitted for brevity

}

@Embeddable
public static class Phone {

    private String type;

    @Column(name = "`number`")
    private String number;

    //Getters and setters are omitted for brevity

}

person.getPhones().add( new Phone( "landline", "028-234-9876" ) );
person.getPhones().add( new Phone( "mobile", "072-122-9876" ) );
INSERT INTO Person_phones ( Person_id, number, type )
VALUES ( 1, '028-234-9876', 'landline' )

INSERT INTO Person_phones ( Person_id, number, type )
VALUES ( 1, '072-122-9876', 'mobile' )

2.8.3. Collections of entities

If value type collections can only form a one-to-many association between an owner entity and multiple basic or embeddable types, entity collections can represent both @OneToMany and @ManyToMany associations.

From a relational database perspective, associations are defined by the foreign key side (the child-side). With value type collections, only the entity can control the association (the parent-side), but for a collection of entities, both sides of the association are managed by the persistence context.

For this reason, entity collections can be devised into two main categories: unidirectional and bidirectional associations. Unidirectional associations are very similar to value type collections since only the parent side controls this relationship. Bidirectional associations are more tricky since, even if sides need to be in-sync at all times, only one side is responsible for managing the association. A bidirectional association has an owning side and an inverse (mappedBy) side.

Another way of categorizing entity collections is by the underlying collection type, and so we can have:

  • bags

  • indexed lists

  • sets

  • sorted sets

  • maps

  • sorted maps

  • arrays

In the following sections, we will go through all these collection types and discuss both unidirectional and bidirectional associations.

2.8.4. Bags

Bags are unordered lists, and we can have unidirectional bags or bidirectional ones.

Unidirectional bags

The unidirectional bag is mapped using a single @OneToMany annotation on the parent side of the association. Behind the scenes, Hibernate requires an association table to manage the parent-child relationship, as we can see in the following example:

Example 198. Unidirectional bag

@Entity(name = "Person")
public static class Person {

    @Id
    private Long id;

    @OneToMany(cascade = CascadeType.ALL)
    private List<Phone> phones = new ArrayList<>();

    //Getters and setters are omitted for brevity

}

@Entity(name = "Phone")
public static class Phone {

    @Id
    private Long id;

    private String type;

    @Column(name = "`number`")
    private String number;

    //Getters and setters are omitted for brevity

}
CREATE TABLE Person (
    id BIGINT NOT NULL ,
    PRIMARY KEY ( id )
)

CREATE TABLE Person_Phone (
    Person_id BIGINT NOT NULL ,
    phones_id BIGINT NOT NULL
)

CREATE TABLE Phone (
    id BIGINT NOT NULL ,
    number VARCHAR(255) ,
    type VARCHAR(255) ,
    PRIMARY KEY ( id )
)

ALTER TABLE Person_Phone
ADD CONSTRAINT UK_9uhc5itwc9h5gcng944pcaslf
UNIQUE (phones_id)

ALTER TABLE Person_Phone
ADD CONSTRAINT FKr38us2n8g5p9rj0b494sd3391
FOREIGN KEY (phones_id) REFERENCES Phone

ALTER TABLE Person_Phone
ADD CONSTRAINT FK2ex4e4p7w1cj310kg2woisjl2
FOREIGN KEY (Person_id) REFERENCES Person

Because both the parent and the child sides are entities, the persistence context manages each entity separately.

The cascading mechanism allows you to propagate an entity state transition from a parent entity to its children.

By marking the parent side with the CascadeType.ALL attribute, the unidirectional association lifecycle becomes very similar to that of a value type collection.

Example 199. Unidirectional bag lifecycle

Person person = new Person( 1L );
person.getPhones().add( new Phone( 1L, "landline", "028-234-9876" ) );
person.getPhones().add( new Phone( 2L, "mobile", "072-122-9876" ) );
entityManager.persist( person );
INSERT INTO Person ( id )
VALUES ( 1 )

INSERT INTO Phone ( number, type, id )
VALUES ( '028-234-9876', 'landline', 1 )

INSERT INTO Phone ( number, type, id )
VALUES ( '072-122-9876', 'mobile', 2 )

INSERT INTO Person_Phone ( Person_id, phones_id )
VALUES ( 1, 1 )

INSERT INTO Person_Phone ( Person_id, phones_id )
VALUES ( 1, 2 )

In the example above, once the parent entity is persisted, the child entities are going to be persisted as well.

Just like value type collections, unidirectional bags are not as efficient when it comes to modifying the collection structure (removing or reshuffling elements).

Because the parent-side cannot uniquely identify each individual child, Hibernate deletes all link table rows associated with the parent entity and re-adds the remaining ones that are found in the current collection state.

Bidirectional bags

The bidirectional bag is the most common type of entity collection. The @ManyToOne side is the owning side of the bidirectional bag association, while the @OneToMany is the inverse side, being marked with the mappedBy attribute.

Example 200. Bidirectional bag

@Entity(name = "Person")
public static class Person {

    @Id
    private Long id;

    @OneToMany(mappedBy = "person", cascade = CascadeType.ALL)
    private List<Phone> phones = new ArrayList<>();

    //Getters and setters are omitted for brevity

    public void addPhone(Phone phone) {
        phones.add( phone );
        phone.setPerson( this );
    }

    public void removePhone(Phone phone) {
        phones.remove( phone );
        phone.setPerson( null );
    }
}

@Entity(name = "Phone")
public static class Phone {

    @Id
    private Long id;

    private String type;

    @Column(name = "`number`", unique = true)
    @NaturalId
    private String number;

    @ManyToOne
    private Person person;

    //Getters and setters are omitted for brevity

    @Override
    public boolean equals(Object o) {
        if ( this == o ) {
            return true;
        }
        if ( o == null || getClass() != o.getClass() ) {
            return false;
        }
        Phone phone = (Phone) o;
        return Objects.equals( number, phone.number );
    }

    @Override
    public int hashCode() {
        return Objects.hash( number );
    }
}
CREATE TABLE Person (
    id BIGINT NOT NULL, PRIMARY KEY (id)
)

CREATE TABLE Phone (
    id BIGINT NOT NULL,
    number VARCHAR(255),
    type VARCHAR(255),
    person_id BIGINT,
    PRIMARY KEY (id)
)

ALTER TABLE Phone
ADD CONSTRAINT UK_l329ab0g4c1t78onljnxmbnp6
UNIQUE (number)

ALTER TABLE Phone
ADD CONSTRAINT FKmw13yfsjypiiq0i1osdkaeqpg
FOREIGN KEy (person_id) REFERENCES Person

Example 201. Bidirectional bag lifecycle

person.addPhone( new Phone( 1L, "landline", "028-234-9876" ) );
person.addPhone( new Phone( 2L, "mobile", "072-122-9876" ) );
entityManager.flush();
person.removePhone( person.getPhones().get( 0 ) );
INSERT INTO Phone (number, person_id, type, id)
VALUES ( '028-234-9876', 1, 'landline', 1 )

INSERT INTO Phone (number, person_id, type, id)
VALUES ( '072-122-9876', 1, 'mobile', 2 )

UPDATE Phone
SET person_id = NULL, type = 'landline' where id = 1

Example 202. Bidirectional bag with orphan removal

@OneToMany(mappedBy = "person", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Phone> phones = new ArrayList<>();
DELETE FROM Phone WHERE id = 1

When rerunning the previous example, the child will get removed because the parent-side propagates the removal upon dissociating the child entity reference.

2.8.5. Ordered Lists

Although they use the List interface on the Java side, bags don’t retain element order. To preserve the collection element order, there are two possibilities:

@OrderBy

the collection is ordered upon retrieval using a child entity property

@OrderColumn

the collection uses a dedicated order column in the collection link table

Unidirectional ordered lists

When using the @OrderBy annotation, the mapping looks as follows:

Example 203. Unidirectional @OrderBy list

@Entity(name = "Person")
public static class Person {

    @Id
    private Long id;

    @OneToMany(cascade = CascadeType.ALL)
    @OrderBy("number")
    private List<Phone> phones = new ArrayList<>();

    //Getters and setters are omitted for brevity

}

@Entity(name = "Phone")
public static class Phone {

    @Id
    private Long id;

    private String type;

    @Column(name = "`number`")
    private String number;

    //Getters and setters are omitted for brevity

}

The database mapping is the same as with the Unidirectional bags example, so it won’t be repeated. Upon fetching the collection, Hibernate generates the following select statement:

Example 204. Unidirectional @OrderBy list select statement

SELECT
   phones0_.Person_id AS Person_i1_1_0_,
   phones0_.phones_id AS phones_i2_1_0_,
   unidirecti1_.id AS id1_2_1_,
   unidirecti1_."number" AS number2_2_1_,
   unidirecti1_.type AS type3_2_1_
FROM
   Person_Phone phones0_
INNER JOIN
   Phone unidirecti1_ ON phones0_.phones_id=unidirecti1_.id
WHERE
   phones0_.Person_id = 1
ORDER BY
   unidirecti1_."number"

The child table column is used to order the list elements.

The @OrderBy annotation can take multiple entity properties, and each property can take an ordering direction too (e.g. @OrderBy(“name ASC, type DESC”)).

If no property is specified (e.g. @OrderBy), the primary key of the child entity table is used for ordering.

Another ordering option is to use the @OrderColumn annotation:

Example 205. Unidirectional @OrderColumn list

@OneToMany(cascade = CascadeType.ALL)
@OrderColumn(name = "order_id")
private List<Phone> phones = new ArrayList<>();
CREATE TABLE Person_Phone (
    Person_id BIGINT NOT NULL ,
    phones_id BIGINT NOT NULL ,
    order_id INTEGER NOT NULL ,
    PRIMARY KEY ( Person_id, order_id )
)

This time, the link table takes the order_id column and uses it to materialize the collection element order. When fetching the list, the following select query is executed:

Example 206. Unidirectional @OrderColumn list select statement

select
   phones0_.Person_id as Person_i1_1_0_,
   phones0_.phones_id as phones_i2_1_0_,
   phones0_.order_id as order_id3_0_,
   unidirecti1_.id as id1_2_1_,
   unidirecti1_.number as number2_2_1_,
   unidirecti1_.type as type3_2_1_
from
   Person_Phone phones0_
inner join
   Phone unidirecti1_
      on phones0_.phones_id=unidirecti1_.id
where
   phones0_.Person_id = 1

With the order_id column in place, Hibernate can order the list in-memory after it’s being fetched from the database.

Bidirectional ordered lists

The mapping is similar with the Bidirectional bags example, just that the parent side is going to be annotated with either @OrderBy or @OrderColumn.

Example 207. Bidirectional @OrderBy list

@OneToMany(mappedBy = "person", cascade = CascadeType.ALL)
@OrderBy("number")
private List<Phone> phones = new ArrayList<>();

Just like with the unidirectional @OrderBy list, the number column is used to order the statement on the SQL level.

When using the @OrderColumn annotation, the order_id column is going to be embedded in the child table:

Example 208. Bidirectional @OrderColumn list

@OneToMany(mappedBy = "person", cascade = CascadeType.ALL)
@OrderColumn(name = "order_id")
private List<Phone> phones = new ArrayList<>();
CREATE TABLE Phone (
    id BIGINT NOT NULL ,
    number VARCHAR(255) ,
    type VARCHAR(255) ,
    person_id BIGINT ,
    order_id INTEGER ,
    PRIMARY KEY ( id )
)

When fetching the collection, Hibernate will use the fetched ordered columns to sort the elements according to the @OrderColumn mapping.

Customizing ordered list ordinal

You can customize the ordinal of the underlying ordered list by using the @ListIndexBase annotation.

Example 209. @ListIndexBase mapping example

@OneToMany(mappedBy = "person", cascade = CascadeType.ALL)
@OrderColumn(name = "order_id")
@ListIndexBase(100)
private List<Phone> phones = new ArrayList<>();

When inserting two Phone records, Hibernate is going to start the List index from 100 this time.

Example 210. @ListIndexBase persist example

Person person = new Person( 1L );
entityManager.persist( person );
person.addPhone( new Phone( 1L, "landline", "028-234-9876" ) );
person.addPhone( new Phone( 2L, "mobile", "072-122-9876" ) );
INSERT INTO Phone("number", person_id, type, id)
VALUES ('028-234-9876', 1, 'landline', 1)

INSERT INTO Phone("number", person_id, type, id)
VALUES ('072-122-9876', 1, 'mobile', 2)

UPDATE Phone
SET order_id = 100
WHERE id = 1

UPDATE Phone
SET order_id = 101
WHERE id = 2
Customizing ORDER BY SQL clause

While the JPA @OrderBy annotation allows you to specify the entity attributes used for sorting when fetching the current annotated collection, the Hibernate specific @OrderBy annotation is used to specify a SQL clause instead.

In the following example, the @OrderBy annotation uses the CHAR_LENGTH SQL function to order the Article entities by the number of characters of the name attribute.

Example 211. @OrderBy mapping example

@Entity(name = "Person")
public static class Person {

    @Id
    private Long id;

    private String name;

    @OneToMany(
        mappedBy = "person",
        cascade = CascadeType.ALL
    )
    @org.hibernate.annotations.OrderBy(
        clause = "CHAR_LENGTH(name) DESC"
    )
    private List<Article> articles = new ArrayList<>();

    //Getters and setters are omitted for brevity
}

@Entity(name = "Article")
public static class Article {

    @Id
    @GeneratedValue
    private Long id;

    private String name;

    private String content;

    @ManyToOne(fetch = FetchType.LAZY)
    private Person person;

    //Getters and setters are omitted for brevity
}

When fetching the articles collection, Hibernate uses the ORDER BY SQL clause provided by the mapping:

Example 212. @OrderBy fetching example

Person person = entityManager.find( Person.class, 1L );
assertEquals(
    "High-Performance Hibernate",
    person.getArticles().get( 0 ).getName()
);
select
    a.person_id as person_i4_0_0_,
    a.id as id1_0_0_,
    a.content as content2_0_1_,
    a.name as name3_0_1_,
    a.person_id as person_i4_0_1_
from
    Article a
where
    a.person_id = ?
order by
    CHAR_LENGTH(a.name) desc

2.8.6. Sets

Sets are collections that don’t allow duplicate entries and Hibernate supports both the unordered Set and the natural-ordering SortedSet.

Unidirectional sets

The unidirectional set uses a link table to hold the parent-child associations and the entity mapping looks as follows:

Example 213. Unidirectional set

@Entity(name = "Person")
public static class Person {

    @Id
    private Long id;

    @OneToMany(cascade = CascadeType.ALL)
    private Set<Phone> phones = new HashSet<>();

    //Getters and setters are omitted for brevity
}

@Entity(name = "Phone")
public static class Phone {

    @Id
    private Long id;

    private String type;

    @NaturalId
    @Column(name = "`number`")
    private String number;

    //Getters and setters are omitted for brevity

    @Override
    public boolean equals(Object o) {
        if ( this == o ) {
            return true;
        }
        if ( o == null || getClass() != o.getClass() ) {
            return false;
        }
        Phone phone = (Phone) o;
        return Objects.equals( number, phone.number );
    }

    @Override
    public int hashCode() {
        return Objects.hash( number );
    }
}

The unidirectional set lifecycle is similar to that of the Unidirectional bags, so it can be omitted. The only difference is that Set doesn’t allow duplicates, but this constraint is enforced by the Java object contract rather than the database mapping.

When using Sets, it’s very important to supply proper equals/hashCode implementations for child entities.

In the absence of a custom equals/hashCode implementation logic, Hibernate will use the default Java reference-based object equality which might render unexpected results when mixing detached and managed object instances.

Bidirectional sets

Just like bidirectional bags, the bidirectional set doesn’t use a link table, and the child table has a foreign key referencing the parent table primary key. The lifecycle is just like with bidirectional bags except for the duplicates which are filtered out.

Example 214. Bidirectional set

@Entity(name = "Person")
public static class Person {

    @Id
    private Long id;

    @OneToMany(mappedBy = "person", cascade = CascadeType.ALL)
    private Set<Phone> phones = new HashSet<>();

    //Getters and setters are omitted for brevity

    public void addPhone(Phone phone) {
        phones.add( phone );
        phone.setPerson( this );
    }

    public void removePhone(Phone phone) {
        phones.remove( phone );
        phone.setPerson( null );
    }
}

@Entity(name = "Phone")
public static class Phone {

    @Id
    private Long id;

    private String type;

    @Column(name = "`number`", unique = true)
    @NaturalId
    private String number;

    @ManyToOne
    private Person person;

    //Getters and setters are omitted for brevity

    @Override
    public boolean equals(Object o) {
        if ( this == o ) {
            return true;
        }
        if ( o == null || getClass() != o.getClass() ) {
            return false;
        }
        Phone phone = (Phone) o;
        return Objects.equals( number, phone.number );
    }

    @Override
    public int hashCode() {
        return Objects.hash( number );
    }
}

2.8.7. Sorted sets

For sorted sets, the entity mapping must use the SortedSet interface instead. According to the SortedSet contract, all elements must implement the Comparable interface and therefore provide the sorting logic.

Unidirectional sorted sets

A SortedSet that relies on the natural sorting order given by the child element Comparable implementation logic must be annotated with the @SortNatural Hibernate annotation.

Example 215. Unidirectional natural sorted set

@Entity(name = "Person")
public static class Person {

    @Id
    private Long id;

    @OneToMany(cascade = CascadeType.ALL)
    @SortNatural
    private SortedSet<Phone> phones = new TreeSet<>();

    //Getters and setters are omitted for brevity

}

@Entity(name = "Phone")
public static class Phone implements Comparable<Phone> {

    @Id
    private Long id;

    private String type;

    @NaturalId
    @Column(name = "`number`")
    private String number;

    //Getters and setters are omitted for brevity

    @Override
    public int compareTo(Phone o) {
        return number.compareTo( o.getNumber() );
    }

    @Override
    public boolean equals(Object o) {
        if ( this == o ) {
            return true;
        }
        if ( o == null || getClass() != o.getClass() ) {
            return false;
        }
        Phone phone = (Phone) o;
        return Objects.equals( number, phone.number );
    }

    @Override
    public int hashCode() {
        return Objects.hash( number );
    }
}

The lifecycle and the database mapping are identical to the Unidirectional bags, so they are intentionally omitted.

To provide a custom sorting logic, Hibernate also provides a @SortComparator annotation:

Example 216. Unidirectional custom comparator sorted set

@Entity(name = "Person")
public static class Person {

    @Id
    private Long id;

    @OneToMany(cascade = CascadeType.ALL)
    @SortComparator(ReverseComparator.class)
    private SortedSet<Phone> phones = new TreeSet<>();

    //Getters and setters are omitted for brevity

}

public static class ReverseComparator implements Comparator<Phone> {

    @Override
    public int compare(Phone o1, Phone o2) {
        return o2.compareTo( o1 );
    }
}

@Entity(name = "Phone")
public static class Phone implements Comparable<Phone> {

    @Id
    private Long id;

    private String type;

    @NaturalId
    @Column(name = "`number`")
    private String number;

    //Getters and setters are omitted for brevity

    @Override
    public int compareTo(Phone o) {
        return number.compareTo( o.getNumber() );
    }

    @Override
    public boolean equals(Object o) {
        if ( this == o ) {
            return true;
        }
        if ( o == null || getClass() != o.getClass() ) {
            return false;
        }
        Phone phone = (Phone) o;
        return Objects.equals( number, phone.number );
    }

    @Override
    public int hashCode() {
        return Objects.hash( number );
    }
}
Bidirectional sorted sets

The @SortNatural and @SortComparator work the same for bidirectional sorted sets too:

Example 217. Bidirectional natural sorted set

@OneToMany(mappedBy = "person", cascade = CascadeType.ALL)
@SortNatural
private SortedSet<Phone> phones = new TreeSet<>();

@SortComparator(ReverseComparator.class)
private SortedSet<Phone> phones = new TreeSet<>();

2.8.8. Maps

A java.util.Map is a ternary association because it requires a parent entity, a map key, and a value. An entity can either be a map key or a map value, depending on the mapping. Hibernate allows using the following map keys:

MapKeyColumn

for value type maps, the map key is a column in the link table that defines the grouping logic

MapKey

the map key is either the primary key or another property of the entity stored as a map entry value

MapKeyEnumerated

the map key is an Enum of the target child entity

MapKeyTemporal

the map key is a Date or a Calendar of the target child entity

MapKeyJoinColumn

the map key is an entity mapped as an association in the child entity that’s stored as a map entry key

Value type maps

A map of value type must use the @ElementCollection annotation, just like value type lists, bags or sets.

Example 218. Value type map with an entity as a map key

public enum PhoneType {
    LAND_LINE,
    MOBILE
}

@Entity(name = "Person")
public static class Person {

    @Id
    private Long id;

    @Temporal(TemporalType.TIMESTAMP)
    @ElementCollection
    @CollectionTable(name = "phone_register")
    @Column(name = "since")
    private Map<Phone, Date> phoneRegister = new HashMap<>();

    //Getters and setters are omitted for brevity

}

@Embeddable
public static class Phone {

    private PhoneType type;

    @Column(name = "`number`")
    private String number;

    //Getters and setters are omitted for brevity

}
CREATE TABLE Person (
    id BIGINT NOT NULL ,
    PRIMARY KEY ( id )
)

CREATE TABLE phone_register (
    Person_id BIGINT NOT NULL ,
    since TIMESTAMP ,
    number VARCHAR(255) NOT NULL ,
    type INTEGER NOT NULL ,
    PRIMARY KEY ( Person_id, number, type )
)

ALTER TABLE phone_register
ADD CONSTRAINT FKrmcsa34hr68of2rq8qf526mlk
FOREIGN KEY (Person_id) REFERENCES Person

Adding entries to the map generates the following SQL statements:

Example 219. Adding value type map entries

person.getPhoneRegister().put(
    new Phone( PhoneType.LAND_LINE, "028-234-9876" ), new Date()
);
person.getPhoneRegister().put(
    new Phone( PhoneType.MOBILE, "072-122-9876" ), new Date()
);
INSERT INTO phone_register (Person_id, number, type, since)
VALUES (1, '072-122-9876', 1, '2015-12-15 17:16:45.311')

INSERT INTO phone_register (Person_id, number, type, since)
VALUES (1, '028-234-9876', 0, '2015-12-15 17:16:45.311')
Maps with a custom key type

Hibernate defines the @MapKeyType annotation which you can use to customize the Map key type.

Considering you have the following tables in your database:

create table person (
    id int8 not null,
    primary key (id)
)

create table call_register (
    person_id int8 not null,
    phone_number int4,
    call_timestamp_epoch int8 not null,
    primary key (person_id, call_timestamp_epoch)
)

alter table if exists call_register
    add constraint FKsn58spsregnjyn8xt61qkxsub
    foreign key (person_id)
    references person

The call_register records the call history for every person. The call_timestamp_epoch column stores the phone call timestamp as a Unix timestamp since the Unix epoch.

The @MapKeyColumn annotation is used to define the table column holding the key while the @Column mapping gives the value of the java.util.Map in question.

Since we want to map all the calls by their associated java.util.Date, not by their timestamp since epoch which is a number, the entity mapping looks as follows:

Example 220. @MapKeyType mapping example

@Entity
@Table(name = "person")
public static class Person {

    @Id
    private Long id;

    @ElementCollection
    @CollectionTable(
        name = "call_register",
        joinColumns = @JoinColumn(name = "person_id")
    )
    @MapKeyType(
        @Type(
            type = "org.hibernate.userguide.collections.type.TimestampEpochType"
        )
    )
    @MapKeyColumn( name = "call_timestamp_epoch" )
    @Column(name = "phone_number")
    private Map<Date, Integer> callRegister = new HashMap<>();

    //Getters and setters are omitted for brevity

}

The associated TimestampEpochType looks as follows:

public class TimestampEpochType
        extends AbstractSingleColumnStandardBasicType<Date>
        implements VersionType<Date>, LiteralType<Date> {

    public static final TimestampEpochType INSTANCE = new TimestampEpochType();

    public TimestampEpochType() {
        super(
            BigIntTypeDescriptor.INSTANCE,
            JdbcTimestampTypeDescriptor.INSTANCE
        );
    }

    @Override
    public String getName() {
        return "epoch";
    }

    @Override
    public Date next(
        Date current,
        SharedSessionContractImplementor session) {
        return seed( session );
    }

    @Override
    public Date seed(
        SharedSessionContractImplementor session) {
        return new Timestamp( System.currentTimeMillis() );
    }

    @Override
    public Comparator<Date> getComparator() {
        return getJavaTypeDescriptor().getComparator();
    }

    @Override
    public String objectToSQLString(
        Date value,
        Dialect dialect) throws Exception {
        final Timestamp ts = Timestamp.class.isInstance( value )
            ? ( Timestamp ) value
            : new Timestamp( value.getTime() );
        return StringType.INSTANCE.objectToSQLString(
            ts.toString(), dialect
        );
    }

    @Override
    public Date fromStringValue(
        String xml) throws HibernateException {
        return fromString( xml );
    }
}

The TimestampEpochType allows us to map a Unix timestamp since epoch to a java.util.Date. But, without the @MapKeyType Hibernate annotation, it would not be possible to customize the Map key type.

Maps having an interface type as the key

Considering you have the following PhoneNumber interface with an implementation given by the MobilePhone class type:

Example 221. PhoneNumber interface and the MobilePhone class type

public interface PhoneNumber {

    String get();
}

@Embeddable
public static class MobilePhone
        implements PhoneNumber {

    static PhoneNumber fromString(String phoneNumber) {
        String[] tokens = phoneNumber.split( "-" );
        if ( tokens.length != 3 ) {
            throw new IllegalArgumentException( "invalid phone number: " + phoneNumber );
        }
        int i = 0;
        return new MobilePhone(
            tokens[i++],
            tokens[i++],
            tokens[i]
        );
    }

    private MobilePhone() {
    }

    public MobilePhone(
            String countryCode,
            String operatorCode,
            String subscriberCode) {
        this.countryCode = countryCode;
        this.operatorCode = operatorCode;
        this.subscriberCode = subscriberCode;
    }

    @Column(name = "country_code")
    private String countryCode;

    @Column(name = "operator_code")
    private String operatorCode;

    @Column(name = "subscriber_code")
    private String subscriberCode;

    @Override
    public String get() {
        return String.format(
            "%s-%s-%s",
            countryCode,
            operatorCode,
            subscriberCode
        );
    }

    @Override
    public boolean equals(Object o) {
        if ( this == o ) {
            return true;
        }
        if ( o == null || getClass() != o.getClass() ) {
            return false;
        }
        MobilePhone that = (MobilePhone) o;
        return Objects.equals( countryCode, that.countryCode ) &&
                Objects.equals( operatorCode, that.operatorCode ) &&
                Objects.equals( subscriberCode, that.subscriberCode );
    }

    @Override
    public int hashCode() {
        return Objects.hash( countryCode, operatorCode, subscriberCode );
    }
}

If you want to use the PhoneNumber interface as a java.util.Map key, then you need to supply the @MapKeyClass annotation as well.

Example 222. @MapKeyClass mapping example

@Entity
@Table(name = "person")
public static class Person {

    @Id
    private Long id;

    @ElementCollection
    @CollectionTable(
        name = "call_register",
        joinColumns = @JoinColumn(name = "person_id")
    )
    @MapKeyColumn( name = "call_timestamp_epoch" )
    @MapKeyClass( MobilePhone.class )
    @Column(name = "call_register")
    private Map<PhoneNumber, Integer> callRegister = new HashMap<>();

    //Getters and setters are omitted for brevity
}
create table person (
    id bigint not null,
    primary key (id)
)

create table call_register (
    person_id bigint not null,
    call_register integer,
    country_code varchar(255) not null,
    operator_code varchar(255) not null,
    subscriber_code varchar(255) not null,
    primary key (person_id, country_code, operator_code, subscriber_code)
)

alter table call_register
    add constraint FKqyj2at6ik010jqckeaw23jtv2
    foreign key (person_id)
    references person

When inserting a Person with a callRegister containing 2 MobilePhone references, Hibernate generates the following SQL statements:

Example 223. @MapKeyClass persist example

Person person = new Person();
person.setId( 1L );
person.getCallRegister().put( new MobilePhone( "01", "234", "567" ), 101 );
person.getCallRegister().put( new MobilePhone( "01", "234", "789" ), 102 );

entityManager.persist( person );
insert into person (id) values (?)

-- binding parameter [1] as [BIGINT] - [1]

insert into call_register(
    person_id,
    country_code,
    operator_code,
    subscriber_code,
    call_register
)
values
    (?, ?, ?, ?, ?)

-- binding parameter [1] as [BIGINT]  - [1]
-- binding parameter [2] as [VARCHAR] - [01]
-- binding parameter [3] as [VARCHAR] - [234]
-- binding parameter [4] as [VARCHAR] - [789]
-- binding parameter [5] as [INTEGER] - [102]

insert into call_register(
    person_id,
    country_code,
    operator_code,
    subscriber_code,
    call_register
)
values
    (?, ?, ?, ?, ?)

-- binding parameter [1] as [BIGINT]  - [1]
-- binding parameter [2] as [VARCHAR] - [01]
-- binding parameter [3] as [VARCHAR] - [234]
-- binding parameter [4] as [VARCHAR] - [567]
-- binding parameter [5] as [INTEGER] - [101]

When fetching a Person and accessing the callRegister Map, Hibernate generates the following SQL statements:

Example 224. @MapKeyClass fetch example

Person person = entityManager.find( Person.class, 1L );
assertEquals( 2, person.getCallRegister().size() );

assertEquals(
    Integer.valueOf( 101 ),
    person.getCallRegister().get( MobilePhone.fromString( "01-234-567" ) )
);

assertEquals(
    Integer.valueOf( 102 ),
    person.getCallRegister().get( MobilePhone.fromString( "01-234-789" ) )
);
select
    cr.person_id as person_i1_0_0_,
    cr.call_register as call_reg2_0_0_,
    cr.country_code as country_3_0_,
    cr.operator_code as operator4_0_,
    cr.subscriber_code as subscrib5_0_
from
    call_register cr
where
    cr.person_id = ?

-- binding parameter [1] as [BIGINT] - [1]

-- extracted value ([person_i1_0_0_] : [BIGINT])  - [1]
-- extracted value ([call_reg2_0_0_] : [INTEGER]) - [101]
-- extracted value ([country_3_0_]   : [VARCHAR]) - [01]
-- extracted value ([operator4_0_]   : [VARCHAR]) - [234]
-- extracted value ([subscrib5_0_]   : [VARCHAR]) - [567]

-- extracted value ([person_i1_0_0_] : [BIGINT])  - [1]
-- extracted value ([call_reg2_0_0_] : [INTEGER]) - [102]
-- extracted value ([country_3_0_]   : [VARCHAR]) - [01]
-- extracted value ([operator4_0_]   : [VARCHAR]) - [234]
-- extracted value ([subscrib5_0_]   : [VARCHAR]) - [789]
Unidirectional maps

A unidirectional map exposes a parent-child association from the parent-side only.

The following example shows a unidirectional map which also uses a @MapKeyTemporal annotation. The map key is a timestamp, and it’s taken from the child entity table.

The @MapKey annotation is used to define the entity attribute used as a key of the java.util.Map in question.

Example 225. Unidirectional Map

public enum PhoneType {
    LAND_LINE,
    MOBILE
}

@Entity(name = "Person")
public static class Person {

    @Id
    private Long id;

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinTable(
        name = "phone_register",
        joinColumns = @JoinColumn(name = "phone_id"),
        inverseJoinColumns = @JoinColumn(name = "person_id"))
    @MapKey(name = "since")
    @MapKeyTemporal(TemporalType.TIMESTAMP)
    private Map<Date, Phone> phoneRegister = new HashMap<>();

    //Getters and setters are omitted for brevity

    public void addPhone(Phone phone) {
        phoneRegister.put( phone.getSince(), phone );
    }
}

@Entity(name = "Phone")
public static class Phone {

    @Id
    @GeneratedValue
    private Long id;

    private PhoneType type;

    @Column(name = "`number`")
    private String number;

    private Date since;

    //Getters and setters are omitted for brevity

}
CREATE TABLE Person (
    id BIGINT NOT NULL ,
    PRIMARY KEY ( id )
)

CREATE TABLE Phone (
    id BIGINT NOT NULL ,
    number VARCHAR(255) ,
    since TIMESTAMP ,
    type INTEGER ,
    PRIMARY KEY ( id )
)

CREATE TABLE phone_register (
    phone_id BIGINT NOT NULL ,
    person_id BIGINT NOT NULL ,
    PRIMARY KEY ( phone_id, person_id )
)

ALTER TABLE phone_register
ADD CONSTRAINT FKc3jajlx41lw6clbygbw8wm65w
FOREIGN KEY (person_id) REFERENCES Phone

ALTER TABLE phone_register
ADD CONSTRAINT FK6npoomh1rp660o1b55py9ndw4
FOREIGN KEY (phone_id) REFERENCES Person
Bidirectional maps

Like most bidirectional associations, this relationship is owned by the child-side while the parent is the inverse side and can propagate its own state transitions to the child entities.

In the following example, you can see that @MapKeyEnumerated was used so that the Phone enumeration becomes the map key.

Example 226. Bidirectional Map

@Entity(name = "Person")
public static class Person {

    @Id
    private Long id;

    @OneToMany(mappedBy = "person", cascade = CascadeType.ALL, orphanRemoval = true)
    @MapKey(name = "type")
    @MapKeyEnumerated
    private Map<PhoneType, Phone> phoneRegister = new HashMap<>();

    //Getters and setters are omitted for brevity

    public void addPhone(Phone phone) {
        phone.setPerson( this );
        phoneRegister.put( phone.getType(), phone );
    }
}

@Entity(name = "Phone")
public static class Phone {

    @Id
    @GeneratedValue
    private Long id;

    private PhoneType type;

    @Column(name = "`number`")
    private String number;

    private Date since;

    @ManyToOne
    private Person person;

    //Getters and setters are omitted for brevity

}
CREATE TABLE Person (
    id BIGINT NOT NULL ,
    PRIMARY KEY ( id )
)

CREATE TABLE Phone (
    id BIGINT NOT NULL ,
    number VARCHAR(255) ,
    since TIMESTAMP ,
    type INTEGER ,
    person_id BIGINT ,
    PRIMARY KEY ( id )
)

ALTER TABLE Phone
ADD CONSTRAINT FKmw13yfsjypiiq0i1osdkaeqpg
FOREIGN KEY (person_id) REFERENCES Person

2.8.9. Arrays

When discussing arrays, it is important to understand the distinction between SQL array types and Java arrays that are mapped as part of the application’s domain model.

Not all databases implement the SQL-99 ARRAY type and, for this reason, Hibernate doesn’t support native database array types.

Hibernate does support the mapping of arrays in the Java domain model - conceptually the same as mapping a List. However, it is important to realize that it is impossible for Hibernate to offer lazy-loading for arrays of entities and, for this reason, it is strongly recommended to map a “collection” of entities using a List rather than an array.

2.8.10. Arrays as binary

By default, Hibernate will choose a BINARY type, as supported by the current Dialect.

Example 227. Arrays stored as binary

@Entity(name = "Person")
public static class Person {

    @Id
    private Long id;

    private String[] phones;

    //Getters and setters are omitted for brevity

}
CREATE TABLE Person (
    id BIGINT NOT NULL ,
    phones VARBINARY(255) ,
    PRIMARY KEY ( id )
)

If you want to map arrays such as String[] or int[] to database-specific array types like PostgreSQL integer[] or text[], you need to write a custom Hibernate Type.

Check out this article for an example of how to write such a custom Hibernate Type.

2.8.11. Collections as basic value type

Notice how all the previous examples explicitly mark the collection attribute as either ElementCollection, OneToMany or ManyToMany. Collections not marked as such require a custom Hibernate Type and the collection elements must be stored in a single database column.

This is sometimes beneficial. Consider a use-case such as a VARCHAR column that represents a delimited list/set of Strings.

Example 228. Comma delimited collection

@Entity(name = "Person")
public static class Person {

    @Id
    private Long id;

    @Type(type = "comma_delimited_strings")
    private List<String> phones = new ArrayList<>();

    public List<String> getPhones() {
        return phones;
    }
}

public class CommaDelimitedStringsJavaTypeDescriptor extends AbstractTypeDescriptor<List> {

    public static final String DELIMITER = ",";

    public CommaDelimitedStringsJavaTypeDescriptor() {
        super(
            List.class,
            new MutableMutabilityPlan<List>() {
                @Override
                protected List deepCopyNotNull(List value) {
                    return new ArrayList( value );
                }
            }
        );
    }

    @Override
    public String toString(List value) {
        return ( (List<String>) value ).stream().collect( Collectors.joining( DELIMITER ) );
    }

    @Override
    public List fromString(String string) {
        List<String> values = new ArrayList<>();
        Collections.addAll( values, string.split( DELIMITER ) );
        return values;
    }

    @Override
    public <X> X unwrap(List value, Class<X> type, WrapperOptions options) {
        return (X) toString( value );
    }

    @Override
    public <X> List wrap(X value, WrapperOptions options) {
        return fromString( (String) value );
    }
}

public class CommaDelimitedStringsType extends AbstractSingleColumnStandardBasicType<List> {

    public CommaDelimitedStringsType() {
        super(
            VarcharTypeDescriptor.INSTANCE,
            new CommaDelimitedStringsJavaTypeDescriptor()
        );
    }

    @Override
    public String getName() {
        return "comma_delimited_strings";
    }
}

The developer can use the comma-delimited collection like any other collection we’ve discussed so far and Hibernate will take care of the type transformation part. The collection itself behaves like any other basic value type, as its lifecycle is bound to its owner entity.

Example 229. Comma delimited collection lifecycle

person.phones.add( "027-123-4567" );
person.phones.add( "028-234-9876" );
session.flush();
person.getPhones().remove( 0 );
INSERT INTO Person ( phones, id )
VALUES ( '027-123-4567,028-234-9876', 1 )

UPDATE Person
SET    phones = '028-234-9876'
WHERE  id = 1

See the Hibernate Integrations Guide for more details on developing custom value type mappings.

2.8.12. Custom collection types

If you wish to use other collection types than List, Set or Map, like Queue for instance, you have to use a custom collection type, as illustrated by the following example:

Example 230. Custom collection mapping example

@Entity(name = "Person")
public static class Person {

    @Id
    private Long id;

    @OneToMany(cascade = CascadeType.ALL)
    @CollectionType( type = "org.hibernate.userguide.collections.type.QueueType")
    private Collection<Phone> phones = new LinkedList<>();

    //Constructors are omitted for brevity

    public Queue<Phone> getPhones() {
        return (Queue<Phone>) phones;
    }
}

@Entity(name = "Phone")
public static class Phone implements Comparable<Phone> {

    @Id
    private Long id;

    private String type;

    @NaturalId
    @Column(name = "`number`")
    private String number;

    //Getters and setters are omitted for brevity

    @Override
    public int compareTo(Phone o) {
        return number.compareTo( o.getNumber() );
    }

    @Override
    public boolean equals(Object o) {
        if ( this == o ) {
            return true;
        }
        if ( o == null || getClass() != o.getClass() ) {
            return false;
        }
        Phone phone = (Phone) o;
        return Objects.equals( number, phone.number );
    }

    @Override
    public int hashCode() {
        return Objects.hash( number );
    }
}

public class QueueType implements UserCollectionType {

    @Override
    public PersistentCollection instantiate(
            SharedSessionContractImplementor session,
            CollectionPersister persister) throws HibernateException {
        return new PersistentQueue( session );
    }

    @Override
    public PersistentCollection wrap(
            SharedSessionContractImplementor session,
            Object collection) {
        return new PersistentQueue( session, (List) collection );
    }

    @Override
    public Iterator getElementsIterator(Object collection) {
        return ( (Queue) collection ).iterator();
    }

    @Override
    public boolean contains(Object collection, Object entity) {
        return ( (Queue) collection ).contains( entity );
    }

    @Override
    public Object indexOf(Object collection, Object entity) {
        int i = ( (List) collection ).indexOf( entity );
        return ( i < 0 ) ? null : i;
    }

    @Override
    public Object replaceElements(
            Object original,
            Object target,
            CollectionPersister persister,
            Object owner,
            Map copyCache,
            SharedSessionContractImplementor session)
            throws HibernateException {
        Queue result = (Queue) target;
        result.clear();
        result.addAll( (Queue) original );
        return result;
    }

    @Override
    public Object instantiate(int anticipatedSize) {
        return new LinkedList<>();
    }

}

public class PersistentQueue extends PersistentBag implements Queue {

    public PersistentQueue(SharedSessionContractImplementor session) {
        super( session );
    }

    public PersistentQueue(SharedSessionContractImplementor session, List list) {
        super( session, list );
    }

    @Override
    public boolean offer(Object o) {
        return add(o);
    }

    @Override
    public Object remove() {
        return poll();
    }

    @Override
    public Object poll() {
        int size = size();
        if(size > 0) {
            Object first = get(0);
            remove( 0 );
            return first;
        }
        throw new NoSuchElementException();
    }

    @Override
    public Object element() {
        return peek();
    }

    @Override
    public Object peek() {
        return size() > 0 ? get( 0 ) : null;
    }
}

The reason why the Queue interface is not used for the entity attribute is because Hibernate only allows the following types:

  • java.util.List

  • java.util.Set

  • java.util.Map

  • java.util.SortedSet

  • java.util.SortedMap

However, the custom collection type can still be customized as long as the base type is one of the aforementioned persistent types.

This way, the Phone collection can be used as a java.util.Queue:

Example 231. Custom collection example

Person person = entityManager.find( Person.class, 1L );
Queue<Phone> phones = person.getPhones();
Phone head = phones.peek();
assertSame(head, phones.poll());
assertEquals( 1, phones.size() );

2.9. Natural Ids

Natural ids represent domain model unique identifiers that have a meaning in the real world too. Even if a natural id does not make a good primary key (surrogate keys being usually preferred), it’s still useful to tell Hibernate about it. As we will see later, Hibernate provides a dedicated, efficient API for loading an entity by its natural id much like it offers for loading by its identifier (PK).

2.9.1. Natural Id Mapping

Natural ids are defined in terms of one or more persistent attributes.

Example 232. Natural id using single basic attribute

@Entity(name = "Book")
public static class Book {

    @Id
    private Long id;

    private String title;

    private String author;

    @NaturalId
    private String isbn;

    //Getters and setters are omitted for brevity
}

Example 233. Natural id using single embedded attribute

@Entity(name = "Book")
public static class Book {

    @Id
    private Long id;

    private String title;

    private String author;

    @NaturalId
    @Embedded
    private Isbn isbn;

    //Getters and setters are omitted for brevity
}

@Embeddable
public static class Isbn implements Serializable {

    private String isbn10;

    private String isbn13;

    //Getters and setters are omitted for brevity

    @Override
    public boolean equals(Object o) {
        if ( this == o ) {
            return true;
        }
        if ( o == null || getClass() != o.getClass() ) {
            return false;
        }
        Isbn isbn = (Isbn) o;
        return Objects.equals( isbn10, isbn.isbn10 ) &&
                Objects.equals( isbn13, isbn.isbn13 );
    }

    @Override
    public int hashCode() {
        return Objects.hash( isbn10, isbn13 );
    }
}

Example 234. Natural id using multiple persistent attributes

@Entity(name = "Book")
public static class Book {

    @Id
    private Long id;

    private String title;

    private String author;

    @NaturalId
    private String productNumber;

    @NaturalId
    @ManyToOne(fetch = FetchType.LAZY)
    private Publisher publisher;

    //Getters and setters are omitted for brevity
}

@Entity(name = "Publisher")
public static class Publisher implements Serializable {

    @Id
    private Long id;

    private String name;

    //Getters and setters are omitted for brevity

    @Override
    public boolean equals(Object o) {
        if ( this == o ) {
            return true;
        }
        if ( o == null || getClass() != o.getClass() ) {
            return false;
        }
        Publisher publisher = (Publisher) o;
        return Objects.equals( id, publisher.id ) &&
                Objects.equals( name, publisher.name );
    }

    @Override
    public int hashCode() {
        return Objects.hash( id, name );
    }
}

2.9.2. Natural Id API

As stated before, Hibernate provides an API for loading entities by their associated natural id. This is represented by the org.hibernate.NaturalIdLoadAccess contract obtained via Session#byNaturalId.

If the entity does not define a natural id, trying to load an entity by its natural id will throw an exception.

Example 235. Using NaturalIdLoadAccess

Book book = entityManager
    .unwrap(Session.class)
    .byNaturalId( Book.class )
    .using( "isbn", "978-9730228236" )
    .load();
Book book = entityManager
    .unwrap(Session.class)
    .byNaturalId( Book.class )
    .using(
        "isbn",
        new Isbn(
            "973022823X",
            "978-9730228236"
        ) )
    .load();
Book book = entityManager
    .unwrap(Session.class)
    .byNaturalId( Book.class )
    .using("productNumber", "973022823X")
    .using("publisher", publisher)
    .load();

NaturalIdLoadAccess offers 2 distinct methods for obtaining the entity:

load()

obtains a reference to the entity, making sure that the entity state is initialized.

getReference()

obtains a reference to the entity. The state may or may not be initialized. If the entity is already associated with the current running Session, that reference (loaded or not) is returned. If the entity is not loaded in the current Session and the entity supports proxy generation, an uninitialized proxy is generated and returned, otherwise the entity is loaded from the database and returned.

NaturalIdLoadAccess allows loading an entity by natural id and at the same time applies a pessimistic lock. For additional details on locking, see the Locking chapter.

We will discuss the last method available on NaturalIdLoadAccess ( setSynchronizationEnabled() ) in Natural Id - Mutability and Caching.

Because the Book entities in the first two examples define “simple” natural ids, we can load them as follows:

Example 236. Loading by simple natural id

Book book = entityManager
    .unwrap(Session.class)
    .bySimpleNaturalId( Book.class )
    .load( "978-9730228236" );
Book book = entityManager
    .unwrap(Session.class)
    .bySimpleNaturalId( Book.class )
    .load(
        new Isbn(
            "973022823X",
            "978-9730228236"
        )
    );

Here we see the use of the org.hibernate.SimpleNaturalIdLoadAccess contract, obtained via Session#bySimpleNaturalId().

SimpleNaturalIdLoadAccess is similar to NaturalIdLoadAccess except that it does not define the using method. Instead, because these simple natural ids are defined based on just one attribute we can directly pass the corresponding natural id attribute value directly to the load() and getReference() methods.

If the entity does not define a natural id, or if the natural id is not of a “simple” type, an exception will be thrown there.

2.9.3. Natural Id - Mutability and Caching

A natural id may be mutable or immutable. By default the @NaturalId annotation marks an immutable natural id attribute. An immutable natural id is expected to never change its value.

If the value(s) of the natural id attribute(s) change, @NaturalId(mutable = true) should be used instead.

Example 237. Mutable natural id mapping

@Entity(name = "Author")
public static class Author {

    @Id
    private Long id;

    private String name;

    @NaturalId(mutable = true)
    private String email;

    //Getters and setters are omitted for brevity
}

Within the Session, Hibernate maintains a mapping from natural id values to entity identifiers (PK) values. If natural ids values changed, it is possible for this mapping to become out of date until a flush occurs.

To work around this condition, Hibernate will attempt to discover any such pending changes and adjust them when the load() or getReference() methods are executed. To be clear: this is only pertinent for mutable natural ids.

This discovery and adjustment have a performance impact. If you are certain that none of the mutable natural ids already associated with the current Session have changed, you can disable this checking by calling setSynchronizationEnabled(false) (the default is true). This will force Hibernate to circumvent the checking of mutable natural ids.

Example 238. Mutable natural id synchronization use-case

Author author = entityManager
    .unwrap(Session.class)
    .bySimpleNaturalId( Author.class )
    .load( "john@acme.com" );

author.setEmail( "john.doe@acme.com" );

assertNull(
    entityManager
        .unwrap(Session.class)
        .bySimpleNaturalId( Author.class )
        .setSynchronizationEnabled( false )
        .load( "john.doe@acme.com" )
);

assertSame( author,
    entityManager
        .unwrap(Session.class)
        .bySimpleNaturalId( Author.class )
        .setSynchronizationEnabled( true )
        .load( "john.doe@acme.com" )
);

Not only can this NaturalId-to-PK resolution be cached in the Session, but we can also have it cached in the second-level cache if second level caching is enabled.

Example 239. Natural id caching

@Entity(name = "Book")
@NaturalIdCache
public static class Book {

    @Id
    private Long id;

    private String title;

    private String author;

    @NaturalId
    private String isbn;

    //Getters and setters are omitted for brevity
}

2.10. Dynamic Model

JPA only acknowledges the entity model mapping so, if you are concerned about JPA provider portability, it’s best to stick to the strict POJO model. On the other hand, Hibernate can work with both POJO entities and dynamic entity models.

2.10.1. Dynamic mapping models

Persistent entities do not necessarily have to be represented as POJO/JavaBean classes. Hibernate also supports dynamic models (using Map of Maps at runtime). With this approach, you do not write persistent classes, only mapping files.

A given entity has just one entity mode within a given SessionFactory. This is a change from previous versions which allowed to define multiple entity modes for an entity and to select which to load. Entity modes can now be mixed within a domain model; a dynamic entity might reference a POJO entity and vice versa.

Example 240. Dynamic domain model Hibernate mapping

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class entity-name="Book">
        <id name="isbn" column="isbn" length="32" type="string"/>

        <property name="title" not-null="true" length="50" type="string"/>

        <property name="author" not-null="true" length="50" type="string"/>

    </class>
</hibernate-mapping>

After you defined your entity mapping, you need to instruct Hibernate to use the dynamic mapping mode:

Example 241. Dynamic domain model Hibernate mapping

settings.put( "hibernate.default_entity_mode", "dynamic-map" );

When you are going to save the following Book dynamic entity, Hibernate is going to generate the following SQL statement:

Example 242. Persist dynamic entity

Map<String, String> book = new HashMap<>();
book.put( "isbn", "978-9730228236" );
book.put( "title", "High-Performance Java Persistence" );
book.put( "author", "Vlad Mihalcea" );

entityManager
    .unwrap(Session.class)
    .save( "Book", book );
insert
into
    Book
    (title, author, isbn)
values
    (?, ?, ?)

-- binding parameter [1] as [VARCHAR] - [High-Performance Java Persistence]
-- binding parameter [2] as [VARCHAR] - [Vlad Mihalcea]
-- binding parameter [3] as [VARCHAR] - [978-9730228236]

The main advantage of dynamic models is the quick turnaround time for prototyping without the need for entity class implementation. The main downfall is that you lose compile-time type checking and will likely deal with many exceptions at runtime. However, as a result of the Hibernate mapping, the database schema can easily be normalized and sound, allowing to add a proper domain model implementation on top later on.

It is also interesting to note that dynamic models are great for certain integration use cases as well. Envers, for example, makes extensive use of dynamic models to represent the historical data.

2.11. Inheritance

Although relational database systems don’t provide support for inheritance, Hibernate provides several strategies to leverage this object-oriented trait onto domain model entities:

MappedSuperclass

Inheritance is implemented in the domain model only without reflecting it in the database schema. See MappedSuperclass.

Single table

The domain model class hierarchy is materialized into a single table which contains entities belonging to different class types. See Single table.

Joined table

The base class and all the subclasses have their own database tables and fetching a subclass entity requires a join with the parent table as well. See Joined table.

Table per class

Each subclass has its own table containing both the subclass and the base class properties. See Table per class.

2.11.1. MappedSuperclass

In the following domain model class hierarchy, a DebitAccount and a CreditAccount share the same Account base class.

Inheritance class diagram

When using MappedSuperclass, the inheritance is visible in the domain model only, and each database table contains both the base class and the subclass properties.

Example 243. @MappedSuperclass inheritance

@MappedSuperclass
public static class Account {

    @Id
    private Long id;

    private String owner;

    private BigDecimal balance;

    private BigDecimal interestRate;

    //Getters and setters are omitted for brevity

}

@Entity(name = "DebitAccount")
public static class DebitAccount extends Account {

    private BigDecimal overdraftFee;

    //Getters and setters are omitted for brevity

}

@Entity(name = "CreditAccount")
public static class CreditAccount extends Account {

    private BigDecimal creditLimit;

    //Getters and setters are omitted for brevity

}
CREATE TABLE DebitAccount (
    id BIGINT NOT NULL ,
    balance NUMERIC(19, 2) ,
    interestRate NUMERIC(19, 2) ,
    owner VARCHAR(255) ,
    overdraftFee NUMERIC(19, 2) ,
    PRIMARY KEY ( id )
)

CREATE TABLE CreditAccount (
    id BIGINT NOT NULL ,
    balance NUMERIC(19, 2) ,
    interestRate NUMERIC(19, 2) ,
    owner VARCHAR(255) ,
    creditLimit NUMERIC(19, 2) ,
    PRIMARY KEY ( id )
)

Because the @MappedSuperclass inheritance model is not mirrored at the database level, it’s not possible to use polymorphic queries referencing the @MappedSuperclass when fetching persistent objects by their base class.

2.11.2. Single table

The single table inheritance strategy maps all subclasses to only one database table. Each subclass declares its own persistent properties. Version and id properties are assumed to be inherited from the root class.

When omitting an explicit inheritance strategy (e.g. @Inheritance), JPA will choose the SINGLE_TABLE strategy by default.

Example 244. Single Table inheritance

@Entity(name = "Account")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public static class Account {

    @Id
    private Long id;

    private String owner;

    private BigDecimal balance;

    private BigDecimal interestRate;

    //Getters and setters are omitted for brevity

}

@Entity(name = "DebitAccount")
public static class DebitAccount extends Account {

    private BigDecimal overdraftFee;

    //Getters and setters are omitted for brevity

}

@Entity(name = "CreditAccount")
public static class CreditAccount extends Account {

    private BigDecimal creditLimit;

    //Getters and setters are omitted for brevity

}
CREATE TABLE Account (
    DTYPE VARCHAR(31) NOT NULL ,
    id BIGINT NOT NULL ,
    balance NUMERIC(19, 2) ,
    interestRate NUMERIC(19, 2) ,
    owner VARCHAR(255) ,
    overdraftFee NUMERIC(19, 2) ,
    creditLimit NUMERIC(19, 2) ,
    PRIMARY KEY ( id )
)

Each subclass in a hierarchy must define a unique discriminator value, which is used to differentiate between rows belonging to separate subclass types. If this is not specified, the DTYPE column is used as a discriminator, storing the associated subclass name.

Example 245. Single Table inheritance discriminator column

DebitAccount debitAccount = new DebitAccount();
debitAccount.setId( 1L );
debitAccount.setOwner( "John Doe" );
debitAccount.setBalance( BigDecimal.valueOf( 100 ) );
debitAccount.setInterestRate( BigDecimal.valueOf( 1.5d ) );
debitAccount.setOverdraftFee( BigDecimal.valueOf( 25 ) );

CreditAccount creditAccount = new CreditAccount();
creditAccount.setId( 2L );
creditAccount.setOwner( "John Doe" );
creditAccount.setBalance( BigDecimal.valueOf( 1000 ) );
creditAccount.setInterestRate( BigDecimal.valueOf( 1.9d ) );
creditAccount.setCreditLimit( BigDecimal.valueOf( 5000 ) );

entityManager.persist( debitAccount );
entityManager.persist( creditAccount );
INSERT INTO Account (balance, interestRate, owner, overdraftFee, DTYPE, id)
VALUES (100, 1.5, 'John Doe', 25, 'DebitAccount', 1)

INSERT INTO Account (balance, interestRate, owner, creditLimit, DTYPE, id)
VALUES (1000, 1.9, 'John Doe', 5000, 'CreditAccount', 2)

When using polymorphic queries, only a single table is required to be scanned to fetch all associated subclass instances.

Example 246. Single Table polymorphic query

List<Account> accounts = entityManager
    .createQuery( "select a from Account a" )
    .getResultList();
SELECT  singletabl0_.id AS id2_0_ ,
        singletabl0_.balance AS balance3_0_ ,
        singletabl0_.interestRate AS interest4_0_ ,
        singletabl0_.owner AS owner5_0_ ,
        singletabl0_.overdraftFee AS overdraf6_0_ ,
        singletabl0_.creditLimit AS creditLi7_0_ ,
        singletabl0_.DTYPE AS DTYPE1_0_
FROM    Account singletabl0_

Among all other inheritance alternatives, the single table strategy performs the best since it requires access to one table only. Because all subclass columns are stored in a single table, it’s not possible to use NOT NULL constraints anymore, so integrity checks must be moved either into the data access layer or enforced through CHECK or TRIGGER constraints.

Discriminator

The discriminator column contains marker values that tell the persistence layer what subclass to instantiate for a particular row. Hibernate Core supports the following restricted set of types as discriminator column: String, char, int, byte, short, boolean(including yes_no, true_false).

Use the @DiscriminatorColumn to define the discriminator column as well as the discriminator type.

The enum DiscriminatorType used in javax.persistence.DiscriminatorColumn only contains the values STRING, CHAR and INTEGER which means that not all Hibernate supported types are available via the @DiscriminatorColumn annotation. You can also use @DiscriminatorFormula to express in SQL a virtual discriminator column. This is particularly useful when the discriminator value can be extracted from one or more columns of the table. Both @DiscriminatorColumn and @DiscriminatorFormula are to be set on the root entity (once per persisted hierarchy).

@org.hibernate.annotations.DiscriminatorOptions allows to optionally specify Hibernate-specific discriminator options which are not standardized in JPA. The available options are force and insert.

The force attribute is useful if the table contains rows with extra discriminator values that are not mapped to a persistent class. This could, for example, occur when working with a legacy database. If force is set to true, Hibernate will specify the allowed discriminator values in the SELECT query even when retrieving all instances of the root class.

The second option, insert, tells Hibernate whether or not to include the discriminator column in SQL INSERTs. Usually, the column should be part of the INSERT statement, but if your discriminator column is also part of a mapped composite identifier you have to set this option to false.

There used to be a @org.hibernate.annotations.ForceDiscriminator annotation which was deprecated in version 3.6 and later removed. Use @DiscriminatorOptions instead.

Discriminator formula

Assuming a legacy database schema where the discriminator is based on inspecting a certain column, we can take advantage of the Hibernate specific @DiscriminatorFormula annotation and map the inheritance model as follows:

Example 247. Single Table discriminator formula

@Entity(name = "Account")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorFormula(
    "case when debitKey is not null " +
    "then 'Debit' " +
    "else ( " +
    "   case when creditKey is not null " +
    "   then 'Credit' " +
    "   else 'Unknown' " +
    "   end ) " +
    "end "
)
public static class Account {

    @Id
    private Long id;

    private String owner;

    private BigDecimal balance;

    private BigDecimal interestRate;

    //Getters and setters are omitted for brevity

}

@Entity(name = "DebitAccount")
@DiscriminatorValue(value = "Debit")
public static class DebitAccount extends Account {

    private String debitKey;

    private BigDecimal overdraftFee;

    //Getters and setters are omitted for brevity

}

@Entity(name = "CreditAccount")
@DiscriminatorValue(value = "Credit")
public static class CreditAccount extends Account {

    private String creditKey;

    private BigDecimal creditLimit;

    //Getters and setters are omitted for brevity

}
CREATE TABLE Account (
    id int8 NOT NULL ,
    balance NUMERIC(19, 2) ,
    interestRate NUMERIC(19, 2) ,
    owner VARCHAR(255) ,
    debitKey VARCHAR(255) ,
    overdraftFee NUMERIC(19, 2) ,
    creditKey VARCHAR(255) ,
    creditLimit NUMERIC(19, 2) ,
    PRIMARY KEY ( id )
)

The @DiscriminatorFormula defines a custom SQL clause that can be used to identify a certain subclass type. The @DiscriminatorValue defines the mapping between the result of the @DiscriminatorFormula and the inheritance subclass type.

Implicit discriminator values

Aside from the usual discriminator values assigned to each individual subclass type, the @DiscriminatorValue can take two additional values:

null

If the underlying discriminator column is null, the null discriminator mapping is going to be used.

not null

If the underlying discriminator column has a not-null value that is not explicitly mapped to any entity, the not-null discriminator mapping used.

To understand how these two values work, consider the following entity mapping:

Example 248. @DiscriminatorValue null and not-null entity mapping

@Entity(name = "Account")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorValue( "null" )
public static class Account {

    @Id
    private Long id;

    private String owner;

    private BigDecimal balance;

    private BigDecimal interestRate;

    //Getters and setters are omitted for brevity

}

@Entity(name = "DebitAccount")
@DiscriminatorValue( "Debit" )
public static class DebitAccount extends Account {

    private BigDecimal overdraftFee;

    //Getters and setters are omitted for brevity

}

@Entity(name = "CreditAccount")
@DiscriminatorValue( "Credit" )
public static class CreditAccount extends Account {

    private BigDecimal creditLimit;

    //Getters and setters are omitted for brevity

}

@Entity(name = "OtherAccount")
@DiscriminatorValue( "not null" )
public static class OtherAccount extends Account {

    private boolean active;

    //Getters and setters are omitted for brevity

}

The Account class has a @DiscriminatorValue( "null" ) mapping, meaning that any account row which does not contain any discriminator value will be mapped to an Account base class entity. The DebitAccount and CreditAccount entities use explicit discriminator values. The OtherAccount entity is used as a generic account type because it maps any database row whose discriminator column is not explicitly assigned to any other entity in the current inheritance tree.

To visualize how it works, consider the following example:

Example 249. @DiscriminatorValue null and not-null entity persistence

DebitAccount debitAccount = new DebitAccount();
debitAccount.setId( 1L );
debitAccount.setOwner( "John Doe" );
debitAccount.setBalance( BigDecimal.valueOf( 100 ) );
debitAccount.setInterestRate( BigDecimal.valueOf( 1.5d ) );
debitAccount.setOverdraftFee( BigDecimal.valueOf( 25 ) );

CreditAccount creditAccount = new CreditAccount();
creditAccount.setId( 2L );
creditAccount.setOwner( "John Doe" );
creditAccount.setBalance( BigDecimal.valueOf( 1000 ) );
creditAccount.setInterestRate( BigDecimal.valueOf( 1.9d ) );
creditAccount.setCreditLimit( BigDecimal.valueOf( 5000 ) );

Account account = new Account();
account.setId( 3L );
account.setOwner( "John Doe" );
account.setBalance( BigDecimal.valueOf( 1000 ) );
account.setInterestRate( BigDecimal.valueOf( 1.9d ) );

entityManager.persist( debitAccount );
entityManager.persist( creditAccount );
entityManager.persist( account );

entityManager.unwrap( Session.class ).doWork( connection -> {
    try(Statement statement = connection.createStatement()) {
        statement.executeUpdate(
            "insert into Account (DTYPE, active, balance, interestRate, owner, id) " +
            "values ('Other', true, 25, 0.5, 'Vlad', 4)"
        );
    }
} );

Map<Long, Account> accounts = entityManager.createQuery(
    "select a from Account a", Account.class )
.getResultList()
.stream()
.collect( Collectors.toMap( Account::getId, Function.identity()));

assertEquals(4, accounts.size());
assertEquals( DebitAccount.class, accounts.get( 1L ).getClass() );
assertEquals( CreditAccount.class, accounts.get( 2L ).getClass() );
assertEquals( Account.class, accounts.get( 3L ).getClass() );
assertEquals( OtherAccount.class, accounts.get( 4L ).getClass() );
INSERT INTO Account (balance, interestRate, owner, overdraftFee, DTYPE, id)
VALUES (100, 1.5, 'John Doe', 25, 'Debit', 1)

INSERT INTO Account (balance, interestRate, owner, overdraftFee, DTYPE, id)
VALUES (1000, 1.9, 'John Doe', 5000, 'Credit', 2)

INSERT INTO Account (balance, interestRate, owner, id)
VALUES (1000, 1.9, 'John Doe', 3)

INSERT INTO Account (DTYPE, active, balance, interestRate, owner, id)
VALUES ('Other', true, 25, 0.5, 'Vlad', 4)

SELECT a.id as id2_0_,
       a.balance as balance3_0_,
       a.interestRate as interest4_0_,
       a.owner as owner5_0_,
       a.overdraftFee as overdraf6_0_,
       a.creditLimit as creditLi7_0_,
       a.active as active8_0_,
       a.DTYPE as DTYPE1_0_
FROM   Account a

As you can see, the Account entity row has a value of NULL in the DTYPE discriminator column, while the OtherAccount entity was saved with a DTYPE column value of other which has not explicit mapping.

2.11.3. Joined table

Each subclass can also be mapped to its own table. This is also called table-per-subclass mapping strategy. An inherited state is retrieved by joining with the table of the superclass.

A discriminator column is not required for this mapping strategy. Each subclass must, however, declare a table column holding the object identifier.

Example 250. Join Table

@Entity(name = "Account")
@Inheritance(strategy = InheritanceType.JOINED)
public static class Account {

    @Id
    private Long id;

    private String owner;

    private BigDecimal balance;

    private BigDecimal interestRate;

    //Getters and setters are omitted for brevity

}

@Entity(name = "DebitAccount")
public static class DebitAccount extends Account {

    private BigDecimal overdraftFee;

    //Getters and setters are omitted for brevity

}

@Entity(name = "CreditAccount")
public static class CreditAccount extends Account {

    private BigDecimal creditLimit;

    //Getters and setters are omitted for brevity

}
CREATE TABLE Account (
    id BIGINT NOT NULL ,
    balance NUMERIC(19, 2) ,
    interestRate NUMERIC(19, 2) ,
    owner VARCHAR(255) ,
    PRIMARY KEY ( id )
)

CREATE TABLE CreditAccount (
    creditLimit NUMERIC(19, 2) ,
    id BIGINT NOT NULL ,
    PRIMARY KEY ( id )
)

CREATE TABLE DebitAccount (
    overdraftFee NUMERIC(19, 2) ,
    id BIGINT NOT NULL ,
    PRIMARY KEY ( id )
)

ALTER TABLE CreditAccount
ADD CONSTRAINT FKihw8h3j1k0w31cnyu7jcl7n7n
FOREIGN KEY (id) REFERENCES Account

ALTER TABLE DebitAccount
ADD CONSTRAINT FKia914478noepymc468kiaivqm
FOREIGN KEY (id) REFERENCES Account

The primary keys of the CreditAccount and DebitAccount tables are also foreign keys to the superclass table primary key and described by the @PrimaryKeyJoinColumns.

The table name still defaults to the non-qualified class name. Also, if @PrimaryKeyJoinColumn is not set, the primary key / foreign key columns are assumed to have the same names as the primary key columns of the primary table of the superclass.

Example 251. Join Table with @PrimaryKeyJoinColumn

@Entity(name = "Account")
@Inheritance(strategy = InheritanceType.JOINED)
public static class Account {

    @Id
    private Long id;

    private String owner;

    private BigDecimal balance;

    private BigDecimal interestRate;

    //Getters and setters are omitted for brevity

}

@Entity(name = "DebitAccount")
@PrimaryKeyJoinColumn(name = "account_id")
public static class DebitAccount extends Account {

    private BigDecimal overdraftFee;

    //Getters and setters are omitted for brevity

}

@Entity(name = "CreditAccount")
@PrimaryKeyJoinColumn(name = "account_id")
public static class CreditAccount extends Account {

    private BigDecimal creditLimit;

    //Getters and setters are omitted for brevity

}
CREATE TABLE CreditAccount (
    creditLimit NUMERIC(19, 2) ,
    account_id BIGINT NOT NULL ,
    PRIMARY KEY ( account_id )
)

CREATE TABLE DebitAccount (
    overdraftFee NUMERIC(19, 2) ,
    account_id BIGINT NOT NULL ,
    PRIMARY KEY ( account_id )
)

ALTER TABLE CreditAccount
ADD CONSTRAINT FK8ulmk1wgs5x7igo370jt0q005
FOREIGN KEY (account_id) REFERENCES Account

ALTER TABLE DebitAccount
ADD CONSTRAINT FK7wjufa570onoidv4omkkru06j
FOREIGN KEY (account_id) REFERENCES Account

When using polymorphic queries, the base class table must be joined with all subclass tables to fetch every associated subclass instance.

Example 252. Join Table polymorphic query

List<Account> accounts = entityManager
    .createQuery( "select a from Account a" )
    .getResultList();
SELECT jointablet0_.id AS id1_0_ ,
       jointablet0_.balance AS balance2_0_ ,
       jointablet0_.interestRate AS interest3_0_ ,
       jointablet0_.owner AS owner4_0_ ,
       jointablet0_1_.overdraftFee AS overdraf1_2_ ,
       jointablet0_2_.creditLimit AS creditLi1_1_ ,
       CASE WHEN jointablet0_1_.id IS NOT NULL THEN 1
            WHEN jointablet0_2_.id IS NOT NULL THEN 2
            WHEN jointablet0_.id IS NOT NULL THEN 0
       END AS clazz_
FROM   Account jointablet0_
       LEFT OUTER JOIN DebitAccount jointablet0_1_ ON jointablet0_.id = jointablet0_1_.id
       LEFT OUTER JOIN CreditAccount jointablet0_2_ ON jointablet0_.id = jointablet0_2_.id

The joined table inheritance polymorphic queries can use several JOINS which might affect performance when fetching a large number of entities.

2.11.4. Table per class

A third option is to map only the concrete classes of an inheritance hierarchy to tables. This is called the table-per-concrete-class strategy. Each table defines all persistent states of the class, including the inherited state.

In Hibernate, it is not necessary to explicitly map such inheritance hierarchies. You can map each class as a separate entity root. However, if you wish to use polymorphic associations (e.g. an association to the superclass of your hierarchy), you need to use the union subclass mapping.

Example 253. Table per class

@Entity(name = "Account")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public static class Account {

    @Id
    private Long id;

    private String owner;

    private BigDecimal balance;

    private BigDecimal interestRate;

    //Getters and setters are omitted for brevity

}

@Entity(name = "DebitAccount")
public static class DebitAccount extends Account {

    private BigDecimal overdraftFee;

    //Getters and setters are omitted for brevity

}

@Entity(name = "CreditAccount")
public static class CreditAccount extends Account {

    private BigDecimal creditLimit;

    //Getters and setters are omitted for brevity

}
CREATE TABLE Account (
    id BIGINT NOT NULL ,
    balance NUMERIC(19, 2) ,
    interestRate NUMERIC(19, 2) ,
    owner VARCHAR(255) ,
    PRIMARY KEY ( id )
)

CREATE TABLE CreditAccount (
    id BIGINT NOT NULL ,
    balance NUMERIC(19, 2) ,
    interestRate NUMERIC(19, 2) ,
    owner VARCHAR(255) ,
    creditLimit NUMERIC(19, 2) ,
    PRIMARY KEY ( id )
)

CREATE TABLE DebitAccount (
    id BIGINT NOT NULL ,
    balance NUMERIC(19, 2) ,
    interestRate NUMERIC(19, 2) ,
    owner VARCHAR(255) ,
    overdraftFee NUMERIC(19, 2) ,
    PRIMARY KEY ( id )
)

When using polymorphic queries, a UNION is required to fetch the base class table along with all subclass tables as well.

Example 254. Table per class polymorphic query

List<Account> accounts = entityManager
    .createQuery( "select a from Account a" )
    .getResultList();
SELECT tablepercl0_.id AS id1_0_ ,
       tablepercl0_.balance AS balance2_0_ ,
       tablepercl0_.interestRate AS interest3_0_ ,
       tablepercl0_.owner AS owner4_0_ ,
       tablepercl0_.overdraftFee AS overdraf1_2_ ,
       tablepercl0_.creditLimit AS creditLi1_1_ ,
       tablepercl0_.clazz_ AS clazz_
FROM (
    SELECT    id ,
             balance ,
             interestRate ,
             owner ,
             CAST(NULL AS INT) AS overdraftFee ,
             CAST(NULL AS INT) AS creditLimit ,
             0 AS clazz_
    FROM     Account
    UNION ALL
    SELECT   id ,
             balance ,
             interestRate ,
             owner ,
             overdraftFee ,
             CAST(NULL AS INT) AS creditLimit ,
             1 AS clazz_
    FROM     DebitAccount
    UNION ALL
    SELECT   id ,
             balance ,
             interestRate ,
             owner ,
             CAST(NULL AS INT) AS overdraftFee ,
             creditLimit ,
             2 AS clazz_
    FROM     CreditAccount
) tablepercl0_

Polymorphic queries require multiple UNION queries, so be aware of the performance implications of a large class hierarchy.

Unfortunately, not all database systems support UNION ALL, in which case, UNION is going to be used instead of UNION ALL.

The following Hibernate dialects support UNION ALL:

  • AbstractHANADialect

  • AbstractTransactSQLDialect

  • CUBRIDDialect

  • DB2Dialect

  • H2Dialect

  • HSQLDialect

  • Ingres9Dialect

  • MySQL5Dialect

  • Oracle8iDialect

  • Oracle9Dialect

  • PostgreSQL81Dialect

  • RDMSOS2200Dialect

2.11.5. Implicit and explicit polymorphism

By default, when you query a base class entity, the polymorphic query will fetch all subclasses belonging to the base type.

However, you can even query interfaces or base classes that don’t belong to the JPA entity inheritance model.

For instance, considering the following DomainModelEntity interface:

Example 255. DomainModelEntity interface

public interface DomainModelEntity<ID> {

    ID getId();

    Integer getVersion();
}

If we have two entity mappings, a Book and a Blog, and the Blog entity is mapped with the @Polymorphism annotation and taking the PolymorphismType.EXPLICIT setting:

Example 256. @Polymorphism entity mapping

@Entity(name = "Event")
public static class Book implements DomainModelEntity<Long> {

    @Id
    private Long id;

    @Version
    private Integer version;

    private String title;

    private String author;

    //Getter and setters omitted for brevity
}

@Entity(name = "Blog")
@Polymorphism(type = PolymorphismType.EXPLICIT)
public static class Blog implements DomainModelEntity<Long> {

    @Id
    private Long id;

    @Version
    private Integer version;

    private String site;

    //Getter and setters omitted for brevity
}

If we have the following entity objects in our system:

Example 257. Domain Model entity objects

Book book = new Book();
book.setId( 1L );
book.setAuthor( "Vlad Mihalcea" );
book.setTitle( "High-Performance Java Persistence" );
entityManager.persist( book );

Blog blog = new Blog();
blog.setId( 1L );
blog.setSite( "vladmihalcea.com" );
entityManager.persist( blog );

We can now query against the DomainModelEntity interface, and Hibernate is going to fetch only the entities that are either mapped with @Polymorphism(type = PolymorphismType.IMPLICIT) or they are not annotated at all with the @Polymorphism annotation (implying the IMPLICIT behavior):

Example 258. Fetching Domain Model entities using non-mapped base class polymorphism

List<DomainModelEntity> accounts = entityManager
.createQuery(
    "select e " +
    "from org.hibernate.userguide.inheritance.polymorphism.DomainModelEntity e" )
.getResultList();

assertEquals(1, accounts.size());
assertTrue( accounts.get( 0 ) instanceof Book );

Therefore, only the Book was fetched since the Blog entity was marked with the @Polymorphism(type = PolymorphismType.EXPLICIT) annotation, which instructs Hibernate to skip it when executing a polymorphic query against a non-mapped base class.

2.12. Immutability

Immutability can be specified for both entities and collections.

2.12.1. Entity immutability

If a specific entity is immutable, it is good practice to mark it with the @Immutable annotation.

Example 259. Immutable entity

@Entity(name = "Event")
@Immutable
public static class Event {

    @Id
    private Long id;

    private Date createdOn;

    private String message;

    //Getters and setters are omitted for brevity

}

Internally, Hibernate is going to perform several optimizations, such as:

  • reducing memory footprint since there is no need to retain the dehydrated state for the dirty checking mechanism

  • speeding-up the Persistence Context flushing phase since immutable entities can skip the dirty checking process

Considering the following entity is persisted in the database:

Example 260. Persisting an immutable entity

doInJPA( this::entityManagerFactory, entityManager -> {
    Event event = new Event();
    event.setId( 1L );
    event.setCreatedOn( new Date( ) );
    event.setMessage( "Hibernate User Guide rocks!" );

    entityManager.persist( event );
} );

When loading the entity and trying to change its state, Hibernate will skip any modification, therefore no SQL UPDATE statement is executed.

Example 261. The immutable entity ignores any update

doInJPA( this::entityManagerFactory, entityManager -> {
    Event event = entityManager.find( Event.class, 1L );
    log.info( "Change event message" );
    event.setMessage( "Hibernate User Guide" );
} );
doInJPA( this::entityManagerFactory, entityManager -> {
    Event event = entityManager.find( Event.class, 1L );
    assertEquals("Hibernate User Guide rocks!", event.getMessage());
} );
SELECT e.id AS id1_0_0_,
       e.createdOn AS createdO2_0_0_,
       e.message AS message3_0_0_
FROM   event e
WHERE  e.id = 1

-- Change event message

SELECT e.id AS id1_0_0_,
       e.createdOn AS createdO2_0_0_,
       e.message AS message3_0_0_
FROM   event e
WHERE  e.id = 1

2.12.2. Collection immutability

Just like entities, collections can also be marked with the @Immutable annotation.

Considering the following entity mappings:

Example 262. Immutable collection

@Entity(name = "Batch")
public static class Batch {

    @Id
    private Long id;

    private String name;

    @OneToMany(cascade = CascadeType.ALL)
    @Immutable
    private List<Event> events = new ArrayList<>( );

    //Getters and setters are omitted for brevity

}

@Entity(name = "Event")
@Immutable
public static class Event {

    @Id
    private Long id;

    private Date createdOn;

    private String message;

    //Getters and setters are omitted for brevity

}

This time, not only the Event entity is immutable, but the Event collection stored by the Batch parent entity. Once the immutable collection is created, it can never be modified.

Example 263. Persisting an immutable collection

doInJPA( this::entityManagerFactory, entityManager -> {
    Batch batch = new Batch();
    batch.setId( 1L );
    batch.setName( "Change request" );

    Event event1 = new Event();
    event1.setId( 1L );
    event1.setCreatedOn( new Date( ) );
    event1.setMessage( "Update Hibernate User Guide" );

    Event event2 = new Event();
    event2.setId( 2L );
    event2.setCreatedOn( new Date( ) );
    event2.setMessage( "Update Hibernate Getting Started Guide" );

    batch.getEvents().add( event1 );
    batch.getEvents().add( event2 );

    entityManager.persist( batch );
} );

The Batch entity is mutable. Only the events collection is immutable.

For instance, we can still modify the entity name:

Example 264. Changing the mutable entity

doInJPA( this::entityManagerFactory, entityManager -> {
    Batch batch = entityManager.find( Batch.class, 1L );
    log.info( "Change batch name" );
    batch.setName( "Proposed change request" );
} );
SELECT b.id AS id1_0_0_,
       b.name AS name2_0_0_
FROM   Batch b
WHERE  b.id = 1

-- Change batch name

UPDATE batch
SET    name = 'Proposed change request'
WHERE  id = 1

However, when trying to modify the events collection:

Example 265. Immutable collections cannot be modified

try {
    doInJPA( this::entityManagerFactory, entityManager -> {
        Batch batch = entityManager.find( Batch.class, 1L );
        batch.getEvents().clear();
    } );
}
catch ( Exception e ) {
    log.error( "Immutable collections cannot be modified" );
}
javax.persistence.RollbackException: Error while committing the transaction

Caused by: javax.persistence.PersistenceException: org.hibernate.HibernateException:

Caused by: org.hibernate.HibernateException: changed an immutable collection instance: [
    org.hibernate.userguide.immutability.CollectionImmutabilityTest$Batch.events#1
]

While immutable entity changes are simply discarded, modifying an immutable collection will end up in a HibernateException being thrown.