B
- the type of entities supported by this mapperC
- the type of constraints supported by this mapperQ
- the type of queries supported by this mapperpublic abstract class BaseMapper<B extends BaseEntity<?>,C extends Constraint,Q extends Query<?,? extends B,C>> extends Object
Modifier and Type | Field and Description |
---|---|
protected Mixing |
mixing |
static String |
VERSION
Contains the name of the version column used for optimistic locking.
|
Constructor and Description |
---|
BaseMapper() |
Modifier and Type | Method and Description |
---|---|
protected abstract void |
createEntity(B entity,
EntityDescriptor ed)
Creates a new entity in the underlying database.
|
<E extends B> |
delete(E entity)
Deletes the given entity from the database.
|
protected abstract void |
deleteEntity(B entity,
boolean force,
EntityDescriptor ed)
Deletes the give entity from the database.
|
abstract FilterFactory<C> |
filters()
Returns the filter factory which is used by this mapper.
|
<E extends B> |
find(Class<E> type,
Object id,
ContextInfo... info)
Performs a database lookup to select the entity of the given type with the given id.
|
protected abstract <E extends B> |
findEntity(E entity) |
protected abstract <E extends B> |
findEntity(Object id,
EntityDescriptor ed,
Function<String,sirius.kernel.commons.Value> context)
Tries to find the entity with the given id.
|
<E extends B> |
findOrFail(Class<E> type,
Object id,
ContextInfo... info)
Tries to
find(Class, Object, ContextInfo...) the entity with the given id. |
<E extends B> |
forceDelete(E entity)
Deletes the given entity from the database even if it is
Versioned and was
concurrently modified. |
boolean |
hasValidationWarnings(B entity)
Determines if the given entity has validation warnings.
|
<E extends B> |
override(E entity)
Performs an
update(BaseEntity) of the entity, without checking for concurrent modifications. |
protected <E extends B> |
performDelete(E entity,
boolean force) |
protected <E extends B> |
performUpdate(E entity,
boolean force) |
<E extends B> |
refreshOrFail(E entity)
Tries to fetch a fresh (updated) instance of the given entity from the database.
|
<E extends B> |
resolve(String name,
ContextInfo... info)
Tries to resolve the
BaseEntity.getUniqueName() into an entity. |
B |
resolveOrFail(String name,
ContextInfo... info)
Tries to
resolve(String, ContextInfo...) the given name into an entity. |
abstract <E extends B> |
select(Class<E> type)
Creates a query for the given type.
|
<E extends B> |
tryDelete(E entity)
Tries to delete the entity from the database.
|
<E extends B> |
tryRefresh(E entity)
Tries to fetch a fresh (updated) instance of the given entity from the database.
|
<E extends B> |
tryUpdate(E entity)
Tries to perform an
update(BaseEntity) of the given entity. |
<E extends B> |
update(E entity)
Writes the contents of the given entity to the database.
|
protected abstract void |
updateEntity(B entity,
boolean force,
EntityDescriptor ed)
Updates an existing entity in the underlying database.
|
List<String> |
validate(B entity)
Executes all validation handlers on the given entity.
|
public static final String VERSION
protected Mixing mixing
public <E extends B> void update(E entity)
If the entity is not persisted yet, we perform an insert. If the entity does exist, we only update those fields, which were changed since they were last read from the database.
While this provides the best performance and circumvents update conflicts, it does not guarantee strong
consistency as the fields in the database might have partially changes. If this behaviour is unwanted,
Versioned
can be used which will turn on Optimistic Locking and
prevent these conditions.
E
- the generic type of the entityentity
- the entity to write to the databasepublic <E extends B> void tryUpdate(E entity) throws OptimisticLockException
update(BaseEntity)
of the given entity.
If the entity is Versioned
and the entity was modified already elsewhere, an
OptimisticLockException
will be thrown, which can be used to trigger a retry.
E
- the generic type of the entityentity
- the entity to updateOptimisticLockException
- in case of a concurrent modificationpublic <E extends B> void override(E entity)
update(BaseEntity)
of the entity, without checking for concurrent modifications.
Concurrent modifications by other users will simply be ignored and overridden.
E
- the generic type of the entityentity
- the entity to updateprotected <E extends B> void performUpdate(E entity, boolean force) throws OptimisticLockException
OptimisticLockException
protected abstract void createEntity(B entity, EntityDescriptor ed) throws Exception
entity
- the entity to createed
- the descriptor of the entityException
- in case of an database errorprotected abstract void updateEntity(B entity, boolean force, EntityDescriptor ed) throws Exception
entity
- the entity to updateforce
- ture if the update is forced and optimistic locking must be disableded
- the descriptor of the entityException
- in case of an database errorpublic <E extends B> void delete(E entity)
If the entity is Versioned
and concurrently modified elsewhere,
an exception is thrown.
E
- the generic entity typeentity
- the entity to deletepublic <E extends B> void tryDelete(E entity) throws OptimisticLockException
If the entity is Versioned
and concurrently modified elsewhere,
an OptimisticLockException
is thrown.
E
- the generic entity typeentity
- the entity to deleteOptimisticLockException
- if the entity was concurrently modifiedpublic <E extends B> void forceDelete(E entity)
Versioned
and was
concurrently modified.E
- the generic entity typeentity
- the entity to deleteprotected <E extends B> void performDelete(E entity, boolean force) throws OptimisticLockException
OptimisticLockException
protected abstract void deleteEntity(B entity, boolean force, EntityDescriptor ed) throws Exception
entity
- the entity to deleteforce
- true if the delete is forced and optimistic locking must be disableded
- the descriptor of the entityException
- in case of an database errorpublic boolean hasValidationWarnings(B entity)
entity
- the entity to checkpublic List<String> validate(B entity)
entity
- the entity to validatepublic <E extends B> Optional<E> find(Class<E> type, Object id, ContextInfo... info)
E
- the generic type of the entity to selecttype
- the type of entity to selectid
- the id (which can be either a long, Long or String) to selectinfo
- info provided as context (e.g. routing infos for Elasticsearch)protected abstract <E extends B> Optional<E> findEntity(Object id, EntityDescriptor ed, Function<String,sirius.kernel.commons.Value> context) throws Exception
E
- the effective type of the entityid
- the id of the entity to finded
- the descriptor of the entity to findcontext
- the advanced search context which can be populated using ContextInfo
in
find(Class, Object, ContextInfo...)
Exception
- in case of a database errorpublic <E extends B> E findOrFail(Class<E> type, Object id, ContextInfo... info)
find(Class, Object, ContextInfo...)
the entity with the given id.
If no entity is found, an exception is thrown.
E
- the generic type of the entity to selecttype
- the type of entity to selectid
- the id (which can be either a long, Long or String) to selectinfo
- info provided as context (e.g. routing infos for Elasticsearch)sirius.kernel.health.HandledException
- if no entity with the given ID was presentpublic <E extends B> Optional<E> resolve(String name, ContextInfo... info)
BaseEntity.getUniqueName()
into an entity.E
- the generic parameter of the entity to resolvename
- the name of the entity to resolveinfo
- info provided as context (e.g. routing infos for Elasticsearch)public B resolveOrFail(String name, ContextInfo... info)
resolve(String, ContextInfo...)
the given name into an entity.name
- the name of the entity to resolveinfo
- info provided as context (e.g. routing infos for Elasticsearch)sirius.kernel.health.HandledException
- if the given name cannot be resolved into an entitypublic <E extends B> E tryRefresh(E entity)
If the entity does no longer exist, the given instance is returned.
E
- the generic type of the entityentity
- the entity to refreshpublic <E extends B> E refreshOrFail(E entity)
If the entity does no longer exist, an exception will be thrown.
E
- the generic type of the entityentity
- the entity to refreshsirius.kernel.health.HandledException
- if the entity no longer exists in the database.public abstract <E extends B> Q select(Class<E> type)
E
- the generic type of entities to be returnedtype
- the type of entities to query for.public abstract FilterFactory<C> filters()
Copyright © 2018. All rights reserved.