- Type Parameters:
T- record type.
Ref records are generally used for foreign key references in entities and projections, preventing deep object graphs. Alternatively, ref records can be used outside the scope entities and projections to simply act as a factory pattern for fetching records on demand.
Ref records are effectively immutable and can be used as keys in maps and sets. Equality is based on the primary key of the record and the actual record instance will be fetched at most once.
- Since:
- 1.3
-
Method Summary
Modifier and TypeMethodDescriptionstatic <ID,E extends Entity<ID>>
IDExtracts the primary key from the given entity ref returning a type-safe id.default Tfetch()Fetches the record if it has not been fetched yet.Fetches the record if it has not been fetched yet.Returns the record if it has already been fetched, without triggering a database call.id()Returns the primary key of the record.booleanReturns whether this ref is attached to a database context and capable of fetching the record on demand.default booleanisLoaded()Returns whether the record has already been fetched and is available in memory.of(E entity) Creates a fully loaded ref instance that wraps the given entity.Creates a detached ref instance for the given type and primary key.static <P extends Projection<ID>,ID>
Ref<P> of(P projection, ID id) Creates a fully loaded ref instance that wraps the given projection along with its primary key.static <ID,P extends Projection<ID>>
IDprojectionId(Ref<P> ref) Extracts the primary key from the given projection ref returning a type-safe id.type()The type of the record.unload()Returns a detached ref with the same identity but without data.
-
Method Details
-
type
The type of the record.- Returns:
- the type of the record.
-
of
Creates a detached ref instance for the given type and primary key.The returned ref is not connected to a database context. Calling
fetch()orfetchOrNull()on a detached ref will returnnullsince there is no database connection available to retrieve the record.- Type Parameters:
T- the type of the record, which must extendData.ID- the type of the primary key.- Parameters:
type- the class of the record.pk- the primary key of the record.- Returns:
- a detached ref instance for the given type and primary key.
-
of
Creates a fully loaded ref instance that wraps the given entity. -
of
Creates a fully loaded ref instance that wraps the given projection along with its primary key.- Type Parameters:
P- the type of the projection.ID- the type of the primary key.- Parameters:
projection- the fully loaded projection to wrap in a ref.id- the primary key of the projection.- Returns:
- a fully loaded ref instance for the provided projection.
-
entityId
Extracts the primary key from the given entity ref returning a type-safe id.- Type Parameters:
ID- the id type.E- the entity type.- Parameters:
ref- ref to extract the primary key from.- Returns:
- the primary key of the specified ref.
-
projectionId
Extracts the primary key from the given projection ref returning a type-safe id.- Type Parameters:
ID- the id type.P- the projection type.- Parameters:
ref- ref to extract the primary key from.- Returns:
- the primary key of the specified ref.
-
id
Object id()Returns the primary key of the record.This method is provided for convenience. If the type of the id is known, you can cast it to the appropriate type.
- Returns:
- the primary key as an Object.
-
getOrNull
Returns the record if it has already been fetched, without triggering a database call.- Returns:
- the record if already loaded, or
nullif not yet fetched. - Since:
- 1.7
-
fetch
Fetches the record if it has not been fetched yet. The record will be fetched at most once.Within a transaction, this method may return the same instance as other retrieval operations for the same primary key without querying the database, depending on the transaction isolation level.
- Returns:
- the fetched record.
- Throws:
PersistenceException- if the record is not available and the Ref is not attached.
-
fetchOrNull
Fetches the record if it has not been fetched yet. Returnsnullif the record is not available and the Ref is not attached.Within a transaction, this method may return the same instance as other retrieval operations for the same primary key without querying the database, depending on the transaction isolation level.
- Returns:
- the fetched record, or
nullif the record is not available and the Ref is not attached. - Since:
- 1.7
-
isFetchable
boolean isFetchable()Returns whether this ref is attached to a database context and capable of fetching the record on demand.A fetchable ref has access to a database connection and can attempt to retrieve the record when
fetch()orfetchOrNull()is called. A non-fetchable (detached) ref can only return data that was already loaded at the time of its creation.Note that this method indicates the capability to fetch, not a guarantee of success. A fetchable ref may still fail to retrieve a record if it has been deleted from the database or if the connection encounters an error.
- Returns:
trueif this ref can attempt to fetch from the database,falseif it is detached.- Since:
- 1.7
-
isLoaded
default boolean isLoaded()Returns whether the record has already been fetched and is available in memory.This method does not trigger a database call. It simply checks if the record is currently cached.
- Returns:
trueif the record is loaded and available viagetOrNull(),falseotherwise.- Since:
- 1.7
-
unload
Returns a detached ref with the same identity but without data.The returned ref is not attached to a database context. Calling
fetch()on the returned ref will fail since there is no database connection to recover the data.For refs that are already detached and unloaded, this method may return the same instance.
- Returns:
- a detached ref with the same type and primary key but without cached data.
-