I
- the generic type of the primary key / id used by the entityE
- the generic type of the referenced entitypublic abstract class BaseEntityRef<I,E extends BaseEntity<I>> extends Object
Instead of directly keeping the entity in a Java field, it is wrapped in an EntityRef. This leads to clean semantics for lazy loading as both the ID and (if fetched) the value are stored in this wrapper.
Modifier and Type | Class and Description |
---|---|
static class |
BaseEntityRef.OnDelete
Declares cascade actions which determine what happens if a referenced entity is deleted.
|
Modifier and Type | Field and Description |
---|---|
protected BaseEntityRef.OnDelete |
deleteHandler |
protected I |
id |
protected Class<E> |
type |
protected E |
value |
Modifier | Constructor and Description |
---|---|
protected |
BaseEntityRef(Class<E> type,
BaseEntityRef.OnDelete deleteHandler) |
Modifier and Type | Method and Description |
---|---|
protected abstract I |
coerceToId(Object id)
Converts the given object into an id.
|
boolean |
containsNonpersistentValue()
Determines if the given entity value was not yet persisted to the database.
|
protected abstract Optional<E> |
find(Class<E> type,
I id)
Performs the lookup of the entity with the given id.
|
BaseEntityRef.OnDelete |
getDeleteHandler()
Returns the delete handler.
|
I |
getId()
Returns the id of the referenced entity.
|
Class<E> |
getType()
Returns the entity type being referenced.
|
String |
getUniqueObjectName()
Returns the Unique Object Name for the referenced entity.
|
E |
getValue()
Returns the effective entity object which is referenced.
|
boolean |
is(Object entityOrId)
Determines if the referenced entity is equal to the given entity or id.
|
boolean |
isEmpty()
Opposite of
isFilled() . |
boolean |
isFilled()
Determines if an entity is referenced.
|
boolean |
isValueLoaded()
Determines if the entity object for the referenced id is loaded.
|
void |
setId(I id)
Sets the id of the referenced entity.
|
void |
setValue(E value)
Sets the entity being referenced.
|
protected Class<E extends BaseEntity<I>> type
protected BaseEntityRef.OnDelete deleteHandler
protected I id
protected E extends BaseEntity<I> value
protected BaseEntityRef(Class<E> type, BaseEntityRef.OnDelete deleteHandler)
public Class<E> getType()
public BaseEntityRef.OnDelete getDeleteHandler()
@Nullable public I getId()
public void setId(@Nullable I id)
id
- the id of the referenced entity or null if no entity is referenced.public String getUniqueObjectName()
BaseEntity.getUniqueName()
@Nullable public E getValue()
Note, this might cause a database lookup if the entity is not prefetched.
protected abstract Optional<E> find(Class<E> type, I id)
type
- the type to search forid
- the id to lookuppublic void setValue(@Nullable E value)
value
- the entity being referenced or null if no entity is referenced.public boolean isValueLoaded()
public boolean isFilled()
The referenced entity might still not be loaded yet.
public boolean isEmpty()
isFilled()
.public boolean is(Object entityOrId)
entityOrId
- the entity or id to check forprotected abstract I coerceToId(@Nonnull Object id)
id
- the value to coercepublic boolean containsNonpersistentValue()
For obvious reasons, a non persisted entity cannot be referenced once the referencing entity is saved.
Copyright © 2018. All rights reserved.