Accept entity parameters to repository methods. (#1595)

Closed #1586.
This commit is contained in:
Michael Reiche
2022-10-31 06:34:02 -10:00
committed by GitHub
parent 832f314953
commit 932901e709
5 changed files with 66 additions and 16 deletions

View File

@@ -16,18 +16,21 @@
package org.springframework.data.couchbase.core.convert;
import java.util.Collection;
import java.util.Collections;
import com.couchbase.client.java.query.QueryScanConsistency;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.data.convert.CustomConversions;
import org.springframework.data.couchbase.core.mapping.CouchbaseDocument;
import org.springframework.data.couchbase.core.mapping.CouchbaseList;
import org.springframework.data.couchbase.core.mapping.CouchbasePersistentProperty;
import org.springframework.data.mapping.model.ConvertingPropertyAccessor;
import org.springframework.data.mapping.model.EntityInstantiators;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.TypeInformation;
/**
* An abstract {@link CouchbaseConverter} that provides the basics for the {@link MappingCouchbaseConverter}.
@@ -141,19 +144,34 @@ public abstract class AbstractCouchbaseConverter implements CouchbaseConverter,
* This convertForWriteIfNeed takes only the value to convert. It cannot access the annotations of the Field being
* converted.
*
* @param value the value to be converted to the class that would actually be stored.
* @param inValue the value to be converted to the class that would actually be stored.
* @return
*/
@Override
public Object convertForWriteIfNeeded(Object value) {
if (value == null) {
public Object convertForWriteIfNeeded(Object inValue) {
if (inValue == null) {
return null;
}
return this.conversions.getCustomWriteTarget(value.getClass()) //
.map(it -> (Object) this.conversionService.convert(value, it)) //
.orElseGet(() -> Enum.class.isAssignableFrom(value.getClass()) ? ((Enum<?>) value).name() : value);
Object value = this.conversions.getCustomWriteTarget(inValue.getClass()) //
.map(it -> (Object) this.conversionService.convert(inValue, it)) //
.orElse(inValue);
Class<?> elementType = value.getClass();
if (elementType == null || conversions.isSimpleType(elementType)) {
value = Enum.class.isAssignableFrom(value.getClass()) ? ((Enum<?>) value).name() : value;
} else if (value instanceof Collection || elementType.isArray()) {
TypeInformation<?> type = ClassTypeInformation.from(value.getClass());
value = ((MappingCouchbaseConverter) this).writeCollectionInternal(MappingCouchbaseConverter.asCollection(value),
new CouchbaseList(conversions.getSimpleTypeHolder()), type, null, null);
} else {
CouchbaseDocument embeddedDoc = new CouchbaseDocument();
TypeInformation<?> type = ClassTypeInformation.from(value.getClass());
((MappingCouchbaseConverter) this).writeInternalRoot(value, embeddedDoc, type, false, null);
value = embeddedDoc;
}
return value;
}
@Override

View File

@@ -169,7 +169,7 @@ public class MappingCouchbaseConverter extends AbstractCouchbaseConverter implem
* @param source the source object.
* @return the target collection.
*/
private static Collection<?> asCollection(final Object source) {
protected static Collection<?> asCollection(final Object source) {
if (source instanceof Collection) {
return (Collection<?>) source;
}
@@ -459,7 +459,7 @@ public class MappingCouchbaseConverter extends AbstractCouchbaseConverter implem
* @param property will be null for the root
*/
@SuppressWarnings("unchecked")
protected void writeInternalRoot(final Object source, CouchbaseDocument target, TypeInformation<?> typeHint,
public void writeInternalRoot(final Object source, CouchbaseDocument target, TypeInformation<?> typeHint,
boolean withId, CouchbasePersistentProperty property) {
if (source == null) {
return;
@@ -759,7 +759,7 @@ public class MappingCouchbaseConverter extends AbstractCouchbaseConverter implem
* @param target the target document.
* @return the created couchbase list.
*/
private CouchbaseList writeCollectionInternal(final Collection<?> source, final CouchbaseList target,
public CouchbaseList writeCollectionInternal(final Collection<?> source, final CouchbaseList target,
final TypeInformation<?> type, CouchbasePersistentProperty prop, ConvertingPropertyAccessor accessor) {
for (Object element : source) {

View File

@@ -33,6 +33,8 @@ import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.couchbase.core.convert.CouchbaseConverter;
import org.springframework.data.couchbase.core.mapping.CouchbaseDocument;
import org.springframework.data.couchbase.core.mapping.CouchbaseList;
import org.springframework.data.couchbase.core.mapping.CouchbasePersistentProperty;
import org.springframework.data.couchbase.core.mapping.Expiration;
import org.springframework.data.couchbase.core.query.N1QLExpression;
@@ -434,6 +436,12 @@ public class StringBasedN1qlQueryParser {
for (Parameter parameter : this.queryMethod.getParameters().getBindableParameters()) {
Object rawValue = accessor.getBindableValue(parameter.getIndex());
Object value = couchbaseConverter.convertForWriteIfNeeded(rawValue);
if (value instanceof CouchbaseDocument) {
value = ((CouchbaseDocument) value).export();
}
if (value instanceof CouchbaseList) {
value = ((CouchbaseList) value).export();
}
putPositionalValue(posValues, value);
}
return posValues;
@@ -452,7 +460,9 @@ public class StringBasedN1qlQueryParser {
String placeholder = parameter.getPlaceholder();
Object rawValue = accessor.getBindableValue(parameter.getIndex());
Object value = couchbaseConverter.convertForWriteIfNeeded(rawValue);
if (value instanceof CouchbaseDocument) {
value = ((CouchbaseDocument) value).export();
}
if (placeholder != null && placeholder.charAt(0) == ':') {
placeholder = placeholder.replaceFirst(":", "");
putNamedValue(namedValues, placeholder, value);