Class AbstractUnitOfWork

java.lang.Object
cz.cvut.kbss.jopa.sessions.AbstractSession
cz.cvut.kbss.jopa.sessions.AbstractUnitOfWork
All Implemented Interfaces:
ConfigurationHolder, MetamodelProvider, UnitOfWork, Wrapper
Direct Known Subclasses:
ChangeTrackingUnitOfWork, OnCommitChangePropagatingUnitOfWork, ReadOnlyUnitOfWork

public abstract class AbstractUnitOfWork extends AbstractSession implements UnitOfWork
  • Field Details

    • LOG

      protected static final org.slf4j.Logger LOG
  • Constructor Details

  • Method Details

    • acquireConnection

      protected ConnectionWrapper acquireConnection()
      Description copied from class: AbstractSession
      Acquires connection to the underlying ontology storage.
      Specified by:
      acquireConnection in class AbstractSession
      Returns:
      Connection
    • release

      public void release()
      Description copied from interface: UnitOfWork
      Releases this unit of work.

      Releasing an active Unit of Work with uncommitted changes causes all pending changes to be discarded.

      Specified by:
      release in interface UnitOfWork
    • clear

      public void clear()
      Description copied from interface: UnitOfWork
      Clears this Unit of Work.
      Specified by:
      clear in interface UnitOfWork
    • getLiveObjectCache

      public CacheManager getLiveObjectCache()
      Description copied from class: AbstractSession
      Get the current live object cache.

      This manager represents the second level cache.

      Specified by:
      getLiveObjectCache in class AbstractSession
      Returns:
      Second level cache manager
    • isActive

      public boolean isActive()
      Description copied from interface: UnitOfWork
      Is this Unit of Work active?
      Specified by:
      isActive in interface UnitOfWork
      Returns:
      boolean
    • begin

      public void begin()
      Description copied from interface: UnitOfWork
      Notifies this Unit of Work that a transaction has begun.
      Specified by:
      begin in interface UnitOfWork
    • commit

      public void commit()
      Description copied from interface: UnitOfWork
      Commit changes to the repository.
      Specified by:
      commit in interface UnitOfWork
    • rollback

      public void rollback()
      Description copied from interface: UnitOfWork
      Rolls back changes done since last commit.
      Specified by:
      rollback in interface UnitOfWork
      See Also:
    • contains

      public boolean contains(Object entity)
      Description copied from interface: UnitOfWork
      Checks whether the specified entity is managed in this Unit of Work.
      Specified by:
      contains in interface UnitOfWork
      Parameters:
      entity - Entity to check
      Returns:
      true if entity is managed, false otherwise
    • readObject

      public <T> T readObject(Class<T> cls, Object identifier, Descriptor descriptor)
      Description copied from interface: UnitOfWork
      Retrieves object with the specified identifier.

      The object as well as its fields are looked for in contexts specified by the descriptor. The result is then cast to the specified type.

      Specified by:
      readObject in interface UnitOfWork
      Parameters:
      cls - The type of the returned object
      identifier - Instance identifier
      descriptor - Entity descriptor
      Returns:
      The retrieved object or null if there is no object with the specified identifier in the specified repository
    • readObjectInternal

      protected <T> T readObjectInternal(Class<T> cls, Object identifier, Descriptor descriptor)
    • isInRepository

      protected boolean isInRepository(Descriptor descriptor, Object entity)
    • getReference

      public <T> T getReference(Class<T> cls, Object identifier, Descriptor descriptor)
      Description copied from interface: UnitOfWork
      Retrieves a reference to an object with the specified identifier.

      A reference is permitted to have its state fetched lazily.

      Specified by:
      getReference in interface UnitOfWork
      Type Parameters:
      T - Entity type
      Parameters:
      cls - The type of the returned object
      identifier - Instance identifier
      descriptor - Entity descriptor
      Returns:
      The retrieved object or null if none can be found
    • readObjectWithoutRegistration

      public <T> T readObjectWithoutRegistration(Class<T> cls, Object identifier, Descriptor descriptor)
      Description copied from interface: UnitOfWork
      Reads an object but does not register it with this persistence context.

      Useful when the caller knows the object will be registered eventually by another routine.

      Specified by:
      readObjectWithoutRegistration in interface UnitOfWork
      Parameters:
      cls - Expected result class
      identifier - Object identifier
      descriptor - Entity descriptor
      Returns:
      The retrieved object or null if there is no object with the specified identifier in the specified repository
    • getState

      public EntityState getState(Object entity)
      Description copied from interface: UnitOfWork
      Gets the lifecycle state of the specified entity.

      Note that since no repository is specified we can only determine if the entity is managed or removed. Therefore, if the case is different this method returns EntityState.NOT_MANAGED.

      Specified by:
      getState in interface UnitOfWork
      Parameters:
      entity - Entity whose state to resolve
      Returns:
      Entity state
    • getState

      public EntityState getState(Object entity, Descriptor descriptor)
      Description copied from interface: UnitOfWork
      Gets the lifecycle state of the specified entity with respect to a repository context indicated by the specified descriptor.
      Specified by:
      getState in interface UnitOfWork
      Parameters:
      entity - Entity whose state to resolve
      descriptor - Descriptor of repository contexts
      Returns:
      Entity state
    • isObjectNew

      public boolean isObjectNew(Object entity)
      Description copied from interface: UnitOfWork
      Checks whether the specified entity has been registered in this Unit of Work as a new object for persist.
      Specified by:
      isObjectNew in interface UnitOfWork
      Parameters:
      entity - Object to check
      Returns:
      true when entity is managed and new, false otherwise
      See Also:
    • isObjectManaged

      public boolean isObjectManaged(Object entity)
      Description copied from interface: UnitOfWork
      Return true if the given entity is managed.

      This means it is tracked by this persistence context either as a new object or an existing object loaded from the repository.

      Specified by:
      isObjectManaged in interface UnitOfWork
      Parameters:
      entity - Object to check
      Returns:
      true when the entity is managed, false otherwise
    • mergeDetached

      public <T> T mergeDetached(T entity, Descriptor descriptor)
      Description copied from interface: UnitOfWork
      Merges the state of the given entity into the current persistence context.

      The descriptor argument specified the ontology contexts into which the detached entity and its fields belong and should be merged.

      Specified by:
      mergeDetached in interface UnitOfWork
      Parameters:
      entity - entity instance
      descriptor - Entity descriptor, specifies repository context
      Returns:
      the managed instance that the state was merged to
    • registerExistingObject

      public Object registerExistingObject(Object entity, Descriptor descriptor)
      Description copied from interface: UnitOfWork
      Register an existing object in this Unit of Work.

      This is a shortcut for UnitOfWork.registerExistingObject(Object, CloneRegistrationDescriptor).

      Specified by:
      registerExistingObject in interface UnitOfWork
      Parameters:
      entity - Object
      descriptor - Entity descriptor identifying repository contexts
      Returns:
      Registered clone of the specified object
      See Also:
    • registerExistingObject

      public Object registerExistingObject(Object entity, CloneRegistrationDescriptor registrationDescriptor)
      Description copied from interface: UnitOfWork
      Register an existing object in this Unit of Work.

      Creates a working clone of the specified object according to the configuration and puts the given object into this Unit of Work cache.

      Specified by:
      registerExistingObject in interface UnitOfWork
      Parameters:
      entity - Object
      registrationDescriptor - Configuration of the registration
      Returns:
      Registered clone of the specified object
    • entityType

      protected <T> IdentifiableEntityType<T> entityType(Class<T> cls)
    • getOriginal

      public Object getOriginal(Object clone)
      Tries to find the original object for the given clone. It searches the existing objects, new objects and deleted objects.
      Parameters:
      clone - Object
      Returns:
      The original object for the given clone
    • registerOriginalForNewClone

      public void registerOriginalForNewClone(Object clone, Object original)
      Registers the specified original for the specified clone, assuming the clone is a new object.

      This method must be called during commit when new objects are persisted so that they can be referenced by other objects.

      Parameters:
      clone - Already registered clone
      original - Original to register
    • getManagedOriginal

      public <T> T getManagedOriginal(Class<T> cls, Object identifier, Descriptor descriptor)
      Gets managed original with the specified identifier or null if there is none matching.

      Descriptor is used to check repository context validity.

      Parameters:
      cls - Return type of the original
      identifier - Instance identifier
      descriptor - Repository descriptor
      Returns:
      Original object managed by this UoW or null if this UoW doesn't contain a matching instance
    • getCloneForOriginal

      public Object getCloneForOriginal(Object original)
      Finds clone of the specified original.
      Specified by:
      getCloneForOriginal in interface UnitOfWork
      Parameters:
      original - The original object whose clone we are looking for
      Returns:
      The clone or null, if there is none
    • hasChanges

      public boolean hasChanges()
    • processInferredValueChanges

      protected ObjectChangeSet processInferredValueChanges(ObjectChangeSet changeSet)
    • getInstanceForMerge

      protected <T> T getInstanceForMerge(URI identifier, EntityType<T> et, Descriptor descriptor)
    • evictAfterMerge

      protected void evictAfterMerge(EntityType<?> et, URI identifier, Descriptor descriptor)
    • copyChangeSet

      protected static ObjectChangeSet copyChangeSet(ObjectChangeSet changeSet, Object original, Object clone, Descriptor descriptor)
    • refreshObject

      public <T> void refreshObject(T object)
      Description copied from interface: UnitOfWork
      Refreshes the state of the specified object from the repository, overwriting any changes made to it.
      Specified by:
      refreshObject in interface UnitOfWork
      Type Parameters:
      T - Object type
      Parameters:
      object - The object to revert
    • registerNewObject

      public void registerNewObject(Object entity, Descriptor descriptor)
      Description copied from interface: UnitOfWork
      Registers the specified new object in this Unit of Work.

      The object will be persisted into the context specified by descriptor.

      Specified by:
      registerNewObject in interface UnitOfWork
      Parameters:
      entity - The object to register
      descriptor - Entity descriptor
    • restoreRemovedObject

      public void restoreRemovedObject(Object entity)
      Description copied from interface: UnitOfWork
      Restores the specified removed object.

      This means it is reinstated as a managed entity and reinserted into the repository.

      Specified by:
      restoreRemovedObject in interface UnitOfWork
      Parameters:
      entity - The object to restore
    • unregisterObject

      public void unregisterObject(Object object)
      Description copied from interface: UnitOfWork
      Detaches the specified registered object from this Unit of Work.
      Specified by:
      unregisterObject in interface UnitOfWork
      Parameters:
      object - Clone to detach
    • unregisterEntityFromOntologyContext

      protected void unregisterEntityFromOntologyContext(Object entity)
    • removeIndirectWrappersAndProxies

      protected void removeIndirectWrappersAndProxies(Object entity)
      Removes IndirectWrapper and LazyLoadingProxy instances from the specified entity (if present).
      Parameters:
      entity - The entity to remove indirect wrappers from
    • writeUncommittedChanges

      public void writeUncommittedChanges()
      Description copied from interface: UnitOfWork
      Writes any uncommitted changes into the ontology.
      Specified by:
      writeUncommittedChanges in interface UnitOfWork
    • getMetamodel

      public MetamodelImpl getMetamodel()
      Description copied from interface: MetamodelProvider
      Gets the metamodel
      Specified by:
      getMetamodel in interface MetamodelProvider
      Returns:
      Metamodel
    • isEntityType

      public boolean isEntityType(Class<?> cls)
      Description copied from interface: MetamodelProvider
      Checks whether the specified class is an entity type.
      Specified by:
      isEntityType in interface MetamodelProvider
      Parameters:
      cls - The class to check
      Returns:
      Whether type is managed
    • isInTransaction

      public boolean isInTransaction()
      Description copied from interface: UnitOfWork
      Returns true if this UnitOfWork represents the persistence context of a currently running transaction.
      Specified by:
      isInTransaction in interface UnitOfWork
      Returns:
      True if in an active transaction
    • isFlushingChanges

      public boolean isFlushingChanges()
      Description copied from interface: UnitOfWork
      Returns true if this Unit of Work is currently flushing changes to the repository.
      Specified by:
      isFlushingChanges in interface UnitOfWork
      Returns:
      true if the UoW is flushing changes, false otherwise
    • loadEntityField

      public <T> Object loadEntityField(T entity, FieldSpecification<? super T,?> fieldSpec)
      Description copied from interface: UnitOfWork
      Loads value of the specified field for the specified entity.

      The value is set on the entity.

      Specified by:
      loadEntityField in interface UnitOfWork
      Parameters:
      entity - The entity to load field for
      fieldSpec - Metamodel element representing the field to load
      Returns:
      The loaded field value
    • stringify

      public String stringify(Object object)
      Gets basic object info for logging.

      This works around using Object.toString() for entities, which could inadvertently trigger lazy field fetching and cause an infinite field loading loop.

      Parameters:
      object - Object to stringify
      Returns:
      String info about the specified object
    • getFieldDescriptor

      protected <T> Descriptor getFieldDescriptor(T entity, Field field, Descriptor entityDescriptor)
    • putObjectIntoCache

      public void putObjectIntoCache(Object identifier, Object entity, Descriptor descriptor)
      Description copied from interface: UnitOfWork
      Puts the specified object into the live object cache.
      Specified by:
      putObjectIntoCache in interface UnitOfWork
      Parameters:
      identifier - Object identifier
      entity - Object to cache
      descriptor - Descriptor of repository context
    • removeObjectFromCache

      public void removeObjectFromCache(Object toRemove, URI context)
      Description copied from interface: UnitOfWork
      Removes the specified object from the live object cache.

      This is particularly meant for merging deleted objects from transactions.

      Specified by:
      removeObjectFromCache in interface UnitOfWork
      Parameters:
      toRemove - Object to remove from cache
      context - Entity context URI
    • isConsistent

      public boolean isConsistent(URI context)
      Description copied from interface: UnitOfWork
      Checks whether the specified repository context is consistent.
      Specified by:
      isConsistent in interface UnitOfWork
      Parameters:
      context - Context URI, null indicates the whole repository should be checked
      Returns:
      true if the context is consistent, false otherwise
    • getContexts

      public List<URI> getContexts()
      Description copied from interface: UnitOfWork
      Gets repository contexts available to this session.
      Specified by:
      getContexts in interface UnitOfWork
      Returns:
      Unmodifiable list of context URIs
    • isInferred

      public <T> boolean isInferred(T entity, FieldSpecification<? super T,?> attribute, Object value)
      Description copied from interface: UnitOfWork
      Checks whether the specified attribute value of the specified entity is inferred in the underlying repository.

      Note that given the nature of the repository implementation, this method may return true if the corresponding statement is both inferred and asserted. Also note that this method will use the descriptor associated with the specified entity in this persistence context to resolve the repository context, but some underlying repositories do not store inferences in data contexts, so the attribute context may be ignored.

      Specified by:
      isInferred in interface UnitOfWork
      Parameters:
      entity - Entity whose attribute to examine. Must be managed by this persistence context
      attribute - Attribute whose value to examine
      value - The value whose inference to examine
      Returns:
      true if the entity attribute value is inferred, false otherwise
    • isLoaded

      public LoadState isLoaded(Object entity, String attributeName)
      Description copied from interface: UnitOfWork
      Gets the load status of the specified attribute on the specified entity.
      Specified by:
      isLoaded in interface UnitOfWork
      Parameters:
      entity - Entity instance
      attributeName - Attribute whose load status is to be determined
      Returns:
      Attribute load status
      See Also:
    • isLoaded

      public LoadState isLoaded(Object entity)
      Description copied from interface: UnitOfWork
      Gets the load status of the specified entity.
      Specified by:
      isLoaded in interface UnitOfWork
      Parameters:
      entity - Entity whose load status is to be determined.
      Returns:
      Entity load status
      See Also:
    • sparqlQueryFactory

      public SparqlQueryFactory sparqlQueryFactory()
      Description copied from interface: UnitOfWork
      Gets a SparqlQueryFactory instance associated with this persistence context.
      Specified by:
      sparqlQueryFactory in interface UnitOfWork
      Returns:
      SPARQL query factory
    • getCriteriaBuilder

      public CriteriaBuilder getCriteriaBuilder()
      Description copied from class: AbstractSession
      Gets a CriteriaBuilder instance for building Criteria API queries.
      Specified by:
      getCriteriaBuilder in interface UnitOfWork
      Specified by:
      getCriteriaBuilder in class AbstractSession
      Returns:
      Criteria query builder
    • getLoadStateRegistry

      public LoadStateDescriptorRegistry getLoadStateRegistry()
      Description copied from interface: UnitOfWork
      Gets the registry of entity load state descriptors.
      Specified by:
      getLoadStateRegistry in interface UnitOfWork
      Returns:
      LoadStateDescriptorRegistry for this persistence context
    • unwrap

      public <T> T unwrap(Class<T> cls)
      Description copied from interface: Wrapper
      Unwraps implementation of the specified class.
      Specified by:
      unwrap in interface Wrapper
      Parameters:
      cls - The class of the object to be returned
      Returns:
      An instance of the specified class
    • markCloneForDeletion

      protected void markCloneForDeletion(Object entity, Object identifier)