@InstallSubject(value="commitDelegate") public interface DataContext
Within DataContext
, an entity with the given identifier is represented by a single object instance, no matter
where and how many times it is used in object graphs.
Modifier and Type | Interface and Description |
---|---|
static class |
DataContext.ChangeEvent
Event sent when the context detects changes in an entity, a new instance is merged or an entity is removed.
|
static class |
DataContext.PostCommitEvent
Event sent after committing changes.
|
static class |
DataContext.PreCommitEvent
Event sent before committing changes.
|
Modifier and Type | Method and Description |
---|---|
Subscription |
addChangeListener(java.util.function.Consumer<DataContext.ChangeEvent> listener)
Adds a listener to
DataContext.ChangeEvent . |
Subscription |
addPostCommitListener(java.util.function.Consumer<DataContext.PostCommitEvent> listener)
Adds a listener to
DataContext.PostCommitEvent . |
Subscription |
addPreCommitListener(java.util.function.Consumer<DataContext.PreCommitEvent> listener)
Adds a listener to
DataContext.PreCommitEvent . |
EntitySet |
commit()
Commits changed and removed instances to the middleware.
|
boolean |
contains(Entity entity)
Returns true if the context contains the given entity (distinguished by its class and id).
|
<T extends Entity> |
create(java.lang.Class<T> entityClass)
Creates an entity instance and merge it into the context.
|
void |
evict(Entity entity)
Removes the entity from the context so the context stops tracking it.
|
default void |
evictAll()
Cleans lists with information of created/modified/deleted entities for commit.
|
<T extends Entity<K>,K> |
find(java.lang.Class<T> entityClass,
K entityId)
Returns an entity instance by its class and id.
|
<T extends Entity> |
find(T entity)
Returns the instance of entity with the same id if it exists in this context.
|
java.util.function.Function<CommitContext,java.util.Set<Entity>> |
getCommitDelegate()
Returns a function which will be used to commit data instead of standard implementation.
|
java.util.Set<Entity> |
getModified()
Returns an immutable set of entities registered as modified.
|
DataContext |
getParent()
Returns a parent context, if any.
|
java.util.Set<Entity> |
getRemoved()
Returns an immutable set of entities registered for removal.
|
boolean |
hasChanges()
Returns true if the context has detected changes in the tracked entities.
|
boolean |
isModified(Entity entity)
Returns true if the context has detected changes in the given entity.
|
boolean |
isRemoved(Entity entity)
Returns true if the context has registered removal of the given entity.
|
EntitySet |
merge(java.util.Collection<? extends Entity> entities)
Merge the given entities into the context.
|
<T extends Entity> |
merge(T entity)
Merge the given entity into the context.
|
void |
remove(Entity entity)
Removes the entity from the context and registers it as deleted.
|
void |
setCommitDelegate(java.util.function.Function<CommitContext,java.util.Set<Entity>> delegate)
Sets a function which will be used to commit data instead of standard implementation.
|
void |
setModified(Entity entity,
boolean modified)
Registers or unregisters the given entity as modified.
|
void |
setParent(DataContext parentContext)
Sets the parent context.
|
@Nullable <T extends Entity<K>,K> T find(java.lang.Class<T> entityClass, K entityId)
@Nullable <T extends Entity> T find(T entity)
boolean contains(Entity entity)
default void evictAll()
@CheckReturnValue <T extends Entity> T merge(T entity)
If an entity with the same identifier already exists in the context, the passed entity state is copied into it and the existing instance is returned. Otherwise, a copy of the passed instance is registered in the context and returned.
If the given instance is new and the the context doesn't contain an instance with the same identifier, the context
will save the new instance on commit()
. Otherwise, even if some attributes of the merged instance are changed
as a result of copying the state of the passed instance, the merged instance will not be committed. Such modifications
are considered as a result of loading more fresh state from the database.
WARNING: use the returned value because it is always a different object instance.
The only case when you get the same instance is if the input was previously returned from the same context as a
result of find(Class, Object)
or merge()
.
@CheckReturnValue EntitySet merge(java.util.Collection<? extends Entity> entities)
Same as merge(Entity)
but for a collection of instances.
merge(Entity)
void remove(Entity entity)
commit()
.
If the given entity is not in the context, nothing happens.
void evict(Entity entity)
If the given entity is not in the context, nothing happens.
<T extends Entity> T create(java.lang.Class<T> entityClass)
Same as:
Foo foo = dataContext.merge(metadata.create(Foo.class));
entityClass
- entity classboolean hasChanges()
boolean isModified(Entity entity)
void setModified(Entity entity, boolean modified)
entity
- entity instance which is already merged into the contextmodified
- true to register or false to unregisterjava.util.Set<Entity> getModified()
boolean isRemoved(Entity entity)
java.util.Set<Entity> getRemoved()
EntitySet commit()
setParent(DataContext)
@Nullable DataContext getParent()
commit()
method merges the changed instances
to it instead of sending to the middleware.void setParent(DataContext parentContext)
commit()
method merges the changed instances
to it instead of sending to the middleware.Subscription addChangeListener(java.util.function.Consumer<DataContext.ChangeEvent> listener)
DataContext.ChangeEvent
.Subscription addPreCommitListener(java.util.function.Consumer<DataContext.PreCommitEvent> listener)
DataContext.PreCommitEvent
.
You can also add an event listener declaratively using a controller method annotated with Subscribe
:
@Subscribe(target = Target.DATA_CONTEXT) protected void onPreCommit(DataContext.PreCommitEvent event) { // handle event here }
listener
- listenerSubscription addPostCommitListener(java.util.function.Consumer<DataContext.PostCommitEvent> listener)
DataContext.PostCommitEvent
.
You can also add an event listener declaratively using a controller method annotated with Subscribe
:
@Subscribe(target = Target.DATA_CONTEXT) protected void onPostCommit(DataContext.PostCommitEvent event) { // handle event here }
listener
- listenerjava.util.function.Function<CommitContext,java.util.Set<Entity>> getCommitDelegate()
void setCommitDelegate(java.util.function.Function<CommitContext,java.util.Set<Entity>> delegate)