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;
}
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;
}
Aggregations