Class MutableLocalEntityProvider<T>

java.lang.Object
com.vaadin.featurepack.data.jpacontainer.provider.LocalEntityProvider<T>
com.vaadin.featurepack.data.jpacontainer.provider.MutableLocalEntityProvider<T>
All Implemented Interfaces:
EntityProvider<T>, EntityProviderChangeNotifier<T>, MutableEntityProvider<T>, Serializable
Direct Known Subclasses:
BatchableLocalEntityProvider, CachingMutableLocalEntityProvider, MutableEntityProvider

public class MutableLocalEntityProvider<T> extends LocalEntityProvider<T> implements MutableEntityProvider<T>, EntityProviderChangeNotifier<T>
Extended version of LocalEntityProvider that provides editing support. Transactions can either be handled internally by the provider, or by an external container such as Spring or EJB (see the JPAContainer manual for examples of how to do this). By default, transactions are handled internally by invoking the transaction methods of the EntityManager.

This entity provider fires EntityProviderChangeEvents every time an entity is added, updated or deleted.

Since:
1.0
Author:
Petter Holmström (Vaadin Ltd)
See Also:
  • Constructor Details

    • MutableLocalEntityProvider

      public MutableLocalEntityProvider(Class<T> entityClass)
      Creates a new MutableLocalEntityProvider. The entity manager must be set using LocalEntityProvider.setEntityManager(javax.persistence.EntityManager).
      Parameters:
      entityClass - the entity class (must not be null).
    • MutableLocalEntityProvider

      public MutableLocalEntityProvider(Class<T> entityClass, jakarta.persistence.EntityManager entityManager)
      Creates a new MutableLocalEntityProvider.
      Parameters:
      entityClass - the entity class (must not be null).
      entityManager - the entity manager to use (must not be null).
  • Method Details

    • setTransactionsHandledByProvider

      public void setTransactionsHandledByProvider(boolean transactionsHandled)
      Specifies whether the entity provider should handle transactions itself or whether they should be handled outside (e.g. if declarative transactions are used).
      Parameters:
      transactionsHandled - true to handle the transactions internally, false to rely on external transaction handling.
    • isTransactionsHandledByProvider

      public boolean isTransactionsHandledByProvider()
      Returns whether the entity provider is handling transactions internally (the default) or relies on external transaction handling.
      Returns:
      true if transactions are handled internally, false if not.
    • runInTransaction

      protected void runInTransaction(Runnable operation)
      If isTransactionsHandledByProvider() is true, operation will be executed inside a transaction that is commited after the operation is completed. Otherwise, operation will just be executed.
      Parameters:
      operation - the operation to run (must not be null).
    • addEntity

      public T addEntity(T entity)
      Description copied from interface: MutableEntityProvider
      Adds entity to the persistence storage. This method returns the same entity after adding to make it possible for the client to access the entity identifier. Note, however, that depending on the implementation of the entity provider and the state of EntityProvider.isEntitiesDetached(), this may or may not be the same instance as entity. Therefore, if EntityProvider.isEntitiesDetached() is true, clients should always assume that entity != returnedEntity.
      Specified by:
      addEntity in interface MutableEntityProvider<T>
      Parameters:
      entity - the entity to add (must not be null).
      Returns:
      the added entity.
    • removeEntity

      public void removeEntity(Object entityId)
      Description copied from interface: MutableEntityProvider
      Removes the entity identified by entityId. If no entity is found, nothing happens.
      Specified by:
      removeEntity in interface MutableEntityProvider<T>
      Parameters:
      entityId - the identifier of the entity to remove.
    • updateEntity

      public T updateEntity(T entity)
      Description copied from interface: MutableEntityProvider
      Saves the changes made to entity to the persistence storage. This method returns the same entity after saving the changes. Note, however, that depending on the implementation of the entity provider and the state of EntityProvider.isEntitiesDetached(), this may or may not be the same instance as entity. Therefore, if EntityProvider.isEntitiesDetached() is true, clients should always assume that entity != returnedEntity.
      Specified by:
      updateEntity in interface MutableEntityProvider<T>
      Parameters:
      entity - the entity to update (must not be null).
      Returns:
      the updated entity.
    • updateEntityProperty

      public void updateEntityProperty(Object entityId, String propertyName, Object propertyValue) throws IllegalArgumentException
      Description copied from interface: MutableEntityProvider
      Updates a single property value of a specific entity. If the entity is not found, nothing happens.
      Specified by:
      updateEntityProperty in interface MutableEntityProvider<T>
      Parameters:
      entityId - the identifier of the entity (must not be null).
      propertyName - the name of the property to update (must not be null).
      propertyValue - the new property value.
      Throws:
      IllegalArgumentException - if propertyName is not a valid property name.
    • addListener

      public void addListener(EntityProviderChangeListener<T> listener)
      Description copied from interface: EntityProviderChangeNotifier
      Registers listener to be notified of EntityProviderChangeEvents.
      Specified by:
      addListener in interface EntityProviderChangeNotifier<T>
      Parameters:
      listener - the listener to register (must not be null).
    • removeListener

      public void removeListener(EntityProviderChangeListener<T> listener)
      Description copied from interface: EntityProviderChangeNotifier
      Removes the previously registered listener.
      Specified by:
      removeListener in interface EntityProviderChangeNotifier<T>
      Parameters:
      listener - the listener to remove (must not be null).
    • setFireEntityProviderChangeEvents

      protected void setFireEntityProviderChangeEvents(boolean fireEvents)
      Sets whether EntityProviderChangeEvents should be fired by this entity provider.
    • isFireEntityProviderChangeEvent

      protected boolean isFireEntityProviderChangeEvent()
      Returns whether EntityProviderChangeEvents should be fired by this entity provider.
    • fireEntityProviderChangeEvent

      protected void fireEntityProviderChangeEvent(EntityProviderChangeEvent<T> event)
      Sends event to all registered listeners if isFireEntityProviderChangeEvent() is true.
      Parameters:
      event - the event to send (must not be null).