Skip to content

Commit

Permalink
Reintroduce delete(id) method with better JPA implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
adrienlauer committed Jul 29, 2015
1 parent eaab5a2 commit 405bd3e
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ protected A doLoad(K id) {
return null;
}

@Override
protected void doDelete(K id) {
}

@Override
protected void doDelete(A a) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ protected A doLoad(K id) {
return null;
}

@Override
protected void doDelete(K id) {

}

@Override
protected void doDelete(A a) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ protected A doLoad(K id) {
return null;
}

@Override
protected void doDelete(K id) {
}

@Override
protected void doDelete(A a) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ protected Aggregate doLoad(Key id) {
return (Aggregate) inMemorySortedMap.get(id);
}

@Override
@Deprecated
protected void doDelete(Key id) {
inMemorySortedMap.remove(id);
}

@Override
protected void doDelete(Aggregate aggregate) {
inMemorySortedMap.remove(aggregate.getEntityId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ protected Customer doLoad(String id) {
return orderMap.get(id);
}

@Override
protected void doDelete(String id) {
orderMap.remove(id);
}

@Override
protected void doDelete(Customer order) {
for (Customer order1 : orderMap.values()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ protected Order doLoad(String id) {
return orderMap.get(id);
}

@Override
protected void doDelete(String id) {
orderMap.remove(id);
}

@Override
protected void doDelete(Order order) {
for (Order order1 : orderMap.values()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;


/**
* This class serves as inheritance base for the JPA repositories.
*
* @param <A> JPA Entity Type (DDD: Aggregate)
* @param <K> key type
*
* @author [email protected]
* @author [email protected]
*/
public abstract class BaseJpaRepository<A extends AggregateRoot<K>, K> extends BaseRepository<A, K> {

@Inject
@Inject
protected EntityManager entityManager;

/**
Expand All @@ -41,24 +41,33 @@ protected BaseJpaRepository(Class<A> aggregateRootClass, Class<K> kClass) {
}

@Override
protected A doLoad(K id) {
protected A doLoad(K id) {
return entityManager.find(getAggregateRootClass(), id);
}

@Override
protected void doDelete(K id) {
A aggregate = load(id);
if (aggregate == null) {
throw new EntityNotFoundException("Attempt to delete non-existent aggregate with id " + id + " of class " + getAggregateRootClass().getCanonicalName());
}
entityManager.remove(aggregate);
}

@Override
protected void doDelete(A aggregate) {
entityManager.remove(aggregate);
}

@Override
protected void doPersist(A aggregate) {
entityManager.persist(aggregate);
entityManager.persist(aggregate);
}

@Override
protected A doSave(A aggregate) {
return entityManager.merge(aggregate);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ public final AGGREGATE load(KEY id) {
return doLoad(id);
}

@Override
public final void delete(KEY id) {
doDelete(id);
}

@Override
public final void delete(AGGREGATE aggregate) {
doDelete(aggregate);
Expand All @@ -102,6 +107,14 @@ public final AGGREGATE save(AGGREGATE aggregate) {
@Read
protected abstract AGGREGATE doLoad(KEY id);

/**
* Delegates the delete mechanism to the infrastructure.
*
* @param id the identifier of the aggregate root to delete
*/
@Delete
protected abstract void doDelete(KEY id);

/**
* Delegates the delete mechanism to the infrastructure.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ public interface Repository<A extends AggregateRoot<K>, K> {
/**
* Deletes an aggregate from the persistence by its key.
*
* @param id the aggregate key
*/
@Delete
void delete(K id);

/**
* Deletes an aggregate instance from the persistence.
*
* @param aggregate the aggregate to delete
*/
@Delete
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ static class MyRepositoryImpl1<A extends AggregateRoot<K>, K> implements Generic
@Override
public A load(K id) { return null; }

@Override
public void delete(K k) { }

@Override
public void delete(A a) { }

Expand Down

0 comments on commit 405bd3e

Please sign in to comment.