Search in sources :

Example 1 with SpannerDataException

use of org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerDataException in project spring-cloud-gcp by spring-cloud.

the class MappingSpannerReadConverter method instantiate.

private static <R> R instantiate(Class<R> type) {
    R object;
    try {
        Constructor<R> constructor = type.getDeclaredConstructor();
        constructor.setAccessible(true);
        object = constructor.newInstance();
    } catch (ReflectiveOperationException e) {
        throw new SpannerDataException("Unable to create a new instance of entity using default constructor.", e);
    }
    return object;
}
Also used : SpannerDataException(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerDataException)

Example 2 with SpannerDataException

use of org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerDataException in project spring-cloud-gcp by spring-cloud.

the class MappingSpannerReadConverter method read.

/**
 * Reads a single POJO from a Spanner row.
 * @param type the type of POJO
 * @param source the Spanner row
 * @param includeColumns the columns to read. If null then all columns will be read.
 * @param <R> the type of the POJO.
 * @return the POJO
 */
public <R> R read(Class<R> type, Struct source, Set<String> includeColumns) {
    boolean readAllColumns = includeColumns == null;
    R object = instantiate(type);
    SpannerPersistentEntity<?> persistentEntity = this.spannerMappingContext.getPersistentEntity(type);
    PersistentPropertyAccessor accessor = persistentEntity.getPropertyAccessor(object);
    persistentEntity.doWithProperties((PropertyHandler<SpannerPersistentProperty>) spannerPersistentProperty -> {
        String columnName = spannerPersistentProperty.getColumnName();
        try {
            if ((!readAllColumns && !includeColumns.contains(columnName)) || source.isNull(columnName)) {
                return;
            }
        } catch (IllegalArgumentException e) {
            throw new SpannerDataException("Unable to read column from Spanner results: " + columnName, e);
        }
        Class propType = spannerPersistentProperty.getType();
        boolean valueSet;
        if (ConversionUtils.isIterableNonByteArrayType(propType)) {
            valueSet = attemptReadIterableValue(spannerPersistentProperty, source, columnName, accessor);
        } else {
            Class sourceType = this.spannerColumnTypeToJavaTypeMapping.get(source.getColumnType(columnName));
            if (sourceType != null && canConvert(sourceType, propType)) {
                valueSet = attemptReadSingleItemValue(spannerPersistentProperty, source, sourceType, columnName, accessor);
            } else {
                valueSet = false;
            }
        }
        if (!valueSet) {
            throw new SpannerDataException(String.format("The value in column with name %s" + " could not be converted to the corresponding property in the entity." + " The property's type is %s.", columnName, propType));
        }
    });
    return object;
}
Also used : Date(com.google.cloud.Date) AbstractStructReader(com.google.cloud.spanner.AbstractStructReader) CustomConversions(org.springframework.data.convert.CustomConversions) ImmutableMap(com.google.common.collect.ImmutableMap) BiFunction(java.util.function.BiFunction) SpannerDataException(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerDataException) Set(java.util.Set) Type(com.google.cloud.spanner.Type) Timestamp(com.google.cloud.Timestamp) Constructor(java.lang.reflect.Constructor) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) PropertyHandler(org.springframework.data.mapping.PropertyHandler) EntityReader(org.springframework.data.convert.EntityReader) List(java.util.List) SpannerPersistentProperty(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentProperty) Struct(com.google.cloud.spanner.Struct) Map(java.util.Map) ByteArray(com.google.cloud.ByteArray) SpannerPersistentEntity(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentEntity) SpannerMappingContext(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerMappingContext) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) SpannerDataException(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerDataException) SpannerPersistentProperty(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentProperty)

Aggregations

SpannerDataException (org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerDataException)2 ByteArray (com.google.cloud.ByteArray)1 Date (com.google.cloud.Date)1 Timestamp (com.google.cloud.Timestamp)1 AbstractStructReader (com.google.cloud.spanner.AbstractStructReader)1 Struct (com.google.cloud.spanner.Struct)1 Type (com.google.cloud.spanner.Type)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Constructor (java.lang.reflect.Constructor)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 BiFunction (java.util.function.BiFunction)1 SpannerMappingContext (org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerMappingContext)1 SpannerPersistentEntity (org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentEntity)1 SpannerPersistentProperty (org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentProperty)1 CustomConversions (org.springframework.data.convert.CustomConversions)1 EntityReader (org.springframework.data.convert.EntityReader)1 PersistentPropertyAccessor (org.springframework.data.mapping.PersistentPropertyAccessor)1 PropertyHandler (org.springframework.data.mapping.PropertyHandler)1