Class AbstractUnitOfWork
- All Implemented Interfaces:
ConfigurationHolder
,MetamodelProvider
,UnitOfWork
,Wrapper
- Direct Known Subclasses:
ChangeTrackingUnitOfWork
,OnCommitChangePropagatingUnitOfWork
,ReadOnlyUnitOfWork
-
Field Summary
FieldsFields inherited from class cz.cvut.kbss.jopa.sessions.AbstractSession
configuration
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected ConnectionWrapper
Acquires connection to the underlying ontology storage.void
begin()
Notifies this Unit of Work that a transaction has begun.void
clear()
Clears this Unit of Work.void
commit()
Commit changes to the repository.boolean
Checks whether the specified entity is managed in this Unit of Work.protected static ObjectChangeSet
copyChangeSet
(ObjectChangeSet changeSet, Object original, Object clone, Descriptor descriptor) protected <T> IdentifiableEntityType<T>
entityType
(Class<T> cls) protected void
evictAfterMerge
(EntityType<?> et, URI identifier, Descriptor descriptor) getCloneForOriginal
(Object original) Finds clone of the specified original.Gets repository contexts available to this session.Gets aCriteriaBuilder
instance for building Criteria API queries.protected <T> Descriptor
getFieldDescriptor
(T entity, Field field, Descriptor entityDescriptor) protected <T> T
getInstanceForMerge
(URI identifier, EntityType<T> et, Descriptor descriptor) Get the current live object cache.Gets the registry of entity load state descriptors.<T> T
getManagedOriginal
(Class<T> cls, Object identifier, Descriptor descriptor) Gets managed original with the specified identifier ornull
if there is none matching.Gets the metamodelgetOriginal
(Object clone) Tries to find the original object for the given clone.<T> T
getReference
(Class<T> cls, Object identifier, Descriptor descriptor) Retrieves a reference to an object with the specified identifier.Gets the lifecycle state of the specified entity.getState
(Object entity, Descriptor descriptor) Gets the lifecycle state of the specified entity with respect to a repository context indicated by the specified descriptor.boolean
boolean
isActive()
Is this Unit of Work active?boolean
isConsistent
(URI context) Checks whether the specified repository context is consistent.boolean
isEntityType
(Class<?> cls) Checks whether the specified class is an entity type.boolean
Returns true if this Unit of Work is currently flushing changes to the repository.<T> boolean
isInferred
(T entity, FieldSpecification<? super T, ?> attribute, Object value) Checks whether the specified attribute value of the specified entity is inferred in the underlying repository.protected boolean
isInRepository
(Descriptor descriptor, Object entity) boolean
Returns true if thisUnitOfWork
represents the persistence context of a currently running transaction.Gets the load status of the specified entity.Gets the load status of the specified attribute on the specified entity.boolean
isObjectManaged
(Object entity) Return true if the given entity is managed.boolean
isObjectNew
(Object entity) Checks whether the specified entity has been registered in this Unit of Work as a new object for persist.<T> Object
loadEntityField
(T entity, FieldSpecification<? super T, ?> fieldSpec) Loads value of the specified field for the specified entity.protected void
markCloneForDeletion
(Object entity, Object identifier) <T> T
mergeDetached
(T entity, Descriptor descriptor) Merges the state of the given entity into the current persistence context.protected ObjectChangeSet
processInferredValueChanges
(ObjectChangeSet changeSet) void
putObjectIntoCache
(Object identifier, Object entity, Descriptor descriptor) Puts the specified object into the live object cache.<T> T
readObject
(Class<T> cls, Object identifier, Descriptor descriptor) Retrieves object with the specified identifier.protected <T> T
readObjectInternal
(Class<T> cls, Object identifier, Descriptor descriptor) <T> T
readObjectWithoutRegistration
(Class<T> cls, Object identifier, Descriptor descriptor) Reads an object but does not register it with this persistence context.<T> void
refreshObject
(T object) Refreshes the state of the specified object from the repository, overwriting any changes made to it.registerExistingObject
(Object entity, Descriptor descriptor) Register an existing object in this Unit of Work.registerExistingObject
(Object entity, CloneRegistrationDescriptor registrationDescriptor) Register an existing object in this Unit of Work.void
registerNewObject
(Object entity, Descriptor descriptor) Registers the specified new object in this Unit of Work.void
registerOriginalForNewClone
(Object clone, Object original) Registers the specified original for the specified clone, assuming the clone is a new object.void
release()
Releases this unit of work.protected void
RemovesIndirectWrapper
andLazyLoadingProxy
instances from the specified entity (if present).void
removeObjectFromCache
(Object toRemove, URI context) Removes the specified object from the live object cache.void
restoreRemovedObject
(Object entity) Restores the specified removed object.void
rollback()
Rolls back changes done since last commit.Gets aSparqlQueryFactory
instance associated with this persistence context.Gets basic object info for logging.protected void
void
unregisterObject
(Object object) Detaches the specified registered object from this Unit of Work.<T> T
Unwraps implementation of the specified class.void
Writes any uncommitted changes into the ontology.Methods inherited from class cz.cvut.kbss.jopa.sessions.AbstractSession
getConfiguration
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface cz.cvut.kbss.jopa.sessions.ConfigurationHolder
getConfiguration
Methods inherited from interface cz.cvut.kbss.jopa.sessions.MetamodelProvider
getNamedQueryManager, getResultSetMappingManager
Methods inherited from interface cz.cvut.kbss.jopa.sessions.UnitOfWork
attributeChanged, attributeChanged, createIndirectCollection, removeObject
-
Field Details
-
LOG
protected static final org.slf4j.Logger LOG
-
-
Constructor Details
-
AbstractUnitOfWork
-
-
Method Details
-
acquireConnection
Description copied from class:AbstractSession
Acquires connection to the underlying ontology storage.- Specified by:
acquireConnection
in classAbstractSession
- 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 interfaceUnitOfWork
-
clear
public void clear()Description copied from interface:UnitOfWork
Clears this Unit of Work.- Specified by:
clear
in interfaceUnitOfWork
-
getLiveObjectCache
Description copied from class:AbstractSession
Get the current live object cache.This manager represents the second level cache.
- Specified by:
getLiveObjectCache
in classAbstractSession
- Returns:
- Second level cache manager
-
isActive
public boolean isActive()Description copied from interface:UnitOfWork
Is this Unit of Work active?- Specified by:
isActive
in interfaceUnitOfWork
- 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 interfaceUnitOfWork
-
commit
public void commit()Description copied from interface:UnitOfWork
Commit changes to the repository.- Specified by:
commit
in interfaceUnitOfWork
-
rollback
public void rollback()Description copied from interface:UnitOfWork
Rolls back changes done since last commit.- Specified by:
rollback
in interfaceUnitOfWork
- See Also:
-
contains
Description copied from interface:UnitOfWork
Checks whether the specified entity is managed in this Unit of Work.- Specified by:
contains
in interfaceUnitOfWork
- Parameters:
entity
- Entity to check- Returns:
- true if entity is managed, false otherwise
-
readObject
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 interfaceUnitOfWork
- Parameters:
cls
- The type of the returned objectidentifier
- Instance identifierdescriptor
- Entity descriptor- Returns:
- The retrieved object or
null
if there is no object with the specified identifier in the specified repository
-
readObjectInternal
-
isInRepository
-
getReference
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 interfaceUnitOfWork
- Type Parameters:
T
- Entity type- Parameters:
cls
- The type of the returned objectidentifier
- Instance identifierdescriptor
- Entity descriptor- Returns:
- The retrieved object or
null
if none can be found
-
readObjectWithoutRegistration
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 interfaceUnitOfWork
- Parameters:
cls
- Expected result classidentifier
- Object identifierdescriptor
- Entity descriptor- Returns:
- The retrieved object or
null
if there is no object with the specified identifier in the specified repository
-
getState
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 interfaceUnitOfWork
- Parameters:
entity
- Entity whose state to resolve- Returns:
- Entity state
-
getState
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 interfaceUnitOfWork
- Parameters:
entity
- Entity whose state to resolvedescriptor
- Descriptor of repository contexts- Returns:
- Entity state
-
isObjectNew
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 interfaceUnitOfWork
- Parameters:
entity
- Object to check- Returns:
true
when entity is managed and new,false
otherwise- See Also:
-
isObjectManaged
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 interfaceUnitOfWork
- Parameters:
entity
- Object to check- Returns:
true
when the entity is managed,false
otherwise
-
mergeDetached
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 interfaceUnitOfWork
- Parameters:
entity
- entity instancedescriptor
- Entity descriptor, specifies repository context- Returns:
- the managed instance that the state was merged to
-
registerExistingObject
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 interfaceUnitOfWork
- Parameters:
entity
- Objectdescriptor
- 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 interfaceUnitOfWork
- Parameters:
entity
- ObjectregistrationDescriptor
- Configuration of the registration- Returns:
- Registered clone of the specified object
-
entityType
-
getOriginal
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
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 cloneoriginal
- Original to register
-
getManagedOriginal
Gets managed original with the specified identifier ornull
if there is none matching.Descriptor is used to check repository context validity.
- Parameters:
cls
- Return type of the originalidentifier
- Instance identifierdescriptor
- Repository descriptor- Returns:
- Original object managed by this UoW or
null
if this UoW doesn't contain a matching instance
-
getCloneForOriginal
Finds clone of the specified original.- Specified by:
getCloneForOriginal
in interfaceUnitOfWork
- 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
-
getInstanceForMerge
-
evictAfterMerge
-
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 interfaceUnitOfWork
- Type Parameters:
T
- Object type- Parameters:
object
- The object to revert
-
registerNewObject
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 interfaceUnitOfWork
- Parameters:
entity
- The object to registerdescriptor
- Entity descriptor
-
restoreRemovedObject
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 interfaceUnitOfWork
- Parameters:
entity
- The object to restore
-
unregisterObject
Description copied from interface:UnitOfWork
Detaches the specified registered object from this Unit of Work.- Specified by:
unregisterObject
in interfaceUnitOfWork
- Parameters:
object
- Clone to detach
-
unregisterEntityFromOntologyContext
-
removeIndirectWrappersAndProxies
RemovesIndirectWrapper
andLazyLoadingProxy
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 interfaceUnitOfWork
-
getMetamodel
Description copied from interface:MetamodelProvider
Gets the metamodel- Specified by:
getMetamodel
in interfaceMetamodelProvider
- Returns:
- Metamodel
-
isEntityType
Description copied from interface:MetamodelProvider
Checks whether the specified class is an entity type.- Specified by:
isEntityType
in interfaceMetamodelProvider
- Parameters:
cls
- The class to check- Returns:
- Whether type is managed
-
isInTransaction
public boolean isInTransaction()Description copied from interface:UnitOfWork
Returns true if thisUnitOfWork
represents the persistence context of a currently running transaction.- Specified by:
isInTransaction
in interfaceUnitOfWork
- 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 interfaceUnitOfWork
- Returns:
true
if the UoW is flushing changes,false
otherwise
-
loadEntityField
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 interfaceUnitOfWork
- Parameters:
entity
- The entity to load field forfieldSpec
- Metamodel element representing the field to load- Returns:
- The loaded field value
-
stringify
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
-
putObjectIntoCache
Description copied from interface:UnitOfWork
Puts the specified object into the live object cache.- Specified by:
putObjectIntoCache
in interfaceUnitOfWork
- Parameters:
identifier
- Object identifierentity
- Object to cachedescriptor
- Descriptor of repository context
-
removeObjectFromCache
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 interfaceUnitOfWork
- Parameters:
toRemove
- Object to remove from cachecontext
- Entity context URI
-
isConsistent
Description copied from interface:UnitOfWork
Checks whether the specified repository context is consistent.- Specified by:
isConsistent
in interfaceUnitOfWork
- Parameters:
context
- Context URI,null
indicates the whole repository should be checked- Returns:
true
if the context is consistent,false
otherwise
-
getContexts
Description copied from interface:UnitOfWork
Gets repository contexts available to this session.- Specified by:
getContexts
in interfaceUnitOfWork
- Returns:
- Unmodifiable list of context URIs
-
isInferred
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 interfaceUnitOfWork
- Parameters:
entity
- Entity whose attribute to examine. Must be managed by this persistence contextattribute
- Attribute whose value to examinevalue
- The value whose inference to examine- Returns:
true
if the entity attribute value is inferred,false
otherwise
-
isLoaded
Description copied from interface:UnitOfWork
Gets the load status of the specified attribute on the specified entity.- Specified by:
isLoaded
in interfaceUnitOfWork
- Parameters:
entity
- Entity instanceattributeName
- Attribute whose load status is to be determined- Returns:
- Attribute load status
- See Also:
-
isLoaded
Description copied from interface:UnitOfWork
Gets the load status of the specified entity.- Specified by:
isLoaded
in interfaceUnitOfWork
- Parameters:
entity
- Entity whose load status is to be determined.- Returns:
- Entity load status
- See Also:
-
sparqlQueryFactory
Description copied from interface:UnitOfWork
Gets aSparqlQueryFactory
instance associated with this persistence context.- Specified by:
sparqlQueryFactory
in interfaceUnitOfWork
- Returns:
- SPARQL query factory
-
getCriteriaBuilder
Description copied from class:AbstractSession
Gets aCriteriaBuilder
instance for building Criteria API queries.- Specified by:
getCriteriaBuilder
in interfaceUnitOfWork
- Specified by:
getCriteriaBuilder
in classAbstractSession
- Returns:
- Criteria query builder
-
getLoadStateRegistry
Description copied from interface:UnitOfWork
Gets the registry of entity load state descriptors.- Specified by:
getLoadStateRegistry
in interfaceUnitOfWork
- Returns:
LoadStateDescriptorRegistry
for this persistence context
-
unwrap
Description copied from interface:Wrapper
Unwraps implementation of the specified class. -
markCloneForDeletion
-