Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2010-2011. Axon Framework
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.axonframework.repository;
 
 
Generic repository implementation that stores JPA annotated aggregates. These aggregates must implement org.axonframework.domain.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.

Parameters:
<T> The type of aggregate the repository provides access to
Author(s):
Allard Buijze
Since:
0.7
 
 public class GenericJpaRepository<T extends AggregateRootextends LockingRepository<T> {
 
     private EntityManager entityManager;
     private final Class<T> aggregateType;
     private boolean forceFlushOnSave = true;

    
Initialize a repository for storing aggregates of the given aggregateType. No additional locking will be used.

Parameters:
aggregateType the aggregate type this repository manages
 
     public GenericJpaRepository(Class<T> aggregateType) {
         this(aggregateType.);
     }

    
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
 
     public GenericJpaRepository(Class<T> aggregateTypeLockingStrategy lockingStrategy) {
         super(lockingStrategy);
         this. = aggregateType;
     }
 
     @Override
     protected void doSaveWithLock(T aggregate) {
         .persist(aggregate);
         if () {
             .flush();
         }
     }
 
     @Override
     protected void doDeleteWithLock(T aggregate) {
         .remove(aggregate);
         if () {
             .flush();
         }
     }

    
Returns the aggregate type stored by this repository.

Returns:
the aggregate type stored by this repository
 
     protected Class<T> getAggregateType() {
         return ;
     }
 
     @Override
     protected T doLoad(AggregateIdentifier aggregateIdentifierLong expectedVersion) {
         return .find(aggregateIdentifier.asString());
     }

    
Sets the EntityManager this repository should use to access the underlying storage.

Parameters:
entityManager the EntityManager providing access to the underlying storage.
    public void setEntityManager(EntityManager entityManager) {
        this. = entityManager;
    }

    
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:
javax.persistence.EntityManager.flush()
    public void setForceFlushOnSave(boolean forceFlushOnSave) {
        this. = forceFlushOnSave;
    }
New to GrepCode? Check out our FAQ X