com.vaadin.addon.jpacontainer
Interface BatchableEntityProvider<T>

All Superinterfaces:
EntityProvider<T>, MutableEntityProvider<T>, Serializable
All Known Implementing Classes:
BatchableLocalEntityProvider, CachingBatchableEntityProvider, CachingBatchableLocalEntityProvider

public interface BatchableEntityProvider<T>
extends MutableEntityProvider<T>

An extended version of MutableEntityProvider that can execute several update operations (that is, adding new entities, and updating and removing existing entities) inside a single transaction. This feature is used by JPAContainer when write-through/auto-commit is turned off.

If the entites handled by this provider contain complex associations, special care should be taken to enforce data integrity. The following example scenarios might give unexpected results if not dealt with properly:

Example 1

  1. Add Entity1
  2. Add Entity2
  3. Update Entity1 to reference Entity2
  4. Run batch
Depending on the entity manager implementation and cascading settings, this might cause an exception due to a nonexistent reference to Entity2 at step 1, or a duplicate of Entity2 at step 2.

Example 2

  1. Add Entity1
  2. Add Entity2
  3. Update Entity1 to reference Entity2
  4. Remove Entity2
  5. Run batch
First of all, Entity2 should not be added at all as it is added and removed inside the same transaction. However, there is still a reference to Entity2 from Entity1. If cascading is turned on, this might result in both Entity1 and Entity2 being added nonetheless.

Since:
1.0
Author:
Petter Holmström (Vaadin Ltd)
See Also:
JPAContainer.setWriteThrough(boolean), JPAContainer.isWriteThrough()

Nested Class Summary
static interface BatchableEntityProvider.BatchUpdateCallback<T>
          Callback interface used by the batchUpdate(com.vaadin.addon.jpacontainer.BatchableEntityProvider.BatchUpdateCallback) method.
 
Method Summary
 void batchUpdate(BatchableEntityProvider.BatchUpdateCallback<T> callback)
          Executes a batch update using the specified callback parameter.
 
Methods inherited from interface com.vaadin.addon.jpacontainer.MutableEntityProvider
addEntity, removeEntity, updateEntity, updateEntityProperty
 
Methods inherited from interface com.vaadin.addon.jpacontainer.EntityProvider
containsEntity, getAllEntityIdentifiers, getEntity, getEntityCount, getEntityIdentifierAt, getEntityManager, getEntityManagerProvider, getFirstEntityIdentifier, getIdentifier, getLastEntityIdentifier, getLazyLoadingDelegate, getNextEntityIdentifier, getPreviousEntityIdentifier, getQueryModifierDelegate, isEntitiesDetached, refresh, refreshEntity, setEntitiesDetached, setEntityManager, setEntityManagerProvider, setLazyLoadingDelegate, setQueryModifierDelegate
 

Method Detail

batchUpdate

void batchUpdate(BatchableEntityProvider.BatchUpdateCallback<T> callback)
                 throws UnsupportedOperationException
Executes a batch update using the specified callback parameter. The batch update should be run inside a single transaction. The implementation may either handle the transaction itself, or rely on external transaction handling provided by a container such as Spring or EJB. If the batch update fails, the entire transaction should be rolled back and an exception thrown. Otherwise, it should be committed.

Clients should instantiate BatchableEntityProvider.BatchUpdateCallback, implement the BatchableEntityProvider.BatchUpdateCallback.batchUpdate(MutableEntityProvider) method and execute the updates as if they were using an ordinary MutableEntityProvider. The following example saves a list of transient entities:

  provider.batchUpdate(new BatchUpdateCallback() {
      public void batchUpdate(MutableEntityProvider batchEnabledEntityProvider) {
          for (Entity e : myListOfEntitiesToAdd) {
              batchEnabledEntityProvider.addEntity(e);
          }
      }
  });
 

Parameters:
callback - the callback that will be used to run the batch update.
Throws:
UnsupportedOperationException - if this entity provider does not support batch updates.


Copyright © 2013 Vaadin Ltd. All rights reserved.