@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 |
---|---|
com.haulmont.bali.events.Subscription |
addChangeListener(java.util.function.Consumer<DataContext.ChangeEvent> listener)
Adds a listener to
DataContext.ChangeEvent . |
com.haulmont.bali.events.Subscription |
addPostCommitListener(java.util.function.Consumer<DataContext.PostCommitEvent> listener)
Adds a listener to
DataContext.PostCommitEvent . |
com.haulmont.bali.events.Subscription |
addPreCommitListener(java.util.function.Consumer<DataContext.PreCommitEvent> listener)
Adds a listener to
DataContext.PreCommitEvent . |
void |
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.
|
<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.
|
DataContext |
getParent()
Returns a parent context, if any.
|
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 |
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)
@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)
boolean isRemoved(Entity entity)
void 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.com.haulmont.bali.events.Subscription addChangeListener(java.util.function.Consumer<DataContext.ChangeEvent> listener)
DataContext.ChangeEvent
.com.haulmont.bali.events.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
- listenercom.haulmont.bali.events.Subscription 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)