public interface TransactionalDataManager
DataManager
but joins an existing transaction.
Use this bean in the middleware logic to perform some operations in one transaction, for example:
@Inject private TransactionalDataManager txDataManager; @Transactional private void transfer(Id<Account, UUID> acc1Id, Id<Account, UUID> acc2Id, Long amount) { Account acc1 = txDataManager.load(acc1Id).one(); Account acc2 = txDataManager.load(acc2Id).one(); acc1.setBalance(acc1.getBalance() - amount); acc2.setBalance(acc2.getBalance() + amount); txDataManager.save(acc1); txDataManager.save(acc2); }Transactions can also be created/committed programmatically using the
Transactions
interface which is available
via transactions()
method.Modifier and Type | Field and Description |
---|---|
static java.lang.String |
NAME |
Modifier and Type | Method and Description |
---|---|
TransactionalAction |
commitAction(java.util.function.Supplier<CommitContext> supplier)
Entry point to TransactionalAction API.
|
<T extends Entity> |
create(java.lang.Class<T> entityClass)
Creates a new entity instance.
|
<T extends BaseGenericIdEntity<K>,K> |
getReference(java.lang.Class<T> entityClass,
K id)
Returns an entity instance which can be used as a reference to an object which exists in the database.
|
default <T extends BaseGenericIdEntity<K>,K> |
getReference(Id<T,K> entityId)
Returns an entity instance which can be used as a reference to an object which exists in the database.
|
<E extends Entity<K>,K> |
load(java.lang.Class<E> entityClass)
Entry point to the fluent API for loading entities.
|
<E extends Entity<K>,K> |
load(Id<E,K> entityId)
Entry point to the fluent API for loading entities.
|
<E extends Entity> |
load(LoadContext<E> context)
Loads a single entity instance.
|
<E extends Entity> |
loadList(LoadContext<E> context)
Loads collection of entity instances.
|
<T> FluentValueLoader<T> |
loadValue(java.lang.String queryString,
java.lang.Class<T> valueClass)
Entry point to the fluent API for loading a single scalar value.
|
FluentValuesLoader |
loadValues(java.lang.String queryString)
Entry point to the fluent API for loading scalar values.
|
java.util.List<KeyValueEntity> |
loadValues(ValueLoadContext context)
Loads list of key-value pairs.
|
void |
remove(Entity entity)
Removes the entity instance from the data store.
|
void |
remove(Entity entity,
boolean softDeletion)
Removes the entity instance from the data store.
|
default <T extends BaseGenericIdEntity<K>,K> |
remove(Id<T,K> entityId)
Removes the entity instance from the data store by its id.
|
<E extends Entity> |
save(E entity)
Saves the entity to the data store.
|
EntitySet |
save(Entity... entities)
Saves entity instances to the data store.
|
<E extends Entity> |
save(E entity,
java.lang.String viewName)
Saves the entity to the data store.
|
<E extends Entity> |
save(E entity,
View view)
Saves the entity to the data store.
|
TransactionalDataManager |
secure()
By default, DataManager does not apply security restrictions on entity operations and attributes, only row-level
constraints take effect.
|
Transactions |
transactions()
Returns an entry point to programmatic transaction control.
|
static final java.lang.String NAME
<E extends Entity<K>,K> FluentLoader<E,K> load(java.lang.Class<E> entityClass)
Usage examples:
Customer customer = txDataManager.load(Customer.class).id(someId).one(); List<Customer> customers = txDataManager.load(Customer.class) .query("select c from sample$Customer c where c.name = :name") .parameter("name", "Smith") .view("customer-view") .list();
entityClass
- class of entity that needs to be loaded<E extends Entity<K>,K> FluentLoader.ById<E,K> load(Id<E,K> entityId)
Usage example:
Customer customer = txDataManager.load(customerId).view("with-grade").one();
entityId
- Id
of entity that needs to be loadedFluentValuesLoader loadValues(java.lang.String queryString)
Usage examples:
List<KeyValueEntity> customerDataList = txDataManager.loadValues( "select c.name, c.status from sample$Customer c where c.name = :n") .properties("custName", "custStatus") .parameter("name", "Smith") .list(); KeyValueEntity customerData = txDataManager.loadValues( "select c.name, count(c) from sample$Customer c group by c.name") .properties("custName", "custCount") .one();
queryString
- query string<T> FluentValueLoader<T> loadValue(java.lang.String queryString, java.lang.Class<T> valueClass)
Terminal methods of this API (list
, one
and optional
) return a single value
from the first column of the query result set. You should provide the expected type of this value in the second
parameter. Number types will be converted appropriately, so for example if the query returns Long and you
expected Integer, the returned value will be automatically converted from Long to Integer.
Usage examples:
Long customerCount = txDataManager.loadValue( "select count(c) from sample$Customer c", Long.class).one();
queryString
- query stringvalueClass
- type of the returning value@Nullable @CheckReturnValue <E extends Entity> E load(LoadContext<E> context)
context
- LoadContext
object, defining what and how to load@CheckReturnValue <E extends Entity> java.util.List<E> loadList(LoadContext<E> context)
context
- LoadContext
object, defining what and how to load@CheckReturnValue java.util.List<KeyValueEntity> loadValues(ValueLoadContext context)
context
- defines a query for scalar values and a list of keys for returned KeyValueEntityEntitySet save(Entity... entities)
entities
- entities to save<E extends Entity> E save(E entity)
entity
- entity instance<E extends Entity> E save(E entity, @Nullable View view)
entity
- entity instanceview
- view object which affects the returned instance<E extends Entity> E save(E entity, @Nullable java.lang.String viewName)
entity
- entity instanceviewName
- name of a view which affects the returned instancevoid remove(Entity entity)
entity
- entity instancevoid remove(Entity entity, boolean softDeletion)
entity
- entity instancesoftDeletion
- whether to use soft deletion for entities implementing SoftDelete
(true by default)default <T extends BaseGenericIdEntity<K>,K> void remove(Id<T,K> entityId)
entityId
- entity id<T extends Entity> T create(java.lang.Class<T> entityClass)
Metadata.create()
.entityClass
- entity class<T extends BaseGenericIdEntity<K>,K> T getReference(java.lang.Class<T> entityClass, K id)
For example, if you are creating a User, you have to set a Group the user belongs to. If you know the group id, you could load it from the database and set to the user. This method saves you from unneeded database round trip:
user.setGroup(dataManager.getReference(Group.class, groupId)); dataManager.commit(user);A reference can also be used to delete an existing object by id:
dataManager.remove(dataManager.getReference(Customer.class, customerId));
entityClass
- entity classid
- id of an existing objectdefault <T extends BaseGenericIdEntity<K>,K> T getReference(Id<T,K> entityId)
entityId
- id of an existing objectgetReference(Class, Object)
TransactionalDataManager secure()
This method returns the TransactionalDataManager
implementation that applies security restrictions on entity operations.
Attribute permissions will be enforced only if you additionally set the cuba.entityAttributePermissionChecking
application property to true.
Usage example:
txDataManager.secure().load(Customer.class).list();
Transactions transactions()
Usage example:
try (Transaction tx = txDataManager.transactions().create()) { // ... tx.commit(); }
TransactionalAction commitAction(java.util.function.Supplier<CommitContext> supplier)
supplier
- defines how to retrieve CommitContext
TransactionalAction
without any additional
actions (onSuccess, onFail, beforeCommit, afterCompletion
) and with joinTransaction=true