diff --git a/.gitignore b/.gitignore index bc8404d7b..0c8e01300 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,7 @@ target/ .classpath .project .settings/* + +.idea +*.iml +out 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 6b4deccbf..a4e62fdfe 100644 --- a/src/main/java/org/springframework/data/couchbase/core/CouchbaseOperations.java +++ b/src/main/java/org/springframework/data/couchbase/core/CouchbaseOperations.java @@ -40,7 +40,7 @@ public interface CouchbaseOperations { * @param objectToSave the object to store in the bucket. */ void save(Object objectToSave); - + /** * Save a list of objects. * @@ -49,8 +49,8 @@ public interface CouchbaseOperations { * * @param batchToSave the list of objects to store in the bucket. */ - void save(Collection batchToSave); - + void save(Collection batchToSave); + /** * Insert the given object. * @@ -60,7 +60,7 @@ public interface CouchbaseOperations { * @param objectToSave the object to add to the bucket. */ void insert(Object objectToSave); - + /** * Insert a list of objects. * @@ -80,7 +80,7 @@ public interface CouchbaseOperations { * @param objectToSave the object to add to the bucket. */ void update(Object objectToSave); - + /** * Insert a list of objects. * @@ -90,7 +90,7 @@ public interface CouchbaseOperations { * @param batchToSave the list of objects to add to the bucket. */ void update(Collection batchToSave); - + /** * Find an object by its given Id and map it to the corresponding entity. * @@ -103,8 +103,9 @@ public interface CouchbaseOperations { /** * 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.

+ *

{@link Query#setIncludeDocs(boolean)} defaults to false. If you require the query to include the entire + * document, you will need to set it to true before calling this method. 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.

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 02ea0ecf7..058d59a02 100644 --- a/src/main/java/org/springframework/data/couchbase/core/CouchbaseTemplate.java +++ b/src/main/java/org/springframework/data/couchbase/core/CouchbaseTemplate.java @@ -44,17 +44,17 @@ public class CouchbaseTemplate implements CouchbaseOperations { protected final MappingContext, CouchbasePersistentProperty> mappingContext; private static final Collection ITERABLE_CLASSES; - private final CouchbaseExceptionTranslator exceptionTranslator = - new CouchbaseExceptionTranslator(); + private final CouchbaseExceptionTranslator exceptionTranslator = + new CouchbaseExceptionTranslator(); private final TranslationService translationService; - - static { - Set iterableClasses = new HashSet(); - iterableClasses.add(List.class.getName()); - iterableClasses.add(Collection.class.getName()); - iterableClasses.add(Iterator.class.getName()); - ITERABLE_CLASSES = Collections.unmodifiableCollection(iterableClasses); - } + + static { + Set iterableClasses = new HashSet(); + iterableClasses.add(List.class.getName()); + iterableClasses.add(Collection.class.getName()); + iterableClasses.add(Iterator.class.getName()); + ITERABLE_CLASSES = Collections.unmodifiableCollection(iterableClasses); + } public CouchbaseTemplate(final CouchbaseClient client) { this(client, null); @@ -84,7 +84,7 @@ private CouchbaseStorable translateDecode(final String source, final CouchbaseSt } public final void insert(final Object objectToSave) { - ensureNotIterable(objectToSave); + ensureNotIterable(objectToSave); final CouchbaseDocument converted = new CouchbaseDocument(); couchbaseConverter.write(objectToSave, converted); @@ -96,16 +96,16 @@ public Boolean doInBucket() throws InterruptedException, ExecutionException { } }); } - + public final void insert(final Collection batchToSave) { - Iterator iter = batchToSave.iterator(); - while (iter.hasNext()) { - insert(iter.next()); - } + Iterator iter = batchToSave.iterator(); + while (iter.hasNext()) { + insert(iter.next()); + } } public void save(final Object objectToSave) { - ensureNotIterable(objectToSave); + ensureNotIterable(objectToSave); final CouchbaseDocument converted = new CouchbaseDocument(); couchbaseConverter.write(objectToSave, converted); @@ -117,7 +117,7 @@ public Boolean doInBucket() throws InterruptedException, ExecutionException { } }); } - + public void save(final Collection batchToSave) { Iterator iter = batchToSave.iterator(); while (iter.hasNext()) { @@ -126,7 +126,7 @@ public void save(final Collection batchToSave) { } public void update(final Object objectToSave) { - ensureNotIterable(objectToSave); + ensureNotIterable(objectToSave); final CouchbaseDocument converted = new CouchbaseDocument(); couchbaseConverter.write(objectToSave, converted); @@ -139,14 +139,14 @@ public Boolean doInBucket() throws InterruptedException, ExecutionException { }); } - + public void update(final Collection batchToSave) { - Iterator iter = batchToSave.iterator(); - while (iter.hasNext()) { - save(iter.next()); - } + Iterator iter = batchToSave.iterator(); + while (iter.hasNext()) { + save(iter.next()); + } } - + public final T findById(final String id, final Class entityClass) { String result = execute(new BucketCallback() { @Override @@ -155,12 +155,12 @@ public String doInBucket() { } }); - if (result == null) { - return null; - } + if (result == null) { + return null; + } CouchbaseDocument converted = new CouchbaseDocument(id); - return couchbaseConverter.read(entityClass, (CouchbaseDocument) translateDecode(result, converted)); + return couchbaseConverter.read(entityClass, (CouchbaseDocument) translateDecode(result, converted)); } @@ -168,9 +168,10 @@ public String doInBucket() { public List findByView(final String designName, final String viewName, final Query query, final Class entityClass) { - if (!query.willIncludeDocs()) { - query.setIncludeDocs(true); + if(query.willIncludeDocs()) { + query.setIncludeDocs(true); } + if (query.willReduce()) { query.setReduce(false); } @@ -261,18 +262,18 @@ public String doInBucket() { * * @param o the object to verify. */ - protected final void ensureNotIterable(Object o) { - if (null != o) { - if (o.getClass().isArray() || ITERABLE_CLASSES.contains(o.getClass().getName())) { - throw new IllegalArgumentException("Cannot use a collection here."); - } - } - } - - private RuntimeException potentiallyConvertRuntimeException(final RuntimeException ex) { - RuntimeException resolved = exceptionTranslator.translateExceptionIfPossible(ex); - return resolved == null ? ex : resolved; - } + protected final void ensureNotIterable(Object o) { + if (null != o) { + if (o.getClass().isArray() || ITERABLE_CLASSES.contains(o.getClass().getName())) { + throw new IllegalArgumentException("Cannot use a collection here."); + } + } + } + + private RuntimeException potentiallyConvertRuntimeException(final RuntimeException ex) { + RuntimeException resolved = exceptionTranslator.translateExceptionIfPossible(ex); + return resolved == null ? ex : resolved; + } @Override public CouchbaseConverter getConverter() { diff --git a/src/main/java/org/springframework/data/couchbase/repository/support/SimpleCouchbaseRepository.java b/src/main/java/org/springframework/data/couchbase/repository/support/SimpleCouchbaseRepository.java index a1adefa43..3cef11ee9 100644 --- a/src/main/java/org/springframework/data/couchbase/repository/support/SimpleCouchbaseRepository.java +++ b/src/main/java/org/springframework/data/couchbase/repository/support/SimpleCouchbaseRepository.java @@ -119,7 +119,7 @@ public Iterable findAll() { String design = entityInformation.getJavaType().getSimpleName().toLowerCase(); String view = "all"; - return couchbaseOperations.findByView(design, view, new Query().setReduce(false), + return couchbaseOperations.findByView(design, view, new Query().setReduce(false).setIncludeDocs(true), entityInformation.getJavaType()); } diff --git a/src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateTests.java b/src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateTests.java index 8e52c7762..203249864 100644 --- a/src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateTests.java +++ b/src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateTests.java @@ -68,12 +68,12 @@ public void saveSimpleEntityCorrectly() throws Exception { @Test public void saveDocumentWithExpiry() throws Exception { - String id = "simple-doc-with-expiry"; - DocumentWithExpiry doc = new DocumentWithExpiry(id); - template.save(doc); - assertNotNull(client.get(id)); - Thread.sleep(3000); - assertNull(client.get(id)); + String id = "simple-doc-with-expiry"; + DocumentWithExpiry doc = new DocumentWithExpiry(id); + template.save(doc); + assertNotNull(client.get(id)); + Thread.sleep(3000); + assertNull(client.get(id)); } @Test @@ -96,10 +96,10 @@ public void insertDoesNotOverride() { @Test public void updateDoesNotInsert() { - String id ="update-does-not-insert"; - SimplePerson doc = new SimplePerson(id, "Nice Guy"); - template.update(doc); - assertNull(client.get(id)); + String id ="update-does-not-insert"; + SimplePerson doc = new SimplePerson(id, "Nice Guy"); + template.update(doc); + assertNull(client.get(id)); } @@ -168,6 +168,7 @@ public void validFindById() { @Test public void shouldLoadAndMapViewDocs() { Query query = new Query(); + query.setIncludeDocs(true); query.setStale(Stale.FALSE); final List beers = template.findByView("test_beers", "by_name", query, Beer.class); @@ -179,7 +180,7 @@ public void shouldLoadAndMapViewDocs() { assertNotNull(beer.getActive()); } } - + /** * A sample document with just an id and property. */ @@ -191,11 +192,11 @@ static class SimplePerson { private final String name; public SimplePerson(String id, String name) { - this.id = id; - this.name = name; - } + this.id = id; + this.name = name; + } } - + /** * A sample document that expires in 2 seconds. */ @@ -203,9 +204,9 @@ public SimplePerson(String id, String name) { static class DocumentWithExpiry { @Id private final String id; - + public DocumentWithExpiry(String id) { - this.id = id; + this.id = id; } } diff --git a/src/test/java/org/springframework/data/couchbase/repository/SimpleCouchbaseRepositoryTests.java b/src/test/java/org/springframework/data/couchbase/repository/SimpleCouchbaseRepositoryTests.java index 90883dae2..5dadb8f06 100644 --- a/src/test/java/org/springframework/data/couchbase/repository/SimpleCouchbaseRepositoryTests.java +++ b/src/test/java/org/springframework/data/couchbase/repository/SimpleCouchbaseRepositoryTests.java @@ -77,7 +77,7 @@ public void simpleCrud() { @Test public void shouldFindAll() { // do a non-stale query to populate data for testing. - client.query(client.getView("user", "all"), new Query().setStale(Stale.FALSE)); + client.query(client.getView("user", "all"), new Query().setIncludeDocs(true).setStale(Stale.FALSE)); Iterable allUsers = repository.findAll(); int size = 0;