update methods now return WriteResult; added configurable WriteResultChecking for update methods
This commit is contained in:
@@ -24,6 +24,7 @@ import org.springframework.data.document.mongodb.builder.UpdateDefinition;
|
|||||||
import com.mongodb.CommandResult;
|
import com.mongodb.CommandResult;
|
||||||
import com.mongodb.DBCollection;
|
import com.mongodb.DBCollection;
|
||||||
import com.mongodb.DBObject;
|
import com.mongodb.DBObject;
|
||||||
|
import com.mongodb.WriteResult;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Interface that specifies a basic set of MongoDB operations. Implemented by {@link MongoTemplate}.
|
* Interface that specifies a basic set of MongoDB operations. Implemented by {@link MongoTemplate}.
|
||||||
@@ -433,7 +434,7 @@ public interface MongoOperations {
|
|||||||
* @param updateDoc the update document that contains the updated object or $ operators to manipulate the
|
* @param updateDoc the update document that contains the updated object or $ operators to manipulate the
|
||||||
* existing object.
|
* existing object.
|
||||||
*/
|
*/
|
||||||
void updateFirst(QueryDefinition query, UpdateDefinition update);
|
WriteResult updateFirst(QueryDefinition query, UpdateDefinition update);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates the first object that is found in the specified collection that matches the query document criteria
|
* Updates the first object that is found in the specified collection that matches the query document criteria
|
||||||
@@ -444,7 +445,7 @@ public interface MongoOperations {
|
|||||||
* @param updateDoc the update document that contains the updated object or $ operators to manipulate the
|
* @param updateDoc the update document that contains the updated object or $ operators to manipulate the
|
||||||
* existing object.
|
* existing object.
|
||||||
*/
|
*/
|
||||||
void updateFirst(String collectionName, QueryDefinition query,
|
WriteResult updateFirst(String collectionName, QueryDefinition query,
|
||||||
UpdateDefinition update);
|
UpdateDefinition update);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -455,7 +456,7 @@ public interface MongoOperations {
|
|||||||
* @param updateDoc the update document that contains the updated object or $ operators to manipulate the
|
* @param updateDoc the update document that contains the updated object or $ operators to manipulate the
|
||||||
* existing object.
|
* existing object.
|
||||||
*/
|
*/
|
||||||
void updateMulti(QueryDefinition query, UpdateDefinition update);
|
WriteResult updateMulti(QueryDefinition query, UpdateDefinition update);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates all objects that are found in the specified collection that matches the query document criteria
|
* Updates all objects that are found in the specified collection that matches the query document criteria
|
||||||
@@ -466,7 +467,7 @@ public interface MongoOperations {
|
|||||||
* @param updateDoc the update document that contains the updated object or $ operators to manipulate the
|
* @param updateDoc the update document that contains the updated object or $ operators to manipulate the
|
||||||
* existing object.
|
* existing object.
|
||||||
*/
|
*/
|
||||||
void updateMulti(String collectionName, QueryDefinition query,
|
WriteResult updateMulti(String collectionName, QueryDefinition query,
|
||||||
UpdateDefinition update);
|
UpdateDefinition update);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ import org.springframework.beans.ConfigurablePropertyAccessor;
|
|||||||
import org.springframework.beans.PropertyAccessorFactory;
|
import org.springframework.beans.PropertyAccessorFactory;
|
||||||
import org.springframework.beans.factory.InitializingBean;
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
import org.springframework.dao.DataAccessException;
|
import org.springframework.dao.DataAccessException;
|
||||||
|
import org.springframework.dao.DataIntegrityViolationException;
|
||||||
import org.springframework.data.document.mongodb.MongoPropertyDescriptors.MongoPropertyDescriptor;
|
import org.springframework.data.document.mongodb.MongoPropertyDescriptors.MongoPropertyDescriptor;
|
||||||
import org.springframework.data.document.mongodb.builder.QueryDefinition;
|
import org.springframework.data.document.mongodb.builder.QueryDefinition;
|
||||||
import org.springframework.data.document.mongodb.builder.UpdateDefinition;
|
import org.springframework.data.document.mongodb.builder.UpdateDefinition;
|
||||||
@@ -43,6 +44,7 @@ import com.mongodb.DBObject;
|
|||||||
import com.mongodb.Mongo;
|
import com.mongodb.Mongo;
|
||||||
import com.mongodb.MongoException;
|
import com.mongodb.MongoException;
|
||||||
import com.mongodb.WriteConcern;
|
import com.mongodb.WriteConcern;
|
||||||
|
import com.mongodb.WriteResult;
|
||||||
import com.mongodb.util.JSON;
|
import com.mongodb.util.JSON;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -65,6 +67,12 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
|
|||||||
*/
|
*/
|
||||||
private WriteConcern writeConcern = null;
|
private WriteConcern writeConcern = null;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* WriteResultChecking to be used for write operations if it has been specified. Otherwise
|
||||||
|
* we should not do any checking.
|
||||||
|
*/
|
||||||
|
private WriteResultChecking writeResultChecking = WriteResultChecking.NONE;
|
||||||
|
|
||||||
private final MongoConverter mongoConverter;
|
private final MongoConverter mongoConverter;
|
||||||
private final Mongo mongo;
|
private final Mongo mongo;
|
||||||
private final MongoExceptionTranslator exceptionTranslator = new MongoExceptionTranslator();
|
private final MongoExceptionTranslator exceptionTranslator = new MongoExceptionTranslator();
|
||||||
@@ -81,7 +89,7 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
|
|||||||
* @param databaseName
|
* @param databaseName
|
||||||
*/
|
*/
|
||||||
public MongoTemplate(Mongo mongo, String databaseName) {
|
public MongoTemplate(Mongo mongo, String databaseName) {
|
||||||
this(mongo, databaseName, null, null, null);
|
this(mongo, databaseName, null, null, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -91,8 +99,8 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
|
|||||||
* @param databaseName
|
* @param databaseName
|
||||||
* @param writeConcern
|
* @param writeConcern
|
||||||
*/
|
*/
|
||||||
public MongoTemplate(Mongo mongo, String databaseName, WriteConcern writeConcern) {
|
public MongoTemplate(Mongo mongo, String databaseName, WriteConcern writeConcern, WriteResultChecking writeResultChecking) {
|
||||||
this(mongo, databaseName, null, null, writeConcern);
|
this(mongo, databaseName, null, null, writeConcern, writeResultChecking);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -102,7 +110,7 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
|
|||||||
* @param defaultCollectionName
|
* @param defaultCollectionName
|
||||||
*/
|
*/
|
||||||
public MongoTemplate(Mongo mongo, String databaseName, String defaultCollectionName) {
|
public MongoTemplate(Mongo mongo, String databaseName, String defaultCollectionName) {
|
||||||
this(mongo, databaseName, defaultCollectionName, null, null);
|
this(mongo, databaseName, defaultCollectionName, null, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -113,8 +121,8 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
|
|||||||
* @param defaultCollectionName
|
* @param defaultCollectionName
|
||||||
* @param writeConcern
|
* @param writeConcern
|
||||||
*/
|
*/
|
||||||
public MongoTemplate(Mongo mongo, String databaseName, String defaultCollectionName, WriteConcern writeConcern) {
|
public MongoTemplate(Mongo mongo, String databaseName, String defaultCollectionName, WriteConcern writeConcern, WriteResultChecking writeResultChecking) {
|
||||||
this(mongo, databaseName, defaultCollectionName, null, writeConcern);
|
this(mongo, databaseName, defaultCollectionName, null, writeConcern, writeResultChecking);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -125,7 +133,7 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
|
|||||||
* @param mongoConverter
|
* @param mongoConverter
|
||||||
*/
|
*/
|
||||||
public MongoTemplate(Mongo mongo, String databaseName, String defaultCollectionName, MongoConverter mongoConverter) {
|
public MongoTemplate(Mongo mongo, String databaseName, String defaultCollectionName, MongoConverter mongoConverter) {
|
||||||
this(mongo, databaseName, defaultCollectionName, mongoConverter, null);
|
this(mongo, databaseName, defaultCollectionName, mongoConverter, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -136,8 +144,9 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
|
|||||||
* @param defaultCollectionName
|
* @param defaultCollectionName
|
||||||
* @param mongoConverter
|
* @param mongoConverter
|
||||||
* @param writeConcern
|
* @param writeConcern
|
||||||
|
* @param writeResultChecking
|
||||||
*/
|
*/
|
||||||
public MongoTemplate(Mongo mongo, String databaseName, String defaultCollectionName, MongoConverter mongoConverter, WriteConcern writeConcern) {
|
public MongoTemplate(Mongo mongo, String databaseName, String defaultCollectionName, MongoConverter mongoConverter, WriteConcern writeConcern, WriteResultChecking writeResultChecking) {
|
||||||
|
|
||||||
Assert.notNull(mongo);
|
Assert.notNull(mongo);
|
||||||
Assert.notNull(databaseName);
|
Assert.notNull(databaseName);
|
||||||
@@ -147,6 +156,9 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
|
|||||||
this.mongo = mongo;
|
this.mongo = mongo;
|
||||||
this.databaseName = databaseName;
|
this.databaseName = databaseName;
|
||||||
this.writeConcern = writeConcern;
|
this.writeConcern = writeConcern;
|
||||||
|
if (writeResultChecking != null) {
|
||||||
|
this.writeResultChecking = writeResultChecking;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -601,23 +613,25 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
|
|||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
* @see org.springframework.data.document.mongodb.MongoOperations#updateFirst(com.mongodb.DBObject, com.mongodb.DBObject)
|
* @see org.springframework.data.document.mongodb.MongoOperations#updateFirst(com.mongodb.DBObject, com.mongodb.DBObject)
|
||||||
*/
|
*/
|
||||||
public void updateFirst(QueryDefinition query, UpdateDefinition update) {
|
public WriteResult updateFirst(QueryDefinition query, UpdateDefinition update) {
|
||||||
updateFirst(getRequiredDefaultCollectionName(), query, update);
|
return updateFirst(getRequiredDefaultCollectionName(), query, update);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
* @see org.springframework.data.document.mongodb.MongoOperations#updateFirst(java.lang.String, com.mongodb.DBObject, com.mongodb.DBObject)
|
* @see org.springframework.data.document.mongodb.MongoOperations#updateFirst(java.lang.String, com.mongodb.DBObject, com.mongodb.DBObject)
|
||||||
*/
|
*/
|
||||||
public void updateFirst(String collectionName, final QueryDefinition query, final UpdateDefinition update) {
|
public WriteResult updateFirst(String collectionName, final QueryDefinition query, final UpdateDefinition update) {
|
||||||
execute(new CollectionCallback<Void>() {
|
return execute(new CollectionCallback<WriteResult>() {
|
||||||
public Void doInCollection(DBCollection collection) throws MongoException, DataAccessException {
|
public WriteResult doInCollection(DBCollection collection) throws MongoException, DataAccessException {
|
||||||
|
WriteResult wr;
|
||||||
if (writeConcern == null) {
|
if (writeConcern == null) {
|
||||||
collection.update(query.getQueryObject(), update.getUpdateObject());
|
wr = collection.update(query.getQueryObject(), update.getUpdateObject());
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
collection.update(query.getQueryObject(), update.getUpdateObject(), false, false, writeConcern);
|
wr = collection.update(query.getQueryObject(), update.getUpdateObject(), false, false, writeConcern);
|
||||||
}
|
}
|
||||||
return null;
|
handleAnyWriteResultErrors(wr, query.getQueryObject(), "update with '" +update.getUpdateObject() + "'");
|
||||||
|
return wr;
|
||||||
}
|
}
|
||||||
}, collectionName);
|
}, collectionName);
|
||||||
}
|
}
|
||||||
@@ -625,23 +639,25 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
|
|||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
* @see org.springframework.data.document.mongodb.MongoOperations#updateMulti(com.mongodb.DBObject, com.mongodb.DBObject)
|
* @see org.springframework.data.document.mongodb.MongoOperations#updateMulti(com.mongodb.DBObject, com.mongodb.DBObject)
|
||||||
*/
|
*/
|
||||||
public void updateMulti(QueryDefinition query, UpdateDefinition update) {
|
public WriteResult updateMulti(QueryDefinition query, UpdateDefinition update) {
|
||||||
updateMulti(getRequiredDefaultCollectionName(), query, update);
|
return updateMulti(getRequiredDefaultCollectionName(), query, update);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
* @see org.springframework.data.document.mongodb.MongoOperations#updateMulti(java.lang.String, com.mongodb.DBObject, com.mongodb.DBObject)
|
* @see org.springframework.data.document.mongodb.MongoOperations#updateMulti(java.lang.String, com.mongodb.DBObject, com.mongodb.DBObject)
|
||||||
*/
|
*/
|
||||||
public void updateMulti(String collectionName, final QueryDefinition query, final UpdateDefinition update) {
|
public WriteResult updateMulti(String collectionName, final QueryDefinition query, final UpdateDefinition update) {
|
||||||
execute(new CollectionCallback<Void>() {
|
return execute(new CollectionCallback<WriteResult>() {
|
||||||
public Void doInCollection(DBCollection collection) throws MongoException, DataAccessException {
|
public WriteResult doInCollection(DBCollection collection) throws MongoException, DataAccessException {
|
||||||
|
WriteResult wr = null;
|
||||||
if (writeConcern == null) {
|
if (writeConcern == null) {
|
||||||
collection.updateMulti(query.getQueryObject(), update.getUpdateObject());
|
wr = collection.updateMulti(query.getQueryObject(), update.getUpdateObject());
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
collection.update(query.getQueryObject(), update.getUpdateObject(), false, true, writeConcern);
|
wr = collection.update(query.getQueryObject(), update.getUpdateObject(), false, true, writeConcern);
|
||||||
}
|
}
|
||||||
return null;
|
handleAnyWriteResultErrors(wr, query.getQueryObject(), "update with '" +update.getUpdateObject() + "'");
|
||||||
|
return wr;
|
||||||
}
|
}
|
||||||
}, collectionName);
|
}, collectionName);
|
||||||
}
|
}
|
||||||
@@ -659,12 +675,14 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
|
|||||||
public void remove(String collectionName, final QueryDefinition query) {
|
public void remove(String collectionName, final QueryDefinition query) {
|
||||||
execute(new CollectionCallback<Void>() {
|
execute(new CollectionCallback<Void>() {
|
||||||
public Void doInCollection(DBCollection collection) throws MongoException, DataAccessException {
|
public Void doInCollection(DBCollection collection) throws MongoException, DataAccessException {
|
||||||
|
WriteResult wr = null;
|
||||||
if (writeConcern == null) {
|
if (writeConcern == null) {
|
||||||
collection.remove(query.getQueryObject());
|
wr = collection.remove(query.getQueryObject());
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
collection.remove(query.getQueryObject(), writeConcern);
|
wr = collection.remove(query.getQueryObject(), writeConcern);
|
||||||
}
|
}
|
||||||
|
handleAnyWriteResultErrors(wr, query.getQueryObject(), "remove");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}, collectionName);
|
}, collectionName);
|
||||||
@@ -794,7 +812,43 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
|
|||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Checks and handles any errors.
|
||||||
|
*
|
||||||
|
* TODO: current implementation logs errors - will be configurable to log warning, errors or
|
||||||
|
* throw exception in later versions
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private void handleAnyWriteResultErrors(WriteResult wr, DBObject query, String operation) {
|
||||||
|
if (WriteResultChecking.NONE == this.writeResultChecking) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String error = wr.getError();
|
||||||
|
int n = wr.getN();
|
||||||
|
if (error != null) {
|
||||||
|
String message = "Execution of '" + operation +
|
||||||
|
(query == null ? "" : "' using '" + query.toString() + "' query") + " failed: " + error;
|
||||||
|
if (WriteResultChecking.EXCEPTION == this.writeResultChecking) {
|
||||||
|
throw new DataIntegrityViolationException(message);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
LOGGER.error(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if(n == 0) {
|
||||||
|
String message = "Execution of '" + operation +
|
||||||
|
(query == null ? "" : "' using '" + query.toString() + "' query") + " did not succeed: 0 documents updated";
|
||||||
|
if (WriteResultChecking.EXCEPTION == this.writeResultChecking) {
|
||||||
|
throw new DataIntegrityViolationException(message);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
LOGGER.warn(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
* Tries to convert the given {@link RuntimeException} into a {@link DataAccessException} but returns the original
|
* Tries to convert the given {@link RuntimeException} into a {@link DataAccessException} but returns the original
|
||||||
* exception if the conversation failed. Thus allows safe rethrowing of the return value.
|
* exception if the conversation failed. Thus allows safe rethrowing of the return value.
|
||||||
*
|
*
|
||||||
@@ -802,7 +856,6 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
private RuntimeException potentiallyConvertRuntimeException(RuntimeException ex) {
|
private RuntimeException potentiallyConvertRuntimeException(RuntimeException ex) {
|
||||||
|
|
||||||
RuntimeException resolved = this.exceptionTranslator.translateExceptionIfPossible(ex);
|
RuntimeException resolved = this.exceptionTranslator.translateExceptionIfPossible(ex);
|
||||||
return resolved == null ? ex : resolved;
|
return resolved == null ? ex : resolved;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package org.springframework.data.document.mongodb;
|
||||||
|
|
||||||
|
public enum WriteResultChecking {
|
||||||
|
NONE, LOG, EXCEPTION
|
||||||
|
}
|
||||||
@@ -30,6 +30,10 @@ public class Update implements UpdateDefinition {
|
|||||||
|
|
||||||
private HashMap<String, Object> criteria = new LinkedHashMap<String, Object>();
|
private HashMap<String, Object> criteria = new LinkedHashMap<String, Object>();
|
||||||
|
|
||||||
|
public static Update startUpdate() {
|
||||||
|
return new Update();
|
||||||
|
}
|
||||||
|
|
||||||
public Update set(String key, Object value) {
|
public Update set(String key, Object value) {
|
||||||
criteria.put("$set", Collections.singletonMap(key, value));
|
criteria.put("$set", Collections.singletonMap(key, value));
|
||||||
return this;
|
return this;
|
||||||
|
|||||||
@@ -17,22 +17,30 @@ package org.springframework.data.document.mongodb;
|
|||||||
|
|
||||||
import static org.hamcrest.Matchers.hasItem;
|
import static org.hamcrest.Matchers.hasItem;
|
||||||
import static org.hamcrest.Matchers.is;
|
import static org.hamcrest.Matchers.is;
|
||||||
|
import static org.hamcrest.Matchers.endsWith;
|
||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.junit.rules.ExpectedException;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.dao.DataIntegrityViolationException;
|
||||||
import org.springframework.data.document.mongodb.builder.Query;
|
import org.springframework.data.document.mongodb.builder.Query;
|
||||||
|
import org.springframework.data.document.mongodb.builder.Update;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
|
import com.mongodb.WriteConcern;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Integration test for {@link MongoTemplate}.
|
* Integration test for {@link MongoTemplate}.
|
||||||
*
|
*
|
||||||
* @author Oliver Gierke
|
* @author Oliver Gierke
|
||||||
|
* @author Thomas Risberg
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration("classpath:infrastructure.xml")
|
@ContextConfiguration("classpath:infrastructure.xml")
|
||||||
@@ -41,6 +49,9 @@ public class MongoTemplateTests {
|
|||||||
@Autowired
|
@Autowired
|
||||||
MongoTemplate template;
|
MongoTemplate template;
|
||||||
|
|
||||||
|
@Rule
|
||||||
|
public ExpectedException thrown = ExpectedException.none();
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
template.dropCollection(template.getDefaultCollectionName());
|
template.dropCollection(template.getDefaultCollectionName());
|
||||||
@@ -50,6 +61,7 @@ public class MongoTemplateTests {
|
|||||||
public void insertsSimpleEntityCorrectly() throws Exception {
|
public void insertsSimpleEntityCorrectly() throws Exception {
|
||||||
|
|
||||||
Person person = new Person("Oliver");
|
Person person = new Person("Oliver");
|
||||||
|
person.setAge(25);
|
||||||
template.insert(person);
|
template.insert(person);
|
||||||
|
|
||||||
MongoConverter converter = template.getConverter();
|
MongoConverter converter = template.getConverter();
|
||||||
@@ -59,8 +71,27 @@ public class MongoTemplateTests {
|
|||||||
assertThat(result, hasItem(person));
|
assertThat(result, hasItem(person));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void updateFailure() throws Exception {
|
||||||
|
|
||||||
|
MongoTemplate mongoTemplate = new MongoTemplate(template.getDb().getMongo(), "test", "people",
|
||||||
|
new WriteConcern(), WriteResultChecking.EXCEPTION);
|
||||||
|
|
||||||
|
Person person = new Person("Oliver2");
|
||||||
|
person.setAge(25);
|
||||||
|
mongoTemplate.insert(person);
|
||||||
|
|
||||||
|
Query q = Query.startQueryWithCriteria("BOGUS").gt(22).end();
|
||||||
|
Update u = Update.startUpdate().set("firstName", "Sven");
|
||||||
|
thrown.expect(DataIntegrityViolationException.class);
|
||||||
|
thrown.expectMessage( endsWith("0 documents updated") );
|
||||||
|
mongoTemplate.updateFirst(q, u);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void simpleQuery() throws Exception {
|
public void simpleQuery() throws Exception {
|
||||||
Query.startQueryWithCriteria("name").is("Mary").and("age").lt(33).gt(22).end().skip(22).limit(20);
|
Query.startQueryWithCriteria("name").is("Mary").and("age").lt(33).gt(22).end().skip(22).limit(20);
|
||||||
|
// TODO: more tests
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,6 +23,8 @@ public class Person {
|
|||||||
|
|
||||||
private String firstName;
|
private String firstName;
|
||||||
|
|
||||||
|
private int age;
|
||||||
|
|
||||||
private Person friend;
|
private Person friend;
|
||||||
|
|
||||||
public Person() {
|
public Person() {
|
||||||
@@ -51,6 +53,14 @@ public class Person {
|
|||||||
this.firstName = firstName;
|
this.firstName = firstName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getAge() {
|
||||||
|
return age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAge(int age) {
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
public Person getFriend() {
|
public Person getFriend() {
|
||||||
return friend;
|
return friend;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user