Skip to content

Commit 85f1895

Browse files
committed
Fix BaseMorphiaRepository contains
1 parent 9779f5a commit 85f1895

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

morphia/src/main/java/org/seedstack/mongodb/morphia/BaseMorphiaRepository.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
package org.seedstack.mongodb.morphia;
99

1010
import org.mongodb.morphia.Datastore;
11+
import org.mongodb.morphia.mapping.Mapper;
12+
import org.mongodb.morphia.query.CountOptions;
1113
import org.mongodb.morphia.query.CriteriaContainer;
1214
import org.mongodb.morphia.query.Query;
1315
import org.seedstack.business.domain.AggregateExistsException;
@@ -72,9 +74,14 @@ public Optional<A> get(ID id) {
7274
return Optional.ofNullable(datastore.get(getAggregateRootClass(), id));
7375
}
7476

77+
@Override
78+
public boolean contains(Specification<A> specification) {
79+
return buildQuery(specification).count(new CountOptions().limit(1)) > 0;
80+
}
81+
7582
@Override
7683
public boolean contains(ID id) {
77-
return datastore.exists(id) != null;
84+
return datastore.find(getAggregateRootClass()).filter(Mapper.ID_KEY, id).count(new CountOptions().limit(1)) > 0;
7885
}
7986

8087
@Override
@@ -89,20 +96,14 @@ public long size() {
8996

9097
@Override
9198
public long remove(Specification<A> specification) throws AggregateNotFoundException {
92-
Query<A> query = buildQuery(specification);
93-
return datastore.delete(query).getN();
99+
return datastore.delete(buildQuery(specification)).getN();
94100
}
95101

96102
@Override
97103
public void remove(ID id) throws AggregateNotFoundException {
98104
checkExactlyOneAggregateRemoved(datastore.delete(getAggregateRootClass(), id).getN(), id);
99105
}
100106

101-
@Override
102-
public void remove(A aggregate) throws AggregateNotFoundException {
103-
checkExactlyOneAggregateRemoved(datastore.delete(aggregate).getN(), aggregate.getId());
104-
}
105-
106107
private void checkExactlyOneAggregateRemoved(int n, ID id) {
107108
if (n == 0) {
108109
throw new AggregateNotFoundException("Non-existent aggregate " + getAggregateRootClass().getSimpleName() + " identified with " + id + " cannot be removed");

0 commit comments

Comments
 (0)