[[identifiers]]
=== Identifiers
:sourcedir: ../../../../../test/java/org/hibernate/userguide/mapping/identifier
:sourcedir-associations: ../../../../../test/java/org/hibernate/userguide/associations
:extrasdir: extras

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 <<chapters/domain/natural_id.adoc#naturalid,Natural Ids>>.

[NOTE]
====
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).

[[identifiers-simple]]
==== 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.

[[identifiers-simple-assigned]]
===== 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.

[[identifiers-simple-assigned-mapping-example]]
.Simple assigned entity identifier
====
[source,java]
----
include::{sourcedir}/AssignedIdentifierTest.java[tag=identifiers-simple-assigned-mapping-example, indent=0]
----
====

[[identifiers-simple-generated]]
===== Generated identifiers

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

[[identifiers-simple-generated-mapping-example]]
.Simple generated identifier
====
[source,java]
----
include::{sourcedir}/GeneratedIdentifierTest.java[tag=identifiers-simple-generated-mapping-example, indent=0]
----
====

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 <<identifiers-generators>> section.

[[identifiers-composite]]
==== 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 <<identifiers-composite-aggregated>>),
or defined using the `javax.persistence.IdClass` annotation (see <<identifiers-composite-nonaggregated>>).
* 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.

[NOTE]
====
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.

[[identifiers-composite-aggregated]]
==== 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.

[[identifiers-basic-embeddedid-mapping-example]]
.Basic `@EmbeddedId`
====
[source,java]
----
include::{sourcedir}/EmbeddedIdTest.java[tag=identifiers-basic-embeddedid-mapping-example, indent=0]
----
====

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

[[identifiers-basic-embeddedid-manytoone-mapping-example]]
.`@EmbeddedId` with `@ManyToOne`
====
[source,java]
----
include::{sourcedir}/EmbeddedIdManyToOneTest.java[tag=identifiers-basic-embeddedid-manytoone-mapping-example, indent=0]
----
====

[NOTE]
====
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 <<identifiers-derived>>.
====

[[identifiers-composite-nonaggregated]]
==== 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".

[[identifiers-basic-idclass-mapping-example]]
.Basic `@IdClass`
====
[source,java]
----
include::{sourcedir}/IdClassTest.java[tag=identifiers-basic-idclass-mapping-example, indent=0]
----
====

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

[[identifiers-basic-idclass-manytoone-mapping-example]]
.IdClass with `@ManyToOne`
====
[source,java]
----
include::{sourcedir}/IdClassManyToOneTest.java[tag=identifiers-basic-idclass-manytoone-mapping-example, indent=0]
----
====

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

[[identifiers-basic-idclass-generatedvalue-mapping-example]]
.`@IdClass` with partial identifier generation using `@GeneratedValue`
====
[source,java]
----
include::{sourcedir}/IdClassGeneratedValueTest.java[tag=identifiers-basic-idclass-generatedvalue-mapping-example, indent=0]
----
====

[NOTE]
====
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.
====

[[identifiers-composite-associations]]
==== 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.

[[identifiers-composite-id-mapping-example]]
.Composite identifiers with associations
====
[source,java]
----
include::{sourcedir}/IdManyToOneTest.java[tag=identifiers-composite-id-mapping-example, indent=0]
----
====

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.

[[identifiers-composite-id-fetching-example]]
.Fetching with composite identifiers
====
[source,java]
----
include::{sourcedir}/IdManyToOneTest.java[tag=identifiers-composite-id-fetching-example, indent=0]
----
====

[[identifiers-composite-generated]]
==== 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 <<chapters/domain/basic_types.adoc#mapping-generated, 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.

[[identifiers-composite-generated-mapping-example]]
.The Event entity and EventId composite identifier
====
[source,java]
----
include::{sourcedir}/composite/Event.java[tag=identifiers-composite-generated-mapping-example, indent=0]
----

