* Convert the given runtime exception to an appropriate exception from the {@code org.springframework.dao} hierarchy.
* Return {@literal null} if no translation is appropriate: any other exception may have resulted from user code, and
* should not be translated.
@@ -45,20 +45,21 @@ public class CouchbaseExceptionTranslator implements PersistenceExceptionTransla
* Translate Couchbase specific exceptions to spring exceptions if possible.
*
* @param ex the exception to translate.
+ *
* @return the translated exception or null.
*/
- @Override
- public final DataAccessException translateExceptionIfPossible(final RuntimeException ex) {
+ @Override
+ public final DataAccessException translateExceptionIfPossible(final RuntimeException ex) {
- if (ex instanceof ConnectionException) {
- return new DataAccessResourceFailureException(ex.getMessage(), ex);
- }
-
- if (ex instanceof ObservedException
- || ex instanceof ObservedTimeoutException
- || ex instanceof ObservedModifiedException) {
- return new DataIntegrityViolationException(ex.getMessage(), ex);
- }
+ if (ex instanceof ConnectionException) {
+ return new DataAccessResourceFailureException(ex.getMessage(), ex);
+ }
+
+ if (ex instanceof ObservedException
+ || ex instanceof ObservedTimeoutException
+ || ex instanceof ObservedModifiedException) {
+ return new DataIntegrityViolationException(ex.getMessage(), ex);
+ }
if (ex instanceof CancellationException) {
throw new OperationCancellationException(ex.getMessage(), ex);
@@ -67,8 +68,9 @@ public class CouchbaseExceptionTranslator implements PersistenceExceptionTransla
if (ex instanceof InvalidViewException) {
throw new InvalidDataAccessResourceUsageException(ex.getMessage(), ex);
}
-
- return null;
- }
+
+ // Unable to translate exception, therefore just throw the original!
+ throw ex;
+ }
}
diff --git a/src/main/java/org/springframework/data/couchbase/core/CouchbaseOperations.java b/src/main/java/org/springframework/data/couchbase/core/CouchbaseOperations.java
index ff812dcb..694a35ba 100644
--- a/src/main/java/org/springframework/data/couchbase/core/CouchbaseOperations.java
+++ b/src/main/java/org/springframework/data/couchbase/core/CouchbaseOperations.java
@@ -33,79 +33,80 @@ public interface CouchbaseOperations {
/**
* Save the given object.
- *
+ *
*
When the document already exists (specified by its unique id), then it will be overriden. Otherwise it will be
* created.
*
* @param objectToSave the object to store in the bucket.
*/
void save(Object objectToSave);
-
+
/**
* Save a list of objects.
- *
+ *
*
When one of the documents already exists (specified by its unique id), then it will be overriden. Otherwise it
* will be created.
*
* @param batchToSave the list of objects to store in the bucket.
*/
- void save(Collection extends Object> batchToSave);
-
+ void save(Collection> batchToSave);
+
/**
* Insert the given object.
- *
+ *
*
When the document already exists (specified by its unique id), then it will not be overriden. Use the
* {@link CouchbaseOperations#save} method for this task.
*
* @param objectToSave the object to add to the bucket.
*/
void insert(Object objectToSave);
-
+
/**
* Insert a list of objects.
- *
+ *
*
When one of the documents already exists (specified by its unique id), then it will not be overriden. Use the
* {@link CouchbaseOperations#save} method for this.
*
* @param batchToSave the list of objects to add to the bucket.
*/
- void insert(Collection extends Object> batchToSave);
+ void insert(Collection> batchToSave);
/**
* Update the given object.
- *
+ *
*
When the document does not exist (specified by its unique id) it will not be created. Use the
* {@link CouchbaseOperations#save} method for this.
*
* @param objectToSave the object to add to the bucket.
*/
void update(Object objectToSave);
-
+
/**
* Insert a list of objects.
- *
+ *
*
If one of the documents does not exist (specified by its unique id), then it will not be created. Use the
* {@link CouchbaseOperations#save} method for this.
*
* @param batchToSave the list of objects to add to the bucket.
*/
- void update(Collection extends Object> batchToSave);
-
+ void update(Collection> batchToSave);
+
/**
* Find an object by its given Id and map it to the corresponding entity.
*
* @param id the unique ID of the document.
* @param entityClass the entity to map to.
+ *
* @return returns the found object or null otherwise.
*/
T findById(String id, Class entityClass);
/**
* Query a View for a list of documents of type T.
- *
+ *
*
There is no need to {@link Query#setIncludeDocs(boolean)} explicitely, because it will be set to true all the
* time. It is valid to pass in a empty constructed {@link Query} object.
- *
+ *
*
This method does not work with reduced views, because they by design do not contain references to original
* objects. Use the provided {@link #queryView} method for more flexibility and direct access.
*
@@ -113,6 +114,7 @@ public interface CouchbaseOperations {
* @param view the name of the viewName.
* @param query the Query object to customize the viewName query.
* @param entityClass the entity to map to.
+ *
* @return the converted collection
*/
List findByView(String design, String view, Query query, Class entityClass);
@@ -120,17 +122,16 @@ public interface CouchbaseOperations {
/**
* Query a View with direct access to the {@link ViewResponse}.
- *
*
This method is available to ease the working with views by still wrapping exceptions into the Spring
* infrastructure.
- *
*
It is especially needed if you want to run reduced viewName queries, because they can't be mapped onto entities
* directly.
*
* @param design the name of the designDocument document.
* @param view the name of the viewName.
* @param query the Query object to customize the viewName query.
- * @return
+ *
+ * @return ViewResponse containing the results of the query.
*/
ViewResponse queryView(String design, String view, Query query);
@@ -138,13 +139,14 @@ public interface CouchbaseOperations {
* Checks if the given document exists.
*
* @param id the unique ID of the document.
+ *
* @return whether the document could be found or not.
*/
boolean exists(String id);
/**
* Remove the given object from the bucket by id.
- *
+ *
* If the object is a String, it will be treated as the document key
* directly.
*
@@ -157,22 +159,24 @@ public interface CouchbaseOperations {
*
* @param batchToRemove the list of Objects to remove.
*/
- void remove(Collection extends Object> batchToRemove);
+ void remove(Collection> batchToRemove);
/**
* Executes a BucketCallback translating any exceptions as necessary.
- *
+ *
* Allows for returning a result object, that is a domain object or a collection of domain objects.
*
* @param action the action to execute in the callback.
* @param the return type.
- * @return
+ *
+ * @return the return type.
*/
T execute(BucketCallback action);
/**
* Returns the underlying {@link CouchbaseConverter}.
- * @return
+ *
+ * @return CouchbaseConverter.
*/
CouchbaseConverter getConverter();
diff --git a/src/main/java/org/springframework/data/couchbase/core/CouchbaseTemplate.java b/src/main/java/org/springframework/data/couchbase/core/CouchbaseTemplate.java
index 02ea0ecf..60251763 100644
--- a/src/main/java/org/springframework/data/couchbase/core/CouchbaseTemplate.java
+++ b/src/main/java/org/springframework/data/couchbase/core/CouchbaseTemplate.java
@@ -26,11 +26,20 @@ import org.springframework.dao.QueryTimeoutException;
import org.springframework.data.couchbase.core.convert.CouchbaseConverter;
import org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter;
import org.springframework.data.couchbase.core.convert.translation.JacksonTranslationService;
-import org.springframework.data.couchbase.core.convert.translation.TranslationService;
-import org.springframework.data.couchbase.core.mapping.*;
+import org.springframework.data.couchbase.core.mapping.CouchbaseDocument;
+import org.springframework.data.couchbase.core.mapping.CouchbaseMappingContext;
+import org.springframework.data.couchbase.core.mapping.CouchbasePersistentEntity;
+import org.springframework.data.couchbase.core.mapping.CouchbasePersistentProperty;
+import org.springframework.data.couchbase.core.mapping.CouchbaseStorable;
import org.springframework.data.mapping.context.MappingContext;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
@@ -41,36 +50,32 @@ public class CouchbaseTemplate implements CouchbaseOperations {
private CouchbaseClient client;
private CouchbaseConverter couchbaseConverter;
- protected final MappingContext extends CouchbasePersistentEntity>,
- CouchbasePersistentProperty> mappingContext;
+ protected final MappingContext extends CouchbasePersistentEntity>, CouchbasePersistentProperty> mappingContext;
private static final Collection ITERABLE_CLASSES;
- private final CouchbaseExceptionTranslator exceptionTranslator =
- new CouchbaseExceptionTranslator();
- private final TranslationService