|
|
|
|
@ -109,6 +109,7 @@ import com.mongodb.util.JSON;
@@ -109,6 +109,7 @@ import com.mongodb.util.JSON;
|
|
|
|
|
* @author Graeme Rocher |
|
|
|
|
* @author Mark Pollack |
|
|
|
|
* @author Oliver Gierke |
|
|
|
|
* @author Amol Nayak |
|
|
|
|
*/ |
|
|
|
|
public class MongoTemplate implements MongoOperations, ApplicationContextAware { |
|
|
|
|
|
|
|
|
|
@ -734,11 +735,13 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
@@ -734,11 +735,13 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
|
|
|
|
|
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); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
@ -757,11 +760,13 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
@@ -757,11 +760,13 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
|
|
|
|
|
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; |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
@ -788,11 +793,13 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
@@ -788,11 +793,13 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
|
|
|
|
|
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); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
@ -880,7 +887,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
@@ -880,7 +887,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Returns a {@link Query} for the given entity by its id. |
|
|
|
|
* |
|
|
|
|
* |
|
|
|
|
* @param object must not be {@literal null}. |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
@ -1171,7 +1178,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
@@ -1171,7 +1178,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Create the specified collection using the provided options |
|
|
|
|
* |
|
|
|
|
* |
|
|
|
|
* @param collectionName |
|
|
|
|
* @param collectionOptions |
|
|
|
|
* @return the collection that was created |
|
|
|
|
@ -1193,7 +1200,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
@@ -1193,7 +1200,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
|
|
|
|
|
* 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 |
|
|
|
|
@ -1218,7 +1225,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
@@ -1218,7 +1225,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
|
|
|
|
|
* 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 |
|
|
|
|
@ -1248,7 +1255,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
@@ -1248,7 +1255,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
|
|
|
|
|
* 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 |
|
|
|
|
@ -1287,7 +1294,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
@@ -1287,7 +1294,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
|
|
|
|
|
* 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. |
|
|
|
|
@ -1334,7 +1341,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
@@ -1334,7 +1341,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Populates the id property of the saved object, if it's not set already. |
|
|
|
|
* |
|
|
|
|
* |
|
|
|
|
* @param savedObject |
|
|
|
|
* @param id |
|
|
|
|
*/ |
|
|
|
|
@ -1387,7 +1394,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
@@ -1387,7 +1394,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
|
|
|
|
|
* <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 |
|
|
|
|
@ -1510,9 +1517,16 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
@@ -1510,9 +1517,16 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
|
|
|
|
|
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); |
|
|
|
|
|