- java.lang.Object
-
- io.github.mmm.entity.link.AbstractLink<E>
-
- io.github.mmm.entity.link.IdLink<E>
-
- Type Parameters:
E
- the generic type of thelinked
entity.
- All Implemented Interfaces:
Link<E>
public class IdLink<E> extends AbstractLink<E>
Implementation ofLink
that carries theId
of an entity. It optionally allows lazy loading on the first call ofgetTarget()
via an externally given resolverFunction
. This approach is easier, less invasive and more lightweight as using proxy objects for entities.- Since:
- 1.0.0
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Id<E>
getId()
E
getTarget()
boolean
isResolved()
static <E> IdLink<E>
of(Id<E> id, Function<Id<E>,E> resolver)
IdLink<E>
withType(Class<?> type)
-
Methods inherited from class io.github.mmm.entity.link.AbstractLink
equals, hashCode, toString
-
-
-
-
Method Detail
-
getId
public Id<E> getId()
- Returns:
- the
unique identifier
of the linkedbean
. When creating newEntities
a link may hold a transientEntity
astarget
that has no ID assigned, yet. In such case this method will returnnull
.
-
isResolved
public boolean isResolved()
- Returns:
true
if thelink target
is already resolved,false
otherwise (ifLink.getTarget()
may trigger lazy loading and could also fail if called without an open transaction).
-
getTarget
public E getTarget()
Description copied from interface:Link
This method resolves the linked entity.
ATTENTION:
ALink
can be evaluated lazily. In such case the first call of this method will resolve the linked entity from the database. That can be a relatively expensive operation and requires an open transaction. UseLink.isResolved()
to distinguish and prevent undesired link resolving.
Further, after serialization (e.g. mapping to JSON and back) maybe only theID
was transferred and this link can not be resolved. In that case this method may returnnull
. Please note thatid
andtarget
can not both be null. In case a link property is empty it will containnull
instead of an emptyLink
so you can always properly distinguish the scenarios.- Returns:
- the link target or
null
if the link can not be resolved.
-
withType
public IdLink<E> withType(Class<?> type)
- Parameters:
type
- the new value ofId.getType()
. Exact type should actually beClass
<E> but this prevents simple usage.- Returns:
- a copy of this
Link
with the giventype
or thisLink
itself if already satisfying. - See Also:
Id.withType(Class)
-
-