[source,java]
----
include::{sourcedir}/composite/EventId.java[tag=identifiers-composite-generated-mapping-example, indent=0]
----
====

[[identifiers-composite-generated-in-memory]]
===== In-memory generated composite identifier properties

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

[[identifiers-composite-generated-in-memory-example]]
.In-memory generated composite identifier properties example
====
[source,java]
----
include::{sourcedir}/composite/EmbeddedIdInMemoryGeneratedValueTest.java[tag=identifiers-composite-generated-in-memory-example, indent=0]
----
====

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.

[[identifiers-composite-generated-database]]
===== 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.

[[identifiers-composite-generated-database-example]]
.Database generated composite identifier properties example
====
[source,java]
----
include::{sourcedir}/composite/EmbeddedIdDatabaseGeneratedValueTest.java[tag=identifiers-composite-generated-database-example, indent=0]
----
====

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.

[[identifiers-generators]]
==== Generated identifier values

[NOTE]
====
You can also auto-generate values for non-identifier attributes. For more details, see the <<chapters/domain/basic_types.adoc#mapping-generated,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.

[NOTE]
====
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`).
====

[IMPORTANT]
====
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 <<identifiers-generators-auto>>.
`IDENTITY`:: Indicates that database IDENTITY columns will be used for primary key value generation. See <<identifiers-generators-identity>>.
`SEQUENCE`:: Indicates that database sequence should be used for obtaining primary key values. See <<identifiers-generators-sequence>>.
`TABLE`:: Indicates that a database table should be used for obtaining primary key values. See <<identifiers-generators-table>>.

[[identifiers-generators-auto]]
==== 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 <<identifiers-generators-uuid, 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 <<appendices/Configurations.adoc#configurations-mapping,`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 <<appendices/Configurations.adoc#configurations-mapping,`hibernate.id.new_generator_mappings`>> configuration property is false.
	The legacy strategy maps `AUTO` to the `native` generator strategy which uses the https://docs.jboss.org/hibernate/orm/{majorMinorVersion}/javadocs/org/hibernate/dialect/Dialect.html#getNativeIdentifierGeneratorStrategy--[Dialect#getNativeIdentifierGeneratorStrategy] to resolve the actual identifier generator (e.g. `identity` or `sequence`).

[[identifiers-generators-sequence]]
==== 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.

[[identifiers-generators-sequence-unnamed]]
.Unnamed sequence
====
[source,java]
----
include::{sourcedir}/SequenceGeneratorUnnamedTest.java[tag=identifiers-generators-sequence-mapping-example, indent=0]
----
====

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

[[identifiers-generators-sequence-named]]
.Named sequence
====
[source,java]
----
include::{sourcedir}/SequenceGeneratorNamedTest.java[tag=identifiers-generators-sequence-mapping-example, indent=0]
----
====

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

[[identifiers-generators-sequence-configured]]
.Configured sequence
====
[source,java]
----
include::{sourcedir}/SequenceGeneratorConfiguredTest.java[tag=identifiers-generators-sequence-mapping-example, indent=0]
----
====

[[identifiers-generators-identity]]
==== 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`.

[IMPORTANT]
====
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).
====

[NOTE]
====
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.
====

[[identifiers-generators-table]]
==== 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.

[[identifiers-generators-table-unnamed-mapping-example]]
.Unnamed table generator
====
[source,java]
----
include::{sourcedir}/TableGeneratorUnnamedTest.java[tag=identifiers-generators-table-mapping-example, indent=0]
----

[source,sql]
----
include::{extrasdir}/id/identifiers-generators-table-unnamed-mapping-example.sql[]
----
====

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 https://javaee.github.io/javaee-spec/javadocs/javax/persistence/TableGenerator.html[`@TableGenerator`] annotation.

[[identifiers-generators-table-configured-mapping-example]]
.Configured table generator
====
[source,java]
----
include::{sourcedir}/TableGeneratorConfiguredTest.java[tag=identifiers-generators-table-mapping-example, indent=0]
----

[source,sql]
----
include::{extrasdir}/id/identifiers-generators-table-configured-mapping-example.sql[]
----
====

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

[[identifiers-generators-table-configured-persist-example]]
.Configured table generator persist example
====
[source,java]
----
include::{sourcedir}/TableGeneratorConfiguredTest.java[tag=identifiers-generators-table-persist-example, indent=0]
----

[source,sql]
----
include::{extrasdir}/id/identifiers-generators-table-configured-persist-example.sql[]
----
====

[[identifiers-generators-uuid]]
==== 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).

[[identifiers-generators-uuid-mapping-example]]
.Implicitly using the random UUID strategy
====
[source,java]
----
include::{sourcedir}/UuidGeneratedValueTest.java[tag=identifiers-generators-uuid-mapping-example, indent=0]
----
====

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`.

