DATADOC-190 - SimpleMongoRepository.exists(…) now works for entities with non-ObjectId id type.
Changed the implementation of the exists(…) method to make sure the query is handed to the QueryMapper to convert the id appropriately before executing the query.
This commit is contained in:
@@ -22,8 +22,6 @@ import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.data.document.mongodb.CollectionCallback;
|
||||
import org.springframework.data.document.mongodb.MongoOperations;
|
||||
import org.springframework.data.document.mongodb.MongoTemplate;
|
||||
import org.springframework.data.document.mongodb.query.Criteria;
|
||||
@@ -35,10 +33,6 @@ import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import com.mongodb.BasicDBObject;
|
||||
import com.mongodb.DBCollection;
|
||||
import com.mongodb.MongoException;
|
||||
|
||||
/**
|
||||
* Repository base implementation for Mongo.
|
||||
*
|
||||
@@ -121,12 +115,8 @@ public class SimpleMongoRepository<T, ID extends Serializable> implements Paging
|
||||
*/
|
||||
public boolean exists(final ID id) {
|
||||
|
||||
return template.execute(entityInformation.getCollectionName(), new CollectionCallback<Boolean>() {
|
||||
|
||||
public Boolean doInCollection(DBCollection collection) throws MongoException, DataAccessException {
|
||||
return collection.count(new BasicDBObject("_id", id)) > 0;
|
||||
}
|
||||
});
|
||||
return template.findOne(new Query(Criteria.where("_id").is(id)), Object.class,
|
||||
entityInformation.getCollectionName()) != null;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -259,4 +259,12 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
|
||||
assertThat(result.size(), is(1));
|
||||
assertThat(result, hasItem(dave));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATADOC-190
|
||||
*/
|
||||
@Test
|
||||
public void existsWorksCorrectly() {
|
||||
assertThat(repository.exists(dave.getId()), is(true));
|
||||
}
|
||||
}
|
||||
@@ -28,13 +28,13 @@ import org.springframework.data.document.mongodb.mapping.Document;
|
||||
abstract class Contact {
|
||||
|
||||
@Id
|
||||
protected final ObjectId id;
|
||||
protected final String id;
|
||||
|
||||
public Contact() {
|
||||
this.id = new ObjectId();
|
||||
this.id = new ObjectId().toString();
|
||||
}
|
||||
|
||||
public ObjectId getId() {
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user