added findOne methods to MongoOperations; added support to automagically map 'id' fields in queries to '_id' and to convert String to ObjectId if possible; fixed SimpleMongoConverter's mapping of a bean property named '_id'
This commit is contained in:
@@ -223,6 +223,81 @@ public interface MongoOperations {
|
||||
*/
|
||||
void ensureIndex(String collectionName, IndexSpecification indexSpecification);
|
||||
|
||||
/**
|
||||
* Map the results of an ad-hoc query on the default MongoDB collection to a single instance of an object
|
||||
* of the specified type.
|
||||
*
|
||||
* The object is converted from the MongoDB native representation using an instance of
|
||||
* {@see MongoConverter}. Unless configured otherwise, an
|
||||
* instance of SimpleMongoConverter will be used.
|
||||
*
|
||||
* The query is specified as a {@link Query} which can be created either using the {@link BasicQuery} or the more
|
||||
* feature rich {@link Query}.
|
||||
*
|
||||
* @param query the query class that specifies the criteria used to find a record and also an optional fields specification
|
||||
* @param targetClass the parameterized type of the returned list.
|
||||
* @return the converted object
|
||||
*/
|
||||
<T> T findOne(Query query, Class<T> targetClass);
|
||||
|
||||
/**
|
||||
* Map the results of an ad-hoc query on the default MongoDB collection to a single instance of an object
|
||||
* of the specified type.
|
||||
*
|
||||
* The object is converted from the MongoDB native representation using an instance of
|
||||
* {@see MongoConverter}. Unless configured otherwise, an
|
||||
* instance of SimpleMongoConverter will be used.
|
||||
*
|
||||
* The query is specified as a {@link Query} which can be created either using the {@link BasicQuery} or the more
|
||||
* feature rich {@link Query}.
|
||||
*
|
||||
* @param query the query class that specifies the criteria used to find a record and also an optional fields specification
|
||||
* @param targetClass the parameterized type of the returned list.
|
||||
* @param reader the MongoReader to convert from DBObject to an object.
|
||||
* @return the converted object
|
||||
*/
|
||||
<T> T findOne(Query query, Class<T> targetClass,
|
||||
MongoReader<T> reader);
|
||||
|
||||
/**
|
||||
* Map the results of an ad-hoc query on the specified collection to a single instance of an object
|
||||
* of the specified type.
|
||||
*
|
||||
* The object is converted from the MongoDB native representation using an instance of
|
||||
* {@see MongoConverter}. Unless configured otherwise, an
|
||||
* instance of SimpleMongoConverter will be used.
|
||||
*
|
||||
* The query is specified as a {@link Query} which can be created either using the {@link BasicQuery} or the more
|
||||
* feature rich {@link Query}.
|
||||
*
|
||||
* @param collectionName name of the collection to retrieve the objects from
|
||||
* @param query the query class that specifies the criteria used to find a record and also an optional fields specification
|
||||
* @param targetClass the parameterized type of the returned list.
|
||||
* @return the converted object
|
||||
*/
|
||||
<T> T findOne(String collectionName, Query query,
|
||||
Class<T> targetClass);
|
||||
|
||||
/**
|
||||
* Map the results of an ad-hoc query on the specified collection to a single instance of an object
|
||||
* of the specified type.
|
||||
*
|
||||
* The object is converted from the MongoDB native representation using an instance of
|
||||
* {@see MongoConverter}. Unless configured otherwise, an
|
||||
* instance of SimpleMongoConverter will be used.
|
||||
*
|
||||
* The query is specified as a {@link Query} which can be created either using the {@link BasicQuery} or the more
|
||||
* feature rich {@link Query}.
|
||||
*
|
||||
* @param collectionName name of the collection to retrieve the objects from
|
||||
* @param query the query class that specifies the criteria used to find a record and also an optional fields specification
|
||||
* @param targetClass the parameterized type of the returned list.
|
||||
* @param reader the MongoReader to convert from DBObject to an object.
|
||||
* @return the converted object
|
||||
*/
|
||||
<T> T findOne(String collectionName, Query query,
|
||||
Class<T> targetClass, MongoReader<T> reader);
|
||||
|
||||
/**
|
||||
* Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type.
|
||||
*
|
||||
@@ -258,7 +333,7 @@ public interface MongoOperations {
|
||||
MongoReader<T> reader);
|
||||
|
||||
/**
|
||||
* Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type.
|
||||
* Map the results of an ad-hoc query on the specified collection to a List of the specified type.
|
||||
*
|
||||
* The object is converted from the MongoDB native representation using an instance of
|
||||
* {@see MongoConverter}. Unless configured otherwise, an
|
||||
@@ -276,7 +351,7 @@ public interface MongoOperations {
|
||||
Class<T> targetClass);
|
||||
|
||||
/**
|
||||
* Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type.
|
||||
* Map the results of an ad-hoc query on the specified collection to a List of the specified type.
|
||||
*
|
||||
* The object is converted from the MongoDB native representation using an instance of
|
||||
* {@see MongoConverter}. Unless configured otherwise, an
|
||||
@@ -296,7 +371,7 @@ public interface MongoOperations {
|
||||
|
||||
|
||||
/**
|
||||
* Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type.
|
||||
* Map the results of an ad-hoc query on the specified collection to a List of the specified type.
|
||||
*
|
||||
* The object is converted from the MongoDB native representation using an instance of
|
||||
* {@see MongoConverter}. Unless configured otherwise, an
|
||||
|
||||
@@ -120,7 +120,7 @@ public class MongoPropertyDescriptors implements Iterable<MongoPropertyDescripto
|
||||
* @return
|
||||
*/
|
||||
boolean isIdProperty() {
|
||||
return ID_PROPERTY.equals(delegate.getName());
|
||||
return ID_PROPERTY.equals(delegate.getName()) || ID_KEY.equals(delegate.getName());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.springframework.data.document.mongodb;
|
||||
|
||||
import java.beans.IntrospectionException;
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
@@ -27,6 +29,7 @@ import org.bson.types.ObjectId;
|
||||
import org.springframework.beans.ConfigurablePropertyAccessor;
|
||||
import org.springframework.beans.PropertyAccessorFactory;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.core.convert.ConversionFailedException;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.data.document.mongodb.MongoPropertyDescriptors.MongoPropertyDescriptor;
|
||||
@@ -295,6 +298,29 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Central callback executing method to do queries against the datastore that requires reading a single object from a
|
||||
* collection of objects. It will take the following steps <ol> <li>Execute the given {@link ConnectionCallback} for a
|
||||
* {@link DBObject}.</li> <li>Apply the given
|
||||
* {@link DbObjectCallback} to each of the {@link DBObject}s to obtain the result.</li> <ol>
|
||||
*
|
||||
* @param <T>
|
||||
* @param collectionCallback the callback to retrieve the {@link DBObject} with
|
||||
* @param objectCallback the {@link DbObjectCallback} to transform {@link DBObject}s into the actual domain type
|
||||
* @param collectionName the collection to be queried
|
||||
* @return
|
||||
*/
|
||||
private <T> T execute(CollectionCallback<DBObject> collectionCallback,
|
||||
DbObjectCallback<T> objectCallback, String collectionName) {
|
||||
|
||||
try {
|
||||
T result = objectCallback.doWith(collectionCallback.doInCollection(getCollection(collectionName)));
|
||||
return result;
|
||||
} catch (MongoException e) {
|
||||
throw potentiallyConvertRuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Central callback executing method to do queries against the datastore that requires reading a collection of
|
||||
* objects. It will take the following steps <ol> <li>Execute the given {@link ConnectionCallback} for a
|
||||
@@ -428,10 +454,31 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
|
||||
}, collectionName);
|
||||
}
|
||||
|
||||
// Find methods that take a Query to express the query.
|
||||
// Find methods that take a Query to express the query and that return a single object.
|
||||
|
||||
public <T> T findOne(Query query, Class<T> targetClass) {
|
||||
return findOne(getDefaultCollectionName(), query, targetClass);
|
||||
}
|
||||
|
||||
public <T> T findOne(Query query, Class<T> targetClass,
|
||||
MongoReader<T> reader) {
|
||||
return findOne(getDefaultCollectionName(), query, targetClass, reader);
|
||||
}
|
||||
|
||||
public <T> T findOne(String collectionName, Query query,
|
||||
Class<T> targetClass) {
|
||||
return findOne(collectionName, query, targetClass, null);
|
||||
}
|
||||
|
||||
public <T> T findOne(String collectionName, Query query,
|
||||
Class<T> targetClass, MongoReader<T> reader) {
|
||||
return doFindOne(collectionName, query.getQueryObject(), query.getFieldsObject(), targetClass, reader);
|
||||
}
|
||||
|
||||
// Find methods that take a Query to express the query and that return a List of objects.
|
||||
|
||||
public <T> List<T> find(Query query, Class<T> targetClass) {
|
||||
return find(getDefaultCollectionName(), query, targetClass); //
|
||||
return find(getDefaultCollectionName(), query, targetClass);
|
||||
}
|
||||
|
||||
public <T> List<T> find(Query query, Class<T> targetClass, MongoReader<T> reader) {
|
||||
@@ -494,7 +541,7 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
|
||||
public <T> void insert(String collectionName, T objectToSave, MongoWriter<T> writer) {
|
||||
BasicDBObject dbDoc = new BasicDBObject();
|
||||
writer.write(objectToSave, dbDoc);
|
||||
ObjectId id = insertDBObject(collectionName, dbDoc);
|
||||
Object id = insertDBObject(collectionName, dbDoc);
|
||||
populateIdIfNecessary(objectToSave, id);
|
||||
}
|
||||
|
||||
@@ -558,21 +605,21 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
|
||||
}
|
||||
|
||||
|
||||
protected ObjectId insertDBObject(String collectionName, final DBObject dbDoc) {
|
||||
protected Object insertDBObject(String collectionName, final DBObject dbDoc) {
|
||||
|
||||
if (dbDoc.keySet().isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return execute(new CollectionCallback<ObjectId>() {
|
||||
public ObjectId doInCollection(DBCollection collection) throws MongoException, DataAccessException {
|
||||
return execute(new CollectionCallback<Object>() {
|
||||
public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException {
|
||||
if (writeConcern == null) {
|
||||
collection.insert(dbDoc);
|
||||
}
|
||||
else {
|
||||
collection.insert(dbDoc, writeConcern);
|
||||
}
|
||||
return (ObjectId) dbDoc.get(ID);
|
||||
return dbDoc.get(ID);
|
||||
}
|
||||
}, collectionName);
|
||||
}
|
||||
@@ -740,6 +787,27 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
|
||||
return MongoDbUtils.getDB(mongo, databaseName, username, password == null ? null : password.toCharArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* Map the results of an ad-hoc query on the default MongoDB collection to an object using the provided MongoReader
|
||||
*
|
||||
* The query document is specified as a standard DBObject and so is the fields specification.
|
||||
*
|
||||
* @param collectionName name of the collection to retrieve the objects from
|
||||
* @param query the query document that specifies the criteria used to find a record
|
||||
* @param fields the document that specifies the fields to be returned
|
||||
* @param targetClass the parameterized type of the returned list.
|
||||
* @param reader the MongoReader to convert from DBObject to an object.
|
||||
* @return the List of converted objects.
|
||||
*/
|
||||
protected <T> T doFindOne(String collectionName, DBObject query, DBObject fields, Class<T> targetClass, MongoReader<T> reader) {
|
||||
MongoReader<? super T> readerToUse = reader;
|
||||
if (readerToUse == null) {
|
||||
readerToUse = this.mongoConverter;
|
||||
}
|
||||
substituteMappedIdIfNecessary(query, targetClass, readerToUse);
|
||||
return execute(new FindOneCallback(query, fields), new ReadDbObjectCallback<T>(readerToUse, targetClass),
|
||||
collectionName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type.
|
||||
@@ -761,6 +829,7 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
|
||||
* @return the List of converted objects.
|
||||
*/
|
||||
protected <T> List<T> doFind(String collectionName, DBObject query, DBObject fields, Class<T> targetClass, CursorPreparer preparer) {
|
||||
substituteMappedIdIfNecessary(query, targetClass, mongoConverter);
|
||||
return executeEach(new FindCallback(query, fields), preparer, new ReadDbObjectCallback<T>(mongoConverter, targetClass),
|
||||
collectionName);
|
||||
}
|
||||
@@ -778,6 +847,7 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
|
||||
* @return the List of converted objects.
|
||||
*/
|
||||
protected <T> List<T> doFind(String collectionName, DBObject query, DBObject fields, Class<T> targetClass, MongoReader<T> reader) {
|
||||
substituteMappedIdIfNecessary(query, targetClass, reader);
|
||||
return executeEach(new FindCallback(query, fields), null, new ReadDbObjectCallback<T>(reader, targetClass),
|
||||
collectionName);
|
||||
}
|
||||
@@ -804,7 +874,7 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
|
||||
* @param savedObject
|
||||
* @param id
|
||||
*/
|
||||
protected void populateIdIfNecessary(Object savedObject, ObjectId id) {
|
||||
protected void populateIdIfNecessary(Object savedObject, Object id) {
|
||||
|
||||
if (id == null) {
|
||||
return;
|
||||
@@ -818,11 +888,72 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
|
||||
}
|
||||
|
||||
if (bw.getPropertyValue(idDescriptor.getName()) == null) {
|
||||
Object target = this.mongoConverter.convertObjectId(id, idDescriptor.getPropertyType());
|
||||
Object target = null;
|
||||
if (id instanceof ObjectId) {
|
||||
target = this.mongoConverter.convertObjectId((ObjectId)id, idDescriptor.getPropertyType());
|
||||
}
|
||||
else {
|
||||
target = id;
|
||||
}
|
||||
bw.setPropertyValue(idDescriptor.getName(), target);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Substitutes the id key if it is found in he query. Any 'id' keys will be replaced with '_id' and the value converted
|
||||
* to an ObjectId if possible. This conversion should match the way that the id fields are converted during read
|
||||
* operations.
|
||||
*
|
||||
* @param query
|
||||
* @param targetClass
|
||||
* @param reader
|
||||
*/
|
||||
protected void substituteMappedIdIfNecessary(DBObject query, Class<?> targetClass, MongoReader<?> reader) {
|
||||
MongoConverter converter = null;
|
||||
if (reader instanceof SimpleMongoConverter) {
|
||||
converter = (MongoConverter) reader;
|
||||
}
|
||||
else {
|
||||
return;
|
||||
}
|
||||
String idKey = null;
|
||||
if (query.containsField("id")) {
|
||||
idKey = "id";
|
||||
}
|
||||
if (query.containsField("_id")) {
|
||||
idKey = "_id";
|
||||
}
|
||||
if (idKey == null) {
|
||||
// no ids in this query
|
||||
return;
|
||||
}
|
||||
final MongoPropertyDescriptor descriptor;
|
||||
try {
|
||||
descriptor = new MongoPropertyDescriptor(new PropertyDescriptor(idKey, targetClass));
|
||||
} catch (IntrospectionException e) {
|
||||
// no property descriptor for this key
|
||||
return;
|
||||
}
|
||||
if (descriptor.isIdProperty() && descriptor.isOfIdType()) {
|
||||
Object value = query.get(idKey);
|
||||
ObjectId newValue = null;
|
||||
try {
|
||||
if (value instanceof String && ObjectId.isValid((String)value)) {
|
||||
newValue = converter.convertObjectId(value);
|
||||
}
|
||||
} catch (ConversionFailedException iae) {
|
||||
LOGGER.warn("Unable to convert the String " + value + " to an ObjectId");
|
||||
}
|
||||
query.removeField(idKey);
|
||||
if (newValue != null) {
|
||||
query.put(MongoPropertyDescriptor.ID_KEY, newValue);
|
||||
}
|
||||
else {
|
||||
query.put(MongoPropertyDescriptor.ID_KEY, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private String getRequiredDefaultCollectionName() {
|
||||
String name = getDefaultCollectionName();
|
||||
@@ -894,6 +1025,34 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Simple {@link CollectionCallback} that takes a query {@link DBObject} plus an optional fields specification
|
||||
* {@link DBObject} and executes that against the {@link DBCollection}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Risberg
|
||||
*/
|
||||
private static class FindOneCallback implements CollectionCallback<DBObject> {
|
||||
|
||||
private final DBObject query;
|
||||
|
||||
private final DBObject fields;
|
||||
|
||||
public FindOneCallback(DBObject query, DBObject fields) {
|
||||
this.query = query;
|
||||
this.fields = fields;
|
||||
}
|
||||
|
||||
public DBObject doInCollection(DBCollection collection) throws MongoException, DataAccessException {
|
||||
if (fields == null) {
|
||||
return collection.findOne(query);
|
||||
}
|
||||
else {
|
||||
return collection.findOne(query, fields);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple {@link CollectionCallback} that takes a query {@link DBObject} plus an optional fields specification
|
||||
* {@link DBObject} and executes that against the {@link DBCollection}.
|
||||
|
||||
@@ -45,6 +45,7 @@ import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.core.convert.support.ConversionServiceFactory;
|
||||
import org.springframework.core.convert.support.GenericConversionService;
|
||||
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||
import org.springframework.data.document.mongodb.MongoPropertyDescriptors.MongoPropertyDescriptor;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.comparator.CompoundComparator;
|
||||
@@ -162,7 +163,7 @@ public class SimpleMongoConverter implements MongoConverter {
|
||||
@SuppressWarnings("rawtypes")
|
||||
public void write(Object obj, DBObject dbo) {
|
||||
|
||||
MongoBeanWrapper beanWrapper = createWraper(obj, false);
|
||||
MongoBeanWrapper beanWrapper = createWrapper(obj, false);
|
||||
for (MongoPropertyDescriptor descriptor : beanWrapper.getDescriptors()) {
|
||||
if (descriptor.isMappable()) {
|
||||
Object value = beanWrapper.getValue(descriptor);
|
||||
@@ -176,13 +177,18 @@ public class SimpleMongoConverter implements MongoConverter {
|
||||
if (descriptor.isEnum()) {
|
||||
writeValue(dbo, keyToUse, ((Enum) value).name());
|
||||
} else if (descriptor.isIdProperty() && descriptor.isOfIdType()) {
|
||||
|
||||
if (value instanceof String && ObjectId.isValid((String)value)) {
|
||||
try {
|
||||
writeValue(dbo, keyToUse, conversionService.convert(value, ObjectId.class));
|
||||
} catch (ConversionFailedException iae) {
|
||||
LOG.debug("Unable to convert the String " + value + " to an ObjectId");
|
||||
LOG.warn("Unable to convert the String " + value + " to an ObjectId");
|
||||
writeValue(dbo, keyToUse, value);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// we can't convert this id - use as is
|
||||
writeValue(dbo, keyToUse, value);
|
||||
}
|
||||
} else {
|
||||
writeValue(dbo, keyToUse, value);
|
||||
}
|
||||
@@ -307,6 +313,10 @@ public class SimpleMongoConverter implements MongoConverter {
|
||||
* @see org.springframework.data.document.mongodb.MongoReader#read(java.lang.Class, com.mongodb.DBObject)
|
||||
*/
|
||||
public <S> S read(Class<S> clazz, DBObject source) {
|
||||
|
||||
if (source == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Assert.notNull(clazz, "Mapped class was not specified");
|
||||
S target = BeanUtils.instantiateClass(clazz);
|
||||
@@ -446,7 +456,7 @@ public class SimpleMongoConverter implements MongoConverter {
|
||||
* @param fieldAccess whether to use field access or property access
|
||||
* @return
|
||||
*/
|
||||
protected MongoBeanWrapper createWraper(Object target, boolean fieldAccess) {
|
||||
protected MongoBeanWrapper createWrapper(Object target, boolean fieldAccess) {
|
||||
|
||||
return new MongoBeanWrapper(target, conversionService, fieldAccess);
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ package org.springframework.data.document.mongodb;
|
||||
|
||||
import static org.hamcrest.Matchers.hasItem;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.hamcrest.Matchers.endsWith;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
@@ -24,6 +25,7 @@ import static org.springframework.data.document.mongodb.query.Criteria.where;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.bson.types.ObjectId;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
@@ -117,7 +119,6 @@ public class MongoTemplateTests {
|
||||
boolean unique = false;
|
||||
boolean dropDupes = false;
|
||||
for (DBObject ix : indexInfo) {
|
||||
System.out.println(ix);
|
||||
if ("age_-1".equals(ix.get("name"))) {
|
||||
indexKey = ix.get("key").toString();
|
||||
unique = (Boolean) ix.get("unique");
|
||||
@@ -130,8 +131,82 @@ public class MongoTemplateTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void simpleQuery() throws Exception {
|
||||
new Query(where("name").is("Mary")).and(where("age").lt(33).gt(22)).skip(22).limit(20);
|
||||
// TODO: more tests
|
||||
public void testProperHandlingOfDifferentIdTypes() throws Exception {
|
||||
PersonWithIdPropertyOfTypeString p1 = new PersonWithIdPropertyOfTypeString();
|
||||
p1.setFirstName("Sven_1");
|
||||
p1.setAge(22);
|
||||
template.insert(p1);
|
||||
assertThat(p1.getId(), notNullValue());
|
||||
PersonWithIdPropertyOfTypeString p1q = template.findOne(new Query(where("id").is(p1.getId())), PersonWithIdPropertyOfTypeString.class);
|
||||
assertThat(p1q, notNullValue());
|
||||
assertThat(p1q.getId(), is(p1.getId()));
|
||||
|
||||
PersonWithIdPropertyOfTypeString p2 = new PersonWithIdPropertyOfTypeString();
|
||||
p2.setFirstName("Sven_2");
|
||||
p2.setAge(22);
|
||||
p2.setId("TWO");
|
||||
template.insert(p2);
|
||||
assertThat(p2.getId(), notNullValue());
|
||||
PersonWithIdPropertyOfTypeString p2q = template.findOne(new Query(where("id").is(p2.getId())), PersonWithIdPropertyOfTypeString.class);
|
||||
assertThat(p2q, notNullValue());
|
||||
assertThat(p2q.getId(), is(p2.getId()));
|
||||
|
||||
PersonWith_idPropertyOfTypeString p3 = new PersonWith_idPropertyOfTypeString();
|
||||
p3.setFirstName("Sven_3");
|
||||
p3.setAge(22);
|
||||
template.insert(p3);
|
||||
assertThat(p3.get_id(), notNullValue());
|
||||
PersonWith_idPropertyOfTypeString p3q = template.findOne(new Query(where("_id").is(p3.get_id())), PersonWith_idPropertyOfTypeString.class);
|
||||
assertThat(p3q, notNullValue());
|
||||
assertThat(p3q.get_id(), is(p3.get_id()));
|
||||
|
||||
PersonWith_idPropertyOfTypeString p4 = new PersonWith_idPropertyOfTypeString();
|
||||
p4.setFirstName("Sven_4");
|
||||
p4.setAge(22);
|
||||
p4.set_id("FOUR");
|
||||
template.insert(p4);
|
||||
assertThat(p4.get_id(), notNullValue());
|
||||
PersonWith_idPropertyOfTypeString p4q = template.findOne(new Query(where("_id").is(p4.get_id())), PersonWith_idPropertyOfTypeString.class);
|
||||
assertThat(p4q, notNullValue());
|
||||
assertThat(p4q.get_id(), is(p4.get_id()));
|
||||
|
||||
PersonWithIdPropertyOfTypeObjectId p5 = new PersonWithIdPropertyOfTypeObjectId();
|
||||
p5.setFirstName("Sven_5");
|
||||
p5.setAge(22);
|
||||
template.insert(p5);
|
||||
assertThat(p5.getId(), notNullValue());
|
||||
PersonWithIdPropertyOfTypeObjectId p5q = template.findOne(new Query(where("id").is(p5.getId())), PersonWithIdPropertyOfTypeObjectId.class);
|
||||
assertThat(p5q, notNullValue());
|
||||
assertThat(p5q.getId(), is(p5.getId()));
|
||||
|
||||
PersonWithIdPropertyOfTypeObjectId p6 = new PersonWithIdPropertyOfTypeObjectId();
|
||||
p6.setFirstName("Sven_6");
|
||||
p6.setAge(22);
|
||||
p6.setId(new ObjectId());
|
||||
template.insert(p6);
|
||||
assertThat(p6.getId(), notNullValue());
|
||||
PersonWithIdPropertyOfTypeObjectId p6q = template.findOne(new Query(where("id").is(p6.getId())), PersonWithIdPropertyOfTypeObjectId.class);
|
||||
assertThat(p6q, notNullValue());
|
||||
assertThat(p6q.getId(), is(p6.getId()));
|
||||
|
||||
PersonWith_idPropertyOfTypeObjectId p7 = new PersonWith_idPropertyOfTypeObjectId();
|
||||
p7.setFirstName("Sven_7");
|
||||
p7.setAge(22);
|
||||
template.insert(p7);
|
||||
assertThat(p7.get_id(), notNullValue());
|
||||
PersonWith_idPropertyOfTypeObjectId p7q = template.findOne(new Query(where("_id").is(p7.get_id())), PersonWith_idPropertyOfTypeObjectId.class);
|
||||
assertThat(p7q, notNullValue());
|
||||
assertThat(p7q.get_id(), is(p7.get_id()));
|
||||
|
||||
PersonWith_idPropertyOfTypeObjectId p8 = new PersonWith_idPropertyOfTypeObjectId();
|
||||
p8.setFirstName("Sven_8");
|
||||
p8.setAge(22);
|
||||
p8.set_id(new ObjectId());
|
||||
template.insert(p8);
|
||||
assertThat(p8.get_id(), notNullValue());
|
||||
PersonWith_idPropertyOfTypeObjectId p8q = template.findOne(new Query(where("_id").is(p8.get_id())), PersonWith_idPropertyOfTypeObjectId.class);
|
||||
assertThat(p8q, notNullValue());
|
||||
assertThat(p8q.get_id(), is(p8.get_id()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright 2010-2011 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.document.mongodb;
|
||||
|
||||
import org.bson.types.ObjectId;
|
||||
|
||||
public class PersonWithIdPropertyOfTypeObjectId {
|
||||
|
||||
private ObjectId id;
|
||||
|
||||
private String firstName;
|
||||
|
||||
private int age;
|
||||
|
||||
public ObjectId getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(ObjectId id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright 2010-2011 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.document.mongodb;
|
||||
|
||||
|
||||
public class PersonWithIdPropertyOfTypeString {
|
||||
|
||||
private String id;
|
||||
|
||||
private String firstName;
|
||||
|
||||
private int age;
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright 2010-2011 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.document.mongodb;
|
||||
|
||||
import org.bson.types.ObjectId;
|
||||
|
||||
public class PersonWith_idPropertyOfTypeObjectId {
|
||||
|
||||
private ObjectId _id;
|
||||
|
||||
private String firstName;
|
||||
|
||||
private int age;
|
||||
|
||||
public ObjectId get_id() {
|
||||
return _id;
|
||||
}
|
||||
|
||||
public void set_id(ObjectId _id) {
|
||||
this._id = _id;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright 2010-2011 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.document.mongodb;
|
||||
|
||||
|
||||
public class PersonWith_idPropertyOfTypeString {
|
||||
|
||||
private String _id;
|
||||
|
||||
private String firstName;
|
||||
|
||||
private int age;
|
||||
|
||||
public String get_id() {
|
||||
return _id;
|
||||
}
|
||||
|
||||
public void set_id(String _id) {
|
||||
this._id = _id;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user