[[identifiers-generators-custom-uuid-mapping-example]]
.Implicitly using the random UUID strategy
====
[source,java]
----
include::{sourcedir}/UuidCustomGeneratedValueTest.java[tag=identifiers-generators-custom-uuid-mapping-example, indent=0]
----
====

[[identifiers-generators-optimizer]]
==== 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.

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

[[identifiers-generators-GenericGenerator]]
==== 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 https://docs.jboss.org/hibernate/orm/{majorMinorVersion}/javadocs/org/hibernate/annotations/GenericGenerator.html[`@GenericGenerator`] annotation:

[[identifiers-generators-pooled-lo-optimizer-mapping-example]]
.Pooled-lo optimizer mapping using `@GenericGenerator` mapping
====
[source, JAVA, indent=0]
----
include::{sourcedir}/PooledOptimizerTest.java[tag=identifiers-generators-pooled-lo-optimizer-mapping-example]
----
====

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

[[identifiers-generators-pooled-lo-optimizer-persist-example]]
.Pooled-lo optimizer mapping using `@GenericGenerator` mapping
====
[source, JAVA, indent=0]
----
include::{sourcedir}/PooledOptimizerTest.java[tag=identifiers-generators-pooled-lo-optimizer-persist-example]
----

[source, SQL, indent=0]
----
include::{extrasdir}/id/identifiers-generators-pooled-lo-optimizer-persist-example.sql[]
----
====

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.

[[identifiers-derived]]
==== 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.

[[identifiers-derived-mapsid]]
.Derived identifier with `@MapsId`
====
[source,java]
----
include::{sourcedir-associations}/OneToOneMapsIdTest.java[tag=identifiers-derived-mapsid, indent=0]
----
====

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.

[[identifiers-derived-mapsid-persist-example]]
.Derived identifier with `@MapsId` persist example
====
[source,java]
----
include::{sourcedir-associations}/OneToOneMapsIdTest.java[tag=identifiers-derived-mapsid-persist-example, indent=0]
----
====

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

The previous example can also be mapped using `@PrimaryKeyJoinColumn`.

[[identifiers-derived-primarykeyjoincolumn]]
.Derived identifier `@PrimaryKeyJoinColumn`
====
[source,java]
----
include::{sourcedir-associations}/OneToOnePrimaryKeyJoinColumnTest.java[tag=identifiers-derived-primarykeyjoincolumn, indent=0]
----
====

[NOTE]
====
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.
====

[[identifiers-rowid]]
==== @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.

[[identifiers-rowid-mapping]]
.`@RowId` entity mapping
====
[source, JAVA, indent=0]
----
include::{sourcedir}/RowIdTest.java[tag=identifiers-rowid-mapping]
----
====

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

[[identifiers-rowid-example]]
.`@RowId` example
====
[source, JAVA, indent=0]
----
include::{sourcedir}/RowIdTest.java[tag=identifiers-rowid-example]
----

[source, SQL, indent=0]
----
include::{extrasdir}/id/identifiers-rowid-example.sql[]
----
====
