org.axonframework.domain
Class AbstractJpaAggregateRoot

java.lang.Object
  extended by org.axonframework.domain.AbstractJpaAggregateRoot
All Implemented Interfaces:
AggregateRoot

public abstract class AbstractJpaAggregateRoot
extends Object
implements AggregateRoot

Implementation of the AggregateRoot interface that allows an aggregate to be stored by a JPA EntityManager. It provides the mechanism to keep track of uncommitted events and maintains a version number based on the number of events generated.

The version number is automatically increased by 1 each time an aggregate is stored. Note that since there is no guarantee that a relationally stored aggregate has an event for each state change, the version of this type of aggregate does not have to be equal to the sequence number of the last Domain Event.

This entity stores the Aggregate Identifier as a String and the last event sequence number and version as a Long. The Aggregate Identifier is assigned as the primary key.

Since:
0.6
Author:
Allard Buijze

Constructor Summary
protected AbstractJpaAggregateRoot()
          Initializes the aggregate root using a random aggregate identifier.
protected AbstractJpaAggregateRoot(AggregateIdentifier identifier)
          Initializes the aggregate root using the provided aggregate identifier.
 
Method Summary
 void commitEvents()
          Clears the events currently marked as "uncommitted".
 AggregateIdentifier getIdentifier()
          Returns the identifier of this aggregate.
 Long getLastEventSequenceNumber()
          Returns the sequence number of the last event registered by this Aggregate.
 int getUncommittedEventCount()
          Returns the number of uncommitted events currently available in the aggregate.
 DomainEventStream getUncommittedEvents()
          Returns a DomainEventStream to the events in the aggregate that have been raised since creation or the last commit.
 Long getVersion()
          Returns the current version number of the aggregate, or null if the aggregate is newly created.
protected  void initializeEventContainer()
          Initializes the EventContainer, which keeps track of uncommitted events in this aggregate.
protected  void registerEvent(DomainEvent event)
          Registers an event to be published when the aggregate is saved.
protected  void updateLastEventSequenceNumber()
          Updates the last event sequence number (see getLastEventSequenceNumber() to the sequence number of the last uncommitted event.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractJpaAggregateRoot

protected AbstractJpaAggregateRoot()
Initializes the aggregate root using a random aggregate identifier.


AbstractJpaAggregateRoot

protected AbstractJpaAggregateRoot(AggregateIdentifier identifier)
Initializes the aggregate root using the provided aggregate identifier.

Parameters:
identifier - the identifier of this aggregate
Method Detail

initializeEventContainer

protected final void initializeEventContainer()
Initializes the EventContainer, which keeps track of uncommitted events in this aggregate.

This method is annotated with @PostLoad and is expected to be invoked by the persistence framework after it has been loaded.


updateLastEventSequenceNumber

protected void updateLastEventSequenceNumber()
Updates the last event sequence number (see getLastEventSequenceNumber() to the sequence number of the last uncommitted event.

This method is annotated with @PreUpdate and @PrePersist and is expected to be called by the persistence framework prior to Update or Persist.


registerEvent

protected void registerEvent(DomainEvent event)
Registers an event to be published when the aggregate is saved.

Parameters:
event - the event to register

getUncommittedEvents

public DomainEventStream getUncommittedEvents()
Returns a DomainEventStream to the events in the aggregate that have been raised since creation or the last commit.

Specified by:
getUncommittedEvents in interface AggregateRoot
Returns:
the DomainEventStream to the uncommitted events.

getIdentifier

public AggregateIdentifier getIdentifier()
Returns the identifier of this aggregate.

Specified by:
getIdentifier in interface AggregateRoot
Returns:
the identifier of this aggregate

commitEvents

public void commitEvents()
Clears the events currently marked as "uncommitted".

Specified by:
commitEvents in interface AggregateRoot

getUncommittedEventCount

public int getUncommittedEventCount()
Returns the number of uncommitted events currently available in the aggregate.

Specified by:
getUncommittedEventCount in interface AggregateRoot
Returns:
the number of uncommitted events currently available in the aggregate.

getLastEventSequenceNumber

public Long getLastEventSequenceNumber()
Returns the sequence number of the last event registered by this Aggregate.

Returns:
the sequence number of the last event registered by this Aggregate.

getVersion

public Long getVersion()
Returns the current version number of the aggregate, or null if the aggregate is newly created. This version must reflect the version number of the aggregate on which changes are applied.

Each time the aggregate is modified and stored in a repository, the version number must be increased by at least 1. This version number can be used by optimistic locking strategies and detection of conflicting concurrent modification.

Typically the sequence number of the last committed event on this aggregate is used as version number.

Specified by:
getVersion in interface AggregateRoot
Returns:
the current version number of this aggregate, or null if no events were ever committed


Copyright © 2011. All Rights Reserved.