T
- The type of aggregate the repository provides access topublic class GenericJpaRepository<T> extends LockingRepository<T,AnnotatedAggregate<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.
Modifier and Type | Class and Description |
---|---|
static class |
GenericJpaRepository.Builder<T>
Builder class to instantiate a
GenericJpaRepository for aggregate type T . |
spanFactory
Modifier | Constructor and Description |
---|---|
protected |
GenericJpaRepository(GenericJpaRepository.Builder<T> builder)
Instantiate a
GenericJpaRepository based on the fields contained in the GenericJpaRepository.Builder . |
Modifier and Type | Method and Description |
---|---|
static <T> GenericJpaRepository.Builder<T> |
builder(Class<T> aggregateType)
Instantiate a Builder to be able to create a
GenericJpaRepository for aggregate type T . |
protected AnnotatedAggregate<T> |
doCreateNewForLock(Callable<T> factoryMethod)
Creates a new aggregate instance using the given
factoryMethod . |
protected void |
doDeleteWithLock(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 void |
doSaveWithLock(AnnotatedAggregate<T> aggregate)
Perform the actual saving of the aggregate.
|
void |
setForceFlushOnSave(boolean forceFlushOnSave)
Indicates whether the EntityManager's state should be flushed each time an aggregate is saved.
|
doCreateNew, doDelete, doLoad, doLoadOrCreate, doSave, prepareForCommit
aggregateModel, canResolve, getAggregateType, load, load, loadOrCreate, managedAggregates, newInstance, newInstance, postDelete, postSave, reportIllegalState, send, validateOnLoad
protected GenericJpaRepository(GenericJpaRepository.Builder<T> builder)
GenericJpaRepository
based on the fields contained in the GenericJpaRepository.Builder
.
A goal of the provided Builder is to create an AggregateModel
specifying generic T
as the
aggregate type to be stored. All aggregates in this repository must be instanceOf
this aggregate type.
To instantiate this AggregateModel, either an AggregateModel
can be provided directly or an
aggregateType
of type Class
can be used. The latter will internally resolve to an
AggregateModel. Thus, either the AggregateModel or the aggregateType
should be provided. An
AxonConfigurationException
is thrown if this criteria is not met.
Additionally will assert that the LockFactory
, EntityManagerProvider
, EventBus
and
identifierConverter
are not null
, resulting in an AxonConfigurationException if for any of these
this is the case.
builder
- the GenericJpaRepository.Builder
used to instantiate a GenericJpaRepository
instancepublic static <T> GenericJpaRepository.Builder<T> builder(Class<T> aggregateType)
GenericJpaRepository
for aggregate type T
.
The LockFactory
is defaulted to an NullLockFactory
, thus providing no additional locking, the
identifierConverter
to Function.identity()
, the RepositorySpanFactory
is defaulted to a
DefaultRepositorySpanFactory
backed by a
NoOpSpanFactory
, and sequence number generation is enabled.
A goal of this Builder goal is to create an AggregateModel
specifying generic T
as the aggregate
type to be stored. All aggregates in this repository must be instanceOf
this aggregate type. To
instantiate this AggregateModel, either an AggregateModel
can be provided directly or an
aggregateType
of type Class
can be used. The latter will internally resolve to an AggregateModel.
Thus, either the AggregateModel or the aggregateType
should be provided.
Additionally, the EntityManagerProvider
and EventBus
are hard requirements and as such
should be provided.
T
- The type of aggregate to build the repository foraggregateType
- The type of aggregate to build the repository forGenericJpaRepository
protected AnnotatedAggregate<T> doLoadWithLock(String aggregateIdentifier, Long expectedVersion)
LockingRepository
doLoadWithLock
in class LockingRepository<T,AnnotatedAggregate<T>>
aggregateIdentifier
- the identifier of the aggregate to loadexpectedVersion
- The expected version of the aggregate to loadprotected AnnotatedAggregate<T> doCreateNewForLock(Callable<T> factoryMethod) throws Exception
LockingRepository
factoryMethod
. Implementations should assume that this
method is only called if a UnitOfWork is currently active.doCreateNewForLock
in class LockingRepository<T,AnnotatedAggregate<T>>
factoryMethod
- The method to create the aggregate's root instanceException
- when the factoryMethod throws an exceptionprotected void doSaveWithLock(AnnotatedAggregate<T> aggregate)
LockingRepository
doSaveWithLock
in class LockingRepository<T,AnnotatedAggregate<T>>
aggregate
- the aggregate to storeprotected void doDeleteWithLock(AnnotatedAggregate<T> aggregate)
LockingRepository
doDeleteWithLock
in class LockingRepository<T,AnnotatedAggregate<T>>
aggregate
- the aggregate to deletepublic void setForceFlushOnSave(boolean forceFlushOnSave)
true
.
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.forceFlushOnSave
- whether or not to flush the EntityManager after each save. Defaults to true
.EntityManager.flush()
Copyright © 2010–2024. All rights reserved.