org.axonframework.repository
Class GenericJpaRepository<T extends AggregateRoot>

java.lang.Object
  extended by org.axonframework.repository.AbstractRepository<T>
      extended by org.axonframework.repository.LockingRepository<T>
          extended by org.axonframework.repository.GenericJpaRepository<T>
Type Parameters:
T - The type of aggregate the repository provides access to
All Implemented Interfaces:
Repository<T>
Direct Known Subclasses:
HybridJpaRepository

public class GenericJpaRepository<T extends AggregateRoot>
extends LockingRepository<T>

Generic repository implementation that stores JPA annotated aggregates. These aggregates must implement AggregateRoot and have the proper JPA Annotations.

Optionally, the repository may be configured with a locking scheme. The repository will always force optimistic locking in the backing data store. The optional lock in the repository is in addition to this optimistic lock. Note that locks on this repository will not be shared with other repository instances.

When this repository is requested to persist changes to an aggregate, it will also flush the EntityManager, to enforce checking of database constraints and optimistic locks.

Since:
0.7
Author:
Allard Buijze

Constructor Summary
GenericJpaRepository(javax.persistence.EntityManager entityManager, Class<T> aggregateType)
          Initialize a repository for storing aggregates of the given aggregateType.
GenericJpaRepository(javax.persistence.EntityManager entityManager, Class<T> aggregateType, LockManager lockManager)
          Initialize a repository for storing aggregates of the given aggregateType with an additional lockManager.
GenericJpaRepository(EntityManagerProvider entityManagerProvider, Class<T> aggregateType)
          Initialize a repository for storing aggregates of the given aggregateType.
GenericJpaRepository(EntityManagerProvider entityManagerProvider, Class<T> aggregateType, LockManager lockManager)
          Initialize a repository for storing aggregates of the given aggregateType with an additional lockManager.
 
Method Summary
protected  void doDeleteWithLock(T aggregate)
          Perform the actual deleting of the aggregate.
protected  T doLoad(Object aggregateIdentifier, Long expectedVersion)
          Perform the actual loading of an aggregate.
protected  void doSaveWithLock(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.
 
Methods inherited from class org.axonframework.repository.LockingRepository
add, doDelete, doSave, load
 
Methods inherited from class org.axonframework.repository.AbstractRepository
getAggregateType, load, postDelete, postSave, setEventBus, validateOnLoad
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GenericJpaRepository

public GenericJpaRepository(EntityManagerProvider entityManagerProvider,
                            Class<T> aggregateType)
Initialize a repository for storing aggregates of the given aggregateType. No additional locking will be used.

Parameters:
entityManagerProvider - The EntityManagerProvider providing the EntityManager instance for this repository
aggregateType - the aggregate type this repository manages

GenericJpaRepository

public GenericJpaRepository(javax.persistence.EntityManager entityManager,
                            Class<T> aggregateType)
Initialize a repository for storing aggregates of the given aggregateType. No additional locking will be used.

Parameters:
entityManager - The EntityManager instance for this repository
aggregateType - the aggregate type this repository manages

GenericJpaRepository

public GenericJpaRepository(EntityManagerProvider entityManagerProvider,
                            Class<T> aggregateType,
                            LockManager lockManager)
Initialize a repository for storing aggregates of the given aggregateType with an additional lockManager.

Parameters:
entityManagerProvider - The EntityManagerProvider providing the EntityManager instance for this repository
aggregateType - the aggregate type this repository manages
lockManager - the additional locking strategy for this repository

GenericJpaRepository

public GenericJpaRepository(javax.persistence.EntityManager entityManager,
                            Class<T> aggregateType,
                            LockManager lockManager)
Initialize a repository for storing aggregates of the given aggregateType with an additional lockManager.

Parameters:
entityManager - The EntityManager instance for this repository
aggregateType - the aggregate type this repository manages
lockManager - the additional locking strategy for this repository
Method Detail

doSaveWithLock

protected void doSaveWithLock(T aggregate)
Description copied from class: LockingRepository
Perform the actual saving of the aggregate. All necessary locks have been verified.

Specified by:
doSaveWithLock in class LockingRepository<T extends AggregateRoot>
Parameters:
aggregate - the aggregate to store

doDeleteWithLock

protected void doDeleteWithLock(T aggregate)
Description copied from class: LockingRepository
Perform the actual deleting of the aggregate. All necessary locks have been verified.

Specified by:
doDeleteWithLock in class LockingRepository<T extends AggregateRoot>
Parameters:
aggregate - the aggregate to delete

doLoad

protected T doLoad(Object aggregateIdentifier,
                   Long expectedVersion)
Description copied from class: LockingRepository
Perform the actual loading of an aggregate. The necessary locks have been obtained.

Specified by:
doLoad in class LockingRepository<T extends AggregateRoot>
Parameters:
aggregateIdentifier - the identifier of the aggregate to load
expectedVersion - The expected version of the aggregate
Returns:
the fully initialized aggregate

setForceFlushOnSave

public void setForceFlushOnSave(boolean forceFlushOnSave)
Indicates whether the EntityManager's state should be flushed each time an aggregate is saved. Defaults to 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.

Parameters:
forceFlushOnSave - whether or not to flush the EntityManager after each save. Defaults to true.
See Also:
EntityManager.flush()


Copyright © 2010-2016. All Rights Reserved.