-
- Type Parameters:
E
- type of the identified entity.
- All Known Implementing Classes:
AbstractId
,AbstractInstantId
,AbstractVersionId
,LongInstantId
,LongVersionId
,StringInstantId
,StringVersionId
,UuidInstantId
,UuidVersionId
public interface Id<E>
This is the interface for an ID that uniquely identifies a persistent entity of a particulartype
(<E>
).
AnId
is build out of the following parts:object-id
- the primary key that identifies the entity and is unique for a specifictype
.type
- is the (optional) type of the identified entity.version
- the optional version (revision) of the entity.
primary key
theversion
andtype
of an object do not change. This allows to use theId
as globally unique identifier for its corresponding entity.
AnId
has a compactstring representation
that can be converted back to anId
. Therefore, the implementation shall provide aString
-arg constructor and a staticvalueOf(String)
method.- Since:
- 1.0.0
- See Also:
AbstractId
-
-
Field Summary
Fields Modifier and Type Field Description static String
PROPERTY_ID
The name of theID
property (e.g.static String
PROPERTY_VERSION
The name of theversion
property (e.g.static Comparable<?>
VERSION_LATEST
The value used asversion
if it unspecified.static char
VERSION_SEPARATOR
The separator for theversion
.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description Object
getId()
default String
getIdAsString()
Class<E>
getType()
Comparable<?>
getVersion()
default String
getVersionAsString()
static <E extends Entity>
Id<E>of(E entity)
Type-safe andnull
-safe variant ofEntity.getId()
.String
toString()
Id<E>
withLatestVersion()
Id<E>
withType(Class<?> type)
-
-
-
Field Detail
-
PROPERTY_ID
static final String PROPERTY_ID
The name of theID
property (e.g. for JSON or XML).- See Also:
- Constant Field Values
-
PROPERTY_VERSION
static final String PROPERTY_VERSION
The name of theversion
property (e.g. for JSON or XML).- See Also:
- Constant Field Values
-
VERSION_LATEST
static final Comparable<?> VERSION_LATEST
-
VERSION_SEPARATOR
static final char VERSION_SEPARATOR
The separator for theversion
.- See Also:
- Constant Field Values
-
-
Method Detail
-
getId
Object getId()
- Returns:
- the primary key of the identified
Entity
asObject
value. It is only unique for a particulartype
of an entity. - See Also:
LongVersionId
,UuidVersionId
,StringVersionId
-
getIdAsString
default String getIdAsString()
- Returns:
- the
primary key
asString
for marshalling.
-
getType
Class<E> getType()
- Returns:
- the
Class
reflecting the type of the referenced entity. May benull
if not available.
-
withLatestVersion
Id<E> withLatestVersion()
- Returns:
- a copy of this
Id
with aversion
ofVERSION_LATEST
e.g. to use theId
as regular foreign key (pointing to the latest revision and not a historic revision) or thisId
itself if already satisfying.
-
getVersion
Comparable<?> getVersion()
- Returns:
- the
version
of this entity. Whenever theEntity
gets updated (a modification is saved and the transaction is committed), this version is increased. Typically the version is aNumber
starting with0
for a newEntity
that is increased whenever a modification is committed. However, it may also be anInstant
. The version acts as a modification sequence for optimistic locking. On each update it will be verified that the version has not been increased already by another transaction. When linking anEntity
(Id
used as foreign key) the version can act as revision for auditing. If it isVERSION_LATEST
(null
) it points to the latest revision of theEntity
. Otherwise it points to a specific historic revision of theEntity
. Depending on the database technology (e.g. when using hibernate envers) the version and the revision can be semantically different. In such case aprimary key
can not be converted 1:1 as revisioned foreign keyId
.
-
getVersionAsString
default String getVersionAsString()
-
toString
String toString()
-
-