use of org.springframework.dao.DataAccessException in project spring-data-mongodb by spring-projects.
the class MongoExceptionTranslatorUnitTests method translateToUncategorizedMongoDbException.
@Test
public void translateToUncategorizedMongoDbException() {
MongoException exception = new MongoException(0, "");
DataAccessException translatedException = translator.translateExceptionIfPossible(exception);
expectExceptionWithCauseMessage(translatedException, UncategorizedMongoDbException.class);
}
use of org.springframework.dao.DataAccessException in project spring-data-mongodb by spring-projects.
the class MongoExceptionTranslatorUnitTests method translateMongoInternalException.
@Test
public void translateMongoInternalException() {
MongoInternalException exception = new MongoInternalException("Internal exception");
DataAccessException translatedException = translator.translateExceptionIfPossible(exception);
expectExceptionWithCauseMessage(translatedException, InvalidDataAccessResourceUsageException.class);
}
use of org.springframework.dao.DataAccessException in project spring-data-mongodb by spring-projects.
the class MongoTemplateTests method shouldExecuteQueryShouldMapQueryBeforeQueryExecution.
// DATAMONGO-816
@Test
public void shouldExecuteQueryShouldMapQueryBeforeQueryExecution() {
ObjectWithEnumValue o = new ObjectWithEnumValue();
o.value = EnumValue.VALUE2;
template.save(o);
Query q = Query.query(Criteria.where("value").in(EnumValue.VALUE2));
template.executeQuery(q, StringUtils.uncapitalize(ObjectWithEnumValue.class.getSimpleName()), new DocumentCallbackHandler() {
@Override
public void processDocument(org.bson.Document document) throws MongoException, DataAccessException {
assertThat(document, is(notNullValue()));
ObjectWithEnumValue result = template.getConverter().read(ObjectWithEnumValue.class, document);
assertThat(result.value, is(EnumValue.VALUE2));
}
});
}
use of org.springframework.dao.DataAccessException in project spring-data-mongodb by spring-projects.
the class MongoTemplate method doRemove.
protected <T> DeleteResult doRemove(final String collectionName, final Query query, @Nullable final Class<T> entityClass) {
Assert.notNull(query, "Query must not be null!");
Assert.hasText(collectionName, "Collection name must not be null or empty!");
final MongoPersistentEntity<?> entity = getPersistentEntity(entityClass);
final Document queryObject = queryMapper.getMappedObject(query.getQueryObject(), entity);
return execute(collectionName, new CollectionCallback<DeleteResult>() {
public DeleteResult doInCollection(MongoCollection<Document> collection) throws MongoException, DataAccessException {
maybeEmitEvent(new BeforeDeleteEvent<T>(queryObject, entityClass, collectionName));
Document removeQuery = queryObject;
DeleteOptions options = new DeleteOptions();
query.getCollation().map(Collation::toMongoCollation).ifPresent(options::collation);
MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.REMOVE, collectionName, entityClass, null, queryObject);
WriteConcern writeConcernToUse = prepareWriteConcern(mongoAction);
DeleteResult dr = null;
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Remove using query: {} in collection: {}.", new Object[] { serializeToJsonSafely(removeQuery), collectionName });
}
if (query.getLimit() > 0 || query.getSkip() > 0) {
MongoCursor<Document> cursor = new QueryCursorPreparer(query, entityClass).prepare(collection.find(removeQuery).projection(new Document(ID_FIELD, 1))).iterator();
Set<Object> ids = new LinkedHashSet<>();
while (cursor.hasNext()) {
ids.add(cursor.next().get(ID_FIELD));
}
removeQuery = new Document(ID_FIELD, new Document("$in", ids));
}
if (writeConcernToUse == null) {
dr = collection.deleteMany(removeQuery, options);
} else {
dr = collection.withWriteConcern(writeConcernToUse).deleteMany(removeQuery, options);
}
maybeEmitEvent(new AfterDeleteEvent<T>(queryObject, entityClass, collectionName));
return dr;
}
});
}
use of org.springframework.dao.DataAccessException in project spring-data-mongodb by spring-projects.
the class MongoTemplate method executeCommand.
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.MongoOperations#executeCommand(org.bson.Document)
*/
@Override
public Document executeCommand(final Document command) {
Assert.notNull(command, "Command must not be null!");
Document result = execute(new DbCallback<Document>() {
public Document doInDB(MongoDatabase db) throws MongoException, DataAccessException {
return db.runCommand(command, Document.class);
}
});
return result;
}
Aggregations