DATACOUCH-625 - Save doesn't return updated entity if immutable. (#274)
Modified applyUpdatedCas() to return accessor.getBean() which will occur if the version property is Immutable. Created applyUpdateId() that is analogous to applyUpdatedCas() Factored out the equals() method that was used by a number of test entity objects. Co-authored-by: mikereiche <michael.reiche@couchbase.com>
This commit is contained in:
@@ -89,14 +89,28 @@ class CouchbaseTemplateSupport implements ApplicationContextAware {
|
||||
return accessor.getBean();
|
||||
}
|
||||
|
||||
public void applyUpdatedCas(final Object entity, final long cas) {
|
||||
public Object applyUpdatedCas(final Object entity, final long cas) {
|
||||
final ConvertingPropertyAccessor<Object> accessor = getPropertyAccessor(entity);
|
||||
final CouchbasePersistentEntity<?> persistentEntity = mappingContext.getRequiredPersistentEntity(entity.getClass());
|
||||
final CouchbasePersistentProperty versionProperty = persistentEntity.getVersionProperty();
|
||||
|
||||
if (versionProperty != null) {
|
||||
accessor.setProperty(versionProperty, cas);
|
||||
return accessor.getBean();
|
||||
}
|
||||
return entity;
|
||||
}
|
||||
|
||||
public Object applyUpdatedId(final Object entity, Object id) {
|
||||
final ConvertingPropertyAccessor<Object> accessor = getPropertyAccessor(entity);
|
||||
final CouchbasePersistentEntity<?> persistentEntity = mappingContext.getRequiredPersistentEntity(entity.getClass());
|
||||
final CouchbasePersistentProperty idProperty = persistentEntity.getIdProperty();
|
||||
|
||||
if (idProperty != null) {
|
||||
accessor.setProperty(idProperty, id);
|
||||
return accessor.getBean();
|
||||
}
|
||||
return entity;
|
||||
}
|
||||
|
||||
public long getCas(final Object entity) {
|
||||
@@ -106,9 +120,9 @@ class CouchbaseTemplateSupport implements ApplicationContextAware {
|
||||
|
||||
long cas = 0;
|
||||
if (versionProperty != null) {
|
||||
Object casObject = (Number)accessor.getProperty(versionProperty);
|
||||
if (casObject instanceof Number){
|
||||
cas = ((Number)casObject).longValue();
|
||||
Object casObject = (Number) accessor.getProperty(versionProperty);
|
||||
if (casObject instanceof Number) {
|
||||
cas = ((Number) casObject).longValue();
|
||||
}
|
||||
}
|
||||
return cas;
|
||||
|
||||
@@ -73,8 +73,8 @@ public class ReactiveInsertByIdOperationSupport implements ReactiveInsertByIdOpe
|
||||
CouchbaseDocument converted = template.support().encodeEntity(o);
|
||||
return template.getCollection(collection).reactive()
|
||||
.insert(converted.getId(), converted.export(), buildInsertOptions()).map(result -> {
|
||||
template.support().applyUpdatedCas(object, result.cas());
|
||||
return o;
|
||||
Object updatedObject = template.support().applyUpdatedId(o, converted.getId());
|
||||
return (T) template.support().applyUpdatedCas(updatedObject, result.cas());
|
||||
});
|
||||
}).onErrorMap(throwable -> {
|
||||
if (throwable instanceof RuntimeException) {
|
||||
@@ -97,7 +97,7 @@ public class ReactiveInsertByIdOperationSupport implements ReactiveInsertByIdOpe
|
||||
} else if (durabilityLevel != DurabilityLevel.NONE) {
|
||||
options.durability(durabilityLevel);
|
||||
}
|
||||
if (expiry != null && ! expiry.isZero()) {
|
||||
if (expiry != null && !expiry.isZero()) {
|
||||
options.expiry(expiry);
|
||||
} else if (domainType.isAnnotationPresent(Document.class)) {
|
||||
Document documentAnn = domainType.getAnnotation(Document.class);
|
||||
@@ -110,26 +110,30 @@ public class ReactiveInsertByIdOperationSupport implements ReactiveInsertByIdOpe
|
||||
@Override
|
||||
public TerminatingInsertById<T> inCollection(final String collection) {
|
||||
Assert.hasText(collection, "Collection must not be null nor empty.");
|
||||
return new ReactiveInsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo, durabilityLevel, expiry);
|
||||
return new ReactiveInsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo, durabilityLevel,
|
||||
expiry);
|
||||
}
|
||||
|
||||
@Override
|
||||
public InsertByIdWithCollection<T> withDurability(final DurabilityLevel durabilityLevel) {
|
||||
Assert.notNull(durabilityLevel, "Durability Level must not be null.");
|
||||
return new ReactiveInsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo, durabilityLevel, expiry);
|
||||
return new ReactiveInsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo, durabilityLevel,
|
||||
expiry);
|
||||
}
|
||||
|
||||
@Override
|
||||
public InsertByIdWithCollection<T> withDurability(final PersistTo persistTo, final ReplicateTo replicateTo) {
|
||||
Assert.notNull(persistTo, "PersistTo must not be null.");
|
||||
Assert.notNull(replicateTo, "ReplicateTo must not be null.");
|
||||
return new ReactiveInsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo, durabilityLevel, expiry);
|
||||
return new ReactiveInsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo, durabilityLevel,
|
||||
expiry);
|
||||
}
|
||||
|
||||
@Override
|
||||
public InsertByIdWithDurability<T> withExpiry(final Duration expiry) {
|
||||
Assert.notNull(expiry, "expiry must not be null.");
|
||||
return new ReactiveInsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo, durabilityLevel, expiry);
|
||||
return new ReactiveInsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo, durabilityLevel,
|
||||
expiry);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package org.springframework.data.couchbase.core;
|
||||
|
||||
import org.springframework.data.couchbase.core.mapping.CouchbasePersistentEntity;
|
||||
import org.springframework.data.couchbase.core.mapping.CouchbasePersistentProperty;
|
||||
import org.springframework.data.couchbase.core.mapping.Document;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
@@ -73,8 +75,8 @@ public class ReactiveUpsertByIdOperationSupport implements ReactiveUpsertByIdOpe
|
||||
CouchbaseDocument converted = template.support().encodeEntity(o);
|
||||
return template.getCollection(collection).reactive()
|
||||
.upsert(converted.getId(), converted.export(), buildUpsertOptions()).map(result -> {
|
||||
template.support().applyUpdatedCas(object, result.cas());
|
||||
return o;
|
||||
Object updatedObject = template.support().applyUpdatedId(o, converted.getId());
|
||||
return (T) template.support().applyUpdatedCas(updatedObject, result.cas());
|
||||
});
|
||||
}).onErrorMap(throwable -> {
|
||||
if (throwable instanceof RuntimeException) {
|
||||
|
||||
@@ -541,6 +541,7 @@ public class MappingCouchbaseConverter extends AbstractCouchbaseConverter implem
|
||||
generatedValueInfo = idProperty.findAnnotation(GeneratedValue.class);
|
||||
String generatedId = generateId(generatedValueInfo, prefixes, suffixes, idAttributes);
|
||||
target.setId(generatedId);
|
||||
// this is not effective if id is Immutable, and accessor.setProperty() returns a new object in getBean()
|
||||
accessor.setProperty(idProperty, generatedId);
|
||||
} else {
|
||||
target.setId(id);
|
||||
|
||||
Reference in New Issue
Block a user