Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
ec74185
init version implementation
Jul 4, 2012
116eda7
DATAMONGO-477 - Change upper bound of Guava dependency to 14.
rocketraman Jul 6, 2012
69493b6
DATAMONGO-475 - Fixed debug output in map-reduce operations.
odrotbohm Jul 16, 2012
83d0396
DATAMONGO-474 - Fixed criteria mapping for MongoTemplate.group(…).
odrotbohm Jul 16, 2012
5e81400
DATAMONGO-482 - Fixed typo in reference documentation.
odrotbohm Jul 16, 2012
3a3fefb
DATAMONGO-483 - Indexes now use the field name even if index name is …
odrotbohm Jul 16, 2012
0dcec2d
DATAMONGO-480 - Consider WriteResult for insert(…) and save(…) methods.
amolnayak311 Jul 10, 2012
fa7c1ea
DATAMONGO-476 - JavaConfig support for repositories.
odrotbohm Jul 6, 2012
c3b58c2
DATAMONGO-485 - Added test case to show complex id's are working.
odrotbohm Jul 17, 2012
73423c3
DATAMONGO-486 - Polished namespace implementation.
odrotbohm Jul 18, 2012
cdc1efe
DATAMONGO-489 - Ensure read collections get converted to appropriate …
odrotbohm Jul 23, 2012
281f8b3
DATAMONGO-490 - Fixed typos.
odrotbohm Jul 23, 2012
190422f
DATAMONGO-474 - Populating id's after save now inspects field only.
odrotbohm Jul 24, 2012
fc0fd14
DATAMONGO-491 - Upgrade to Spring Data Commons 1.4.0.M1.
odrotbohm Jul 24, 2012
7ae6d82
DATAMONGO-491 - Prepare 1.1.0.M2 release.
odrotbohm Jul 24, 2012
c91fb8c
DATAMONGO-491 - Release 1.1.0.M2.
spring-builds Jul 24, 2012
24e2f3b
DATAMONGO-491 - Prepare next development iteration.
spring-builds Jul 24, 2012
0ebfb90
DATAMONGO-493 - Added test case to show the described scenario is wor…
odrotbohm Jul 24, 2012
fff8998
DATAMONGO-493 - Fixed broken $ne handling in QueryMapper.
odrotbohm Jul 24, 2012
55a16a3
DATAMONGO-495 - Fixed debug output in MongoTemplate.doFind(…).
odrotbohm Jul 26, 2012
5e0bca9
DATAMONGO-494 - QueryMapper now forwards entity metadata into nested …
odrotbohm Jul 27, 2012
dd7222d
DATAMONGO-497 - Fixed reading of empty collections.
odrotbohm Jul 30, 2012
bf436cc
DATAMONGO-496 - AbstractMongoConfiguration now defaults mapping base …
odrotbohm Jul 30, 2012
5860a9d
DATAMONGO-499 - Fixed namespace reference to repository XSD.
odrotbohm Jul 31, 2012
697a96b
DATAMONGO-500 - Index creation is only done for the correct MappingCo…
odrotbohm Jul 31, 2012
4213be4
DATAMONGO-502 - QueryMapper now translates property names into field …
odrotbohm Aug 8, 2012
81d5bc8
DATAMONGO-472 - MongoQueryCreator now correctly translates Not keyword.
odrotbohm Aug 10, 2012
538bccd
DATAMONGO-476 - @EnableMongoRepositories is now inherited into sub-cl…
odrotbohm Aug 13, 2012
3cf7951
DATACMNS-212 - Apply refactorings in Spring Data Commons.
odrotbohm Aug 13, 2012
5e5df90
DATAMONGO-505 - Fixed handling of parameter binding of associations a…
odrotbohm Aug 15, 2012
9257f84
DATAMONGO-508 - Eagerly return DBRef creation if the given value alre…
odrotbohm Aug 15, 2012
b440b4e
DATAMONGO-509 - SimpleMongoRepository.exists(…) now avoids loading un…
odrotbohm Aug 15, 2012
c63dc00
DATAMONGO-510 - Criteria now only uses BasicDBList internally.
odrotbohm Aug 15, 2012
60769ef
DATAMONGO-511 - QueryMapper now maps associations correctly.
odrotbohm Aug 15, 2012
5a9aad9
DATAMONGO-506 - Added test case to show BasicQuery is working for nes…
odrotbohm Aug 15, 2012
ff7e897
DATACMNS-214 - Adapted API change in Spring Data Commons.
odrotbohm Aug 16, 2012
f00962a
DATAMONGO-519 - Make Spring 3.1.2.RELEASE default Spring dependency v…
odrotbohm Aug 20, 2012
cbdedd1
DATAMONGO-517 - Fixed complex keyword handling.
odrotbohm Aug 21, 2012
b412cf4
DATAMONGO-513 - Update to Spring Data Commons 1.4.0.RC1.
odrotbohm Aug 23, 2012
649572a
DATAMONGO-513 - Prepare changelog for 1.1.0.RC1.
odrotbohm Aug 24, 2012
0b91218
DATAMONGO-513 - Release 1.1.0.RC1.
spring-builds Aug 24, 2012
1c3b5c2
DATAMONGO-513 - Prepare next development iteration.
spring-builds Aug 24, 2012
95e44e2
DATAMONGO-526 - Polished README.md.
markpollack Aug 25, 2012
a5d7ce5
DATAMONGO-513 - Update to Spring Data Commons 1.4.0.BUILD-SNAPSHOT.
odrotbohm Aug 30, 2012
9bb3311
DATAMONGO-523 - Added test case to verify type alias detection.
odrotbohm Sep 3, 2012
3e4016c
DATAMONGO-279
noter Sep 4, 2012
58854af
DATAMONGO-521 - Added test case to show that repository AND query works.
odrotbohm Sep 3, 2012
1cf70f6
DATAMONGO-527 - Fixed Criteria.equals(…).
odrotbohm Sep 3, 2012
c5f5f64
DATAMONGO-279 - fix broken repository tests
noter Sep 4, 2012
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
DATAMONGO-480 - Consider WriteResult for insert(…) and save(…) methods.
  • Loading branch information
amolnayak311 authored and noter committed Sep 3, 2012
commit 0dcec2dda7c82c68c14e9a1c8e90a88ad9e7c27c
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
* @author Graeme Rocher
* @author Mark Pollack
* @author Oliver Gierke
* @author Amol Nayak
*/
public class MongoTemplate implements MongoOperations, ApplicationContextAware {

Expand Down Expand Up @@ -731,11 +732,13 @@ public Object doInCollection(DBCollection collection) throws MongoException, Dat
MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.INSERT, collectionName,
entityClass, dbDoc, null);
WriteConcern writeConcernToUse = prepareWriteConcern(mongoAction);
WriteResult wr;
if (writeConcernToUse == null) {
collection.insert(dbDoc);
wr = collection.insert(dbDoc);
} else {
collection.insert(dbDoc, writeConcernToUse);
wr = collection.insert(dbDoc, writeConcernToUse);
}
handleAnyWriteResultErrors(wr, dbDoc, "insert");
return dbDoc.get(ID);
}
});
Expand All @@ -754,11 +757,13 @@ public Void doInCollection(DBCollection collection) throws MongoException, DataA
MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.INSERT_LIST, collectionName, null,
null, null);
WriteConcern writeConcernToUse = prepareWriteConcern(mongoAction);
WriteResult wr;
if (writeConcernToUse == null) {
collection.insert(dbDocList);
wr = collection.insert(dbDocList);
} else {
collection.insert(dbDocList.toArray((DBObject[]) new BasicDBObject[dbDocList.size()]), writeConcernToUse);
wr = collection.insert(dbDocList.toArray((DBObject[]) new BasicDBObject[dbDocList.size()]), writeConcernToUse);
}
handleAnyWriteResultErrors(wr, null, "insert_list");
return null;
}
});
Expand All @@ -785,11 +790,13 @@ public Object doInCollection(DBCollection collection) throws MongoException, Dat
MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.SAVE, collectionName, entityClass,
dbDoc, null);
WriteConcern writeConcernToUse = prepareWriteConcern(mongoAction);
WriteResult wr;
if (writeConcernToUse == null) {
collection.save(dbDoc);
wr = collection.save(dbDoc);
} else {
collection.save(dbDoc, writeConcernToUse);
wr = collection.save(dbDoc, writeConcernToUse);
}
handleAnyWriteResultErrors(wr, dbDoc, "save");
return dbDoc.get(ID);
}
});
Expand Down Expand Up @@ -890,7 +897,7 @@ public void remove(Object object, String collection) {

/**
* Returns a {@link Query} for the given entity by its id.
*
*
* @param object must not be {@literal null}.
* @return
*/
Expand Down Expand Up @@ -1181,7 +1188,7 @@ protected <T> void maybeEmitEvent(MongoMappingEvent<T> event) {

/**
* Create the specified collection using the provided options
*
*
* @param collectionName
* @param collectionOptions
* @return the collection that was created
Expand All @@ -1203,7 +1210,7 @@ public DBCollection doInDB(DB db) throws MongoException, DataAccessException {
* Map the results of an ad-hoc query on the default MongoDB collection to an object using the template's converter
* <p/>
* 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
Expand All @@ -1228,7 +1235,7 @@ protected <T> T doFindOne(String collectionName, DBObject query, DBObject fields
* The query document is specified as a standard DBObject and so is the fields specification.
* <p/>
* Can be overridden by subclasses.
*
*
* @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
Expand Down Expand Up @@ -1258,7 +1265,7 @@ protected <S, T> List<T> doFind(String collectionName, DBObject query, DBObject
* Map the results of an ad-hoc query on the default MongoDB collection to a List using the template's converter.
* <p/>
* 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
Expand Down Expand Up @@ -1297,7 +1304,7 @@ protected DBObject convertToDbObject(CollectionOptions collectionOptions) {
* The first document that matches the query is returned and also removed from the collection in the database.
* <p/>
* 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 entityClass the parameterized type of the returned list.
Expand Down Expand Up @@ -1344,7 +1351,7 @@ protected <T> T doFindAndModify(String collectionName, DBObject query, DBObject

/**
* Populates the id property of the saved object, if it's not set already.
*
*
* @param savedObject
* @param id
*/
Expand Down Expand Up @@ -1397,7 +1404,7 @@ private DBCollection getAndPrepareCollection(DB db, String collectionName) {
* <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
Expand Down Expand Up @@ -1523,9 +1530,16 @@ protected void handleAnyWriteResultErrors(WriteResult wr, DBObject query, String
String error = wr.getError();

if (error != null) {

String message = String.format("Execution of %s%s failed: %s", operation, query == null ? "" : "' using '"
+ query.toString() + "' query", error);
String message;
if (operation.equals("insert") || operation.equals("save")) {
// assuming the insert operations will begin with insert string
message = String.format("Insert/Save for %s failed: %s", query, error);
} else if (operation.equals("insert_list")) {
message = String.format("Insert list failed: %s", error);
} else {
message = String.format("Execution of %s%s failed: %s", operation,
query == null ? "" : "' using '" + query.toString() + "' query", error);
}

if (WriteResultChecking.EXCEPTION == this.writeResultChecking) {
throw new DataIntegrityViolationException(message);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2011 the original author or authors.
* Copyright 2011-2012 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.
Expand Down Expand Up @@ -37,9 +37,9 @@
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.core.convert.converter.Converter;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.PersistenceConstructor;
import org.springframework.data.mongodb.InvalidMongoDbApiUsageException;
Expand Down Expand Up @@ -73,6 +73,7 @@
*
* @author Oliver Gierke
* @author Thomas Risberg
* @author Amol Nayak
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:infrastructure.xml")
Expand Down Expand Up @@ -163,6 +164,112 @@ public void bogusUpdateDoesNotTriggerException() throws Exception {
mongoTemplate.updateFirst(q, u, Person.class);
}

/**
* @see DATAMONGO-480
*/
@Test
public void throwsExceptionForDuplicateIds() {

MongoTemplate template = new MongoTemplate(factory);
template.setWriteResultChecking(WriteResultChecking.EXCEPTION);

Person person = new Person(new ObjectId(), "Amol");
person.setAge(28);

template.insert(person);

try {
template.insert(person);
fail("Expected DataIntegrityViolationException!");
} catch (DataIntegrityViolationException e) {
assertThat(e.getMessage(), containsString("E11000 duplicate key error index: database.person.$_id_ dup key:"));
}
}

/**
* @see DATAMONGO-480
*/
@Test
public void throwsExceptionForUpdateWithInvalidPushOperator() {

MongoTemplate template = new MongoTemplate(factory);
template.setWriteResultChecking(WriteResultChecking.EXCEPTION);

ObjectId id = new ObjectId();
Person person = new Person(id, "Amol");
person.setAge(28);

template.insert(person);

try {

Query query = new Query(Criteria.where("firstName").is("Amol"));
Update upd = new Update().push("age", 29);
template.updateFirst(query, upd, Person.class);
fail("Expected DataIntegrityViolationException!");

} catch (DataIntegrityViolationException e) {

assertThat(e.getMessage(),
is("Execution of update with '{ \"$push\" : { \"age\" : 29}}'' using '{ \"firstName\" : \"Amol\"}' "
+ "query failed: Cannot apply $push/$pushAll modifier to non-array"));
}
}

/**
* @see DATAMONGO-480
*/
@Test
public void throwsExceptionForIndexViolationIfConfigured() {

MongoTemplate template = new MongoTemplate(factory);
template.setWriteResultChecking(WriteResultChecking.EXCEPTION);
template.indexOps(Person.class).ensureIndex(new Index().on("firstName", Order.DESCENDING).unique());

Person person = new Person(new ObjectId(), "Amol");
person.setAge(28);

template.save(person);

person = new Person(new ObjectId(), "Amol");
person.setAge(28);

try {
template.save(person);
fail("Expected DataIntegrityViolationException!");
} catch (DataIntegrityViolationException e) {
assertThat(e.getMessage(),
containsString("E11000 duplicate key error index: database.person.$firstName_-1 dup key:"));
}
}

/**
* @see DATAMONGO-480
*/
@Test
public void rejectsDuplicateIdInInsertAll() {

MongoTemplate template = new MongoTemplate(factory);
template.setWriteResultChecking(WriteResultChecking.EXCEPTION);

ObjectId id = new ObjectId();
Person person = new Person(id, "Amol");
person.setAge(28);

List<Person> records = new ArrayList<Person>();
records.add(person);
records.add(person);

try {
template.insertAll(records);
fail("Expected DataIntegrityViolationException!");
} catch (DataIntegrityViolationException e) {
assertThat(
e.getMessage(),
startsWith("Insert list failed: E11000 duplicate key error index: database.person.$_id_ dup key: { : ObjectId"));
}
}

@Test
public void testEnsureIndex() throws Exception {

Expand Down Expand Up @@ -193,7 +300,7 @@ public void testEnsureIndex() throws Exception {
assertThat(dropDupes, is(true));

List<IndexInfo> indexInfoList = template.indexOps(Person.class).getIndexInfo();
System.out.println(indexInfoList);

assertThat(indexInfoList.size(), is(2));
IndexInfo ii = indexInfoList.get(1);
assertThat(ii.isUnique(), is(true));
Expand Down Expand Up @@ -939,7 +1046,7 @@ public void updatesDBRefsCorrectly() {
DBRef first = new DBRef(factory.getDb(), "foo", new ObjectId());
DBRef second = new DBRef(factory.getDb(), "bar", new ObjectId());

template.updateFirst(null, Update.update("dbRefs", Arrays.asList(first, second)), ClassWithDBRefs.class);
template.updateFirst(null, update("dbRefs", Arrays.asList(first, second)), ClassWithDBRefs.class);
}

class ClassWithDBRefs {
Expand Down Expand Up @@ -1102,7 +1209,7 @@ public void executesQueryWithNegatedRegexCorrectly() {
template.save(second);

Query query = query(where("field").not().regex("Matthews"));
System.out.println(query.getQueryObject());

List<Sample> result = template.find(query, Sample.class);
assertThat(result.size(), is(1));
assertThat(result.get(0).field, is("Beauford"));
Expand Down