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

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>

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

Generic repository implementation that stores JPA annotated aggregates. These aggregates must implement AbstractJpaAggregateRoot.

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(Class<T> aggregateType)
          Initialize a repository for storing aggregates of the given aggregateType.
GenericJpaRepository(Class<T> aggregateType, LockingStrategy lockingStrategy)
          Initialize a repository for storing aggregates of the given aggregateType with an additional lockingStrategy.
 
Method Summary
protected  T doLoad(AggregateIdentifier aggregateIdentifier, Long expectedVersion)
          Perform the actual loading of an aggregate.
protected  void doSaveWithLock(T aggregate)
          Perform the actual saving of the aggregate.
 void setEntityManager(javax.persistence.EntityManager entityManager)
          Sets the EntityManager this repository should use to access the underlying storage.
 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
doSave, load
 
Methods inherited from class org.axonframework.repository.AbstractRepository
add, load, 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(Class<T> aggregateType)
Initialize a repository for storing aggregates of the given aggregateType. No additional locking will be used.

Parameters:
aggregateType - the aggregate type this repository manages

GenericJpaRepository

public GenericJpaRepository(Class<T> aggregateType,
                            LockingStrategy lockingStrategy)
Initialize a repository for storing aggregates of the given aggregateType with an additional lockingStrategy.

Parameters:
aggregateType - the aggregate type this repository manages
lockingStrategy - 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 AbstractJpaAggregateRoot>
Parameters:
aggregate - the aggregate to store

doLoad

protected T doLoad(AggregateIdentifier 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 AbstractJpaAggregateRoot>
Parameters:
aggregateIdentifier - the identifier of the aggregate to load
expectedVersion - The expected version of the aggregate
Returns:
the fully initialized aggregate

setEntityManager

public void setEntityManager(javax.persistence.EntityManager entityManager)
Sets the EntityManager this repository should use to access the underlying storage.

Parameters:
entityManager - the EntityManager providing access to the underlying storage.

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 © 2011. All Rights Reserved.