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:
Oliver Gierke
2011-07-06 19:49:11 +02:00
parent 2256ebac1b
commit dd06973f50
3 changed files with 13 additions and 15 deletions

View File

@@ -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;
}
/*

View File

@@ -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));
}
}

View File

@@ -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;
}
}