Class GenericJpaRepository<T>
- Type Parameters:
T- The type of aggregate the repository provides access to
- All Implemented Interfaces:
ScopeAware,Repository<T>
By default, this repository implementation will generate sequences for the events published by the aggregate. In
doing so, the user is capable of querying the events based on the aggregate identifier and order them based on the
aforementioned sequence number. The downside of this, is that even if the stored aggregate is removed, the aggregate
identifier cannot be reused. This follows from the uniqueness constraint on the Event Store defining that the
combination of aggregate identifier to sequence number should be unique. However, sequence number generation can be
disabled, through GenericJpaRepository.Builder.disableSequenceNumberGeneration(). When disabled, published events will no
longer hold the sequence number nor the aggregate identifier. As such, the aggregate identifier can be reused
(after removal of the previous aggregate referring to that identifier). The obvious downside of this is that the
events in the store can no longer be queried based on the aggregate identifier.
- Since:
- 0.7
- Author:
- Allard Buijze
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder class to instantiate aGenericJpaRepositoryfor aggregate typeT. -
Field Summary
Fields inherited from class org.axonframework.modelling.command.AbstractRepository
spanFactory -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedInstantiate aGenericJpaRepositorybased on the fields contained in theGenericJpaRepository.Builder. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> GenericJpaRepository.Builder<T> Instantiate a Builder to be able to create aGenericJpaRepositoryfor aggregate typeT.protected AnnotatedAggregate<T> doCreateNewForLock(Callable<T> factoryMethod) Creates a new aggregate instance using the givenfactoryMethod.protected voiddoDeleteWithLock(AnnotatedAggregate<T> aggregate) Perform the actual deleting of the aggregate.protected AnnotatedAggregate<T> doLoadWithLock(String aggregateIdentifier, Long expectedVersion) Loads the aggregate with the given aggregateIdentifier.protected voiddoSaveWithLock(AnnotatedAggregate<T> aggregate) Perform the actual saving of the aggregate.voidsetForceFlushOnSave(boolean forceFlushOnSave) Indicates whether the EntityManager's state should be flushed each time an aggregate is saved.Methods inherited from class org.axonframework.modelling.command.LockingRepository
doCreateNew, doDelete, doLoad, doLoadOrCreate, doSave, prepareForCommitMethods inherited from class org.axonframework.modelling.command.AbstractRepository
aggregateModel, canResolve, getAggregateType, load, load, loadOrCreate, managedAggregates, newInstance, newInstance, postDelete, postSave, reportIllegalState, send, validateOnLoad
-
Constructor Details
-
GenericJpaRepository
Instantiate aGenericJpaRepositorybased on the fields contained in theGenericJpaRepository.Builder.A goal of the provided Builder is to create an
AggregateModelspecifying genericTas the aggregate type to be stored. All aggregates in this repository must beinstanceOfthis aggregate type. To instantiate this AggregateModel, either anAggregateModelcan be provided directly or anaggregateTypeof typeClasscan be used. The latter will internally resolve to an AggregateModel. Thus, either the AggregateModel or theaggregateTypeshould be provided. AnAxonConfigurationExceptionis thrown if this criteria is not met.Additionally will assert that the
LockFactory,EntityManagerProvider,EventBusandidentifierConverterare notnull, resulting in an AxonConfigurationException if for any of these this is the case.- Parameters:
builder- theGenericJpaRepository.Builderused to instantiate aGenericJpaRepositoryinstance
-
-
Method Details
-
builder
Instantiate a Builder to be able to create aGenericJpaRepositoryfor aggregate typeT.The
LockFactoryis defaulted to anNullLockFactory, thus providing no additional locking, theidentifierConvertertoFunction.identity(), theRepositorySpanFactoryis defaulted to aDefaultRepositorySpanFactorybacked by aNoOpSpanFactory, and sequence number generation is enabled.A goal of this Builder goal is to create an
AggregateModelspecifying genericTas the aggregate type to be stored. All aggregates in this repository must beinstanceOfthis aggregate type. To instantiate this AggregateModel, either anAggregateModelcan be provided directly or anaggregateTypeof typeClasscan be used. The latter will internally resolve to an AggregateModel. Thus, either the AggregateModel or theaggregateTypeshould be provided.Additionally, the
EntityManagerProviderandEventBusare hard requirements and as such should be provided.- Type Parameters:
T- The type of aggregate to build the repository for- Parameters:
aggregateType- The type of aggregate to build the repository for- Returns:
- a Builder to be able to create a
GenericJpaRepository
-
doLoadWithLock
Description copied from class:LockingRepositoryLoads the aggregate with the given aggregateIdentifier. All necessary locks have been obtained.- Specified by:
doLoadWithLockin classLockingRepository<T,AnnotatedAggregate<T>> - Parameters:
aggregateIdentifier- the identifier of the aggregate to loadexpectedVersion- The expected version of the aggregate to load- Returns:
- a fully initialized aggregate
-
doCreateNewForLock
Description copied from class:LockingRepositoryCreates a new aggregate instance using the givenfactoryMethod. Implementations should assume that this method is only called if a UnitOfWork is currently active.- Specified by:
doCreateNewForLockin classLockingRepository<T,AnnotatedAggregate<T>> - Parameters:
factoryMethod- The method to create the aggregate's root instance- Returns:
- an Aggregate instance describing the aggregate's state
- Throws:
Exception- when the factoryMethod throws an exception
-
doSaveWithLock
Description copied from class:LockingRepositoryPerform the actual saving of the aggregate. All necessary locks have been verified.- Specified by:
doSaveWithLockin classLockingRepository<T,AnnotatedAggregate<T>> - Parameters:
aggregate- the aggregate to store
-
doDeleteWithLock
Description copied from class:LockingRepositoryPerform the actual deleting of the aggregate. All necessary locks have been verified.- Specified by:
doDeleteWithLockin classLockingRepository<T,AnnotatedAggregate<T>> - Parameters:
aggregate- the aggregate to delete
-
setForceFlushOnSave
public void setForceFlushOnSave(boolean forceFlushOnSave) Indicates whether the EntityManager's state should be flushed each time an aggregate is saved. Defaults totrue. Flushing the EntityManager will force JPA to send state changes to the database. Any key violations and failing optimistic locks will be identified in an early stage.- Parameters:
forceFlushOnSave- whether or not to flush the EntityManager after each save. Defaults totrue.- See Also:
-