Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d845ef1

Browse files
author
Thomas Darimont
committedMar 20, 2015
DATAMONGO-1188 - Add suport for findAndModify in query derivation.
We now also support findAndModify semantic for single entity query executions.
1 parent 0b45ed0 commit d845ef1

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed
 

‎spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/AbstractMongoQuery.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ public Object execute(Object[] parameters) {
111111
} else if (method.isPageQuery()) {
112112
return new PagedExecution(accessor.getPageable()).execute(query);
113113
} else {
114+
115+
if (method.isModifyingQuery()) {
116+
return new UpdatingSingleEntityExecution(accessor.getUpdate()).execute(query);
117+
}
118+
114119
return new SingleEntityExecution(isCountQuery()).execute(query);
115120
}
116121
}
@@ -343,6 +348,31 @@ Object execute(Query query) {
343348
}
344349
}
345350

351+
/**
352+
* {@link Execution} to return a single entity with update.
353+
*
354+
* @author Thomas Darimont
355+
*/
356+
final class UpdatingSingleEntityExecution extends Execution {
357+
358+
private final Update update;
359+
360+
private UpdatingSingleEntityExecution(Update update) {
361+
this.update = update;
362+
}
363+
364+
/*
365+
* (non-Javadoc)
366+
* @see org.springframework.data.mongodb.repository.AbstractMongoQuery.Execution#execute(org.springframework.data.mongodb.core.core.query.Query)
367+
*/
368+
@Override
369+
Object execute(Query query) {
370+
371+
MongoEntityMetadata<?> metadata = method.getEntityInformation();
372+
return operations.findAndModify(query.limit(1), update, metadata.getJavaType(), metadata.getCollectionName());
373+
}
374+
}
375+
346376
/**
347377
* {@link Execution} to execute geo-near queries.
348378
*

‎spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/AbstractPersonRepositoryIntegrationTests.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,7 @@ public void executesGeoNearQueryForResultsCorrectlyWhenGivenMinAndMaxDistance()
11891189
* @see DATAMONGO-1188
11901190
*/
11911191
@Test
1192-
public void shouldSupportFindAndModfiyForQueryDerivation() {
1192+
public void shouldSupportFindAndModfiyForQueryDerivationWithCollectionResult() {
11931193

11941194
List<Person> result = repository.findAndModifyByFirstname("Dave", new Update().inc("visits", 42));
11951195

@@ -1200,4 +1200,20 @@ public void shouldSupportFindAndModfiyForQueryDerivation() {
12001200

12011201
assertThat(dave.visits, is(42));
12021202
}
1203+
1204+
/**
1205+
* @see DATAMONGO-1188
1206+
*/
1207+
@Test
1208+
public void shouldSupportFindAndModfiyForQueryDerivationWithSingleResult() {
1209+
1210+
Person result = repository.findOneAndModifyByFirstname("Dave", new Update().inc("visits", 1337));
1211+
1212+
assertThat(result, is(dave));
1213+
1214+
Person dave = repository.findOne(result.getId());
1215+
1216+
assertThat(dave.visits, is(1337));
1217+
}
1218+
12031219
}

‎spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/PersonRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,4 +336,6 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
336336
Stream<Person> findByCustomQueryWithStreamingCursorByFirstnames(List<String> firstnames);
337337

338338
List<Person> findAndModifyByFirstname(String firstname, Update update);
339+
340+
Person findOneAndModifyByFirstname(String firstname, Update update);
339341
}

0 commit comments

Comments
 (0)
Please sign in to comment.