use of org.springframework.data.mapping.PersistentPropertyAccessor in project spring-data-mongodb by spring-projects.
the class DefaultDbRefProxyHandler method populateId.
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.convert.DbRefProxyHandler#populateId(com.mongodb.DBRef, java.lang.Object)
*/
@Override
public Object populateId(MongoPersistentProperty property, @Nullable DBRef source, Object proxy) {
if (source == null) {
return proxy;
}
MongoPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(property);
MongoPersistentProperty idProperty = entity.getRequiredIdProperty();
if (idProperty.usePropertyAccess()) {
return proxy;
}
SpELExpressionEvaluator evaluator = new DefaultSpELExpressionEvaluator(proxy, spELContext);
PersistentPropertyAccessor accessor = entity.getPropertyAccessor(proxy);
Document object = new Document(idProperty.getFieldName(), source.getId());
ObjectPath objectPath = ObjectPath.ROOT.push(proxy, entity, null);
accessor.setProperty(idProperty, resolver.getValueInternal(idProperty, object, evaluator, objectPath));
return proxy;
}
use of org.springframework.data.mapping.PersistentPropertyAccessor in project spring-data-mongodb by spring-projects.
the class MappingMongoConverter method read.
@Nullable
private <S extends Object> S read(final MongoPersistentEntity<S> entity, final Document bson, final ObjectPath path) {
DefaultSpELExpressionEvaluator evaluator = new DefaultSpELExpressionEvaluator(bson, spELContext);
ParameterValueProvider<MongoPersistentProperty> provider = getParameterProvider(entity, bson, evaluator, path);
EntityInstantiator instantiator = instantiators.getInstantiatorFor(entity);
S instance = instantiator.createInstance(entity, provider);
PersistentPropertyAccessor accessor = new ConvertingPropertyAccessor(entity.getPropertyAccessor(instance), conversionService);
MongoPersistentProperty idProperty = entity.getIdProperty();
DocumentAccessor documentAccessor = new DocumentAccessor(bson);
// make sure id property is set before all other properties
Object idValue = null;
if (idProperty != null && documentAccessor.hasValue(idProperty)) {
idValue = readIdValue(path, evaluator, idProperty, documentAccessor);
accessor.setProperty(idProperty, idValue);
}
ObjectPath currentPath = path.push(instance, entity, idValue != null ? bson.get(idProperty.getFieldName()) : null);
MongoDbPropertyValueProvider valueProvider = new MongoDbPropertyValueProvider(documentAccessor, evaluator, currentPath);
DbRefResolverCallback callback = new DefaultDbRefResolverCallback(bson, currentPath, evaluator, MappingMongoConverter.this);
readProperties(entity, accessor, idProperty, documentAccessor, valueProvider, callback);
return instance;
}
use of org.springframework.data.mapping.PersistentPropertyAccessor in project spring-data-mongodb by spring-projects.
the class DbRefMappingMongoConverterUnitTests method shouldEagerlyResolveIdPropertyWithFieldAccess.
// DATAMONGO-1012
@Test
public void shouldEagerlyResolveIdPropertyWithFieldAccess() {
MongoPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(ClassWithLazyDbRefs.class);
MongoPersistentProperty property = entity.getRequiredPersistentProperty("dbRefToConcreteType");
MongoPersistentEntity<?> propertyEntity = mappingContext.getRequiredPersistentEntity(property);
String idValue = new ObjectId().toString();
DBRef dbRef = converter.toDBRef(new LazyDbRefTarget(idValue), property);
Document object = new Document("dbRefToConcreteType", dbRef);
ClassWithLazyDbRefs result = converter.read(ClassWithLazyDbRefs.class, object);
PersistentPropertyAccessor accessor = propertyEntity.getPropertyAccessor(result.dbRefToConcreteType);
MongoPersistentProperty idProperty = mappingContext.getRequiredPersistentEntity(LazyDbRefTarget.class).getIdProperty();
assertThat(accessor.getProperty(idProperty), is(notNullValue()));
assertProxyIsResolved(result.dbRefToConcreteType, false);
}
use of org.springframework.data.mapping.PersistentPropertyAccessor in project spring-data-jdbc by spring-projects.
the class EntityRowMapper method readEntityFrom.
private <S> S readEntityFrom(ResultSet rs, PersistentProperty<?> property) {
String prefix = property.getName() + "_";
@SuppressWarnings("unchecked") JdbcPersistentEntity<S> entity = (JdbcPersistentEntity<S>) context.getRequiredPersistentEntity(property.getActualType());
if (readFrom(rs, entity.getRequiredIdProperty(), prefix) == null) {
return null;
}
S instance = instantiator.createInstance(entity, new ResultSetParameterValueProvider(rs, entity, conversions, prefix));
PersistentPropertyAccessor accessor = entity.getPropertyAccessor(instance);
ConvertingPropertyAccessor propertyAccessor = new ConvertingPropertyAccessor(accessor, conversions);
for (JdbcPersistentProperty p : entity) {
propertyAccessor.setProperty(p, readFrom(rs, p, prefix));
}
return instance;
}
use of org.springframework.data.mapping.PersistentPropertyAccessor in project spring-cloud-gcp by spring-cloud.
the class SpannerMutationFactoryImpl method delete.
@Override
public <T> Mutation delete(Class<T> entityClass, Iterable<? extends T> entities) {
SpannerPersistentEntity<?> persistentEntity = this.spannerMappingContext.getPersistentEntity(entityClass);
KeySet.Builder builder = KeySet.newBuilder();
for (T entity : entities) {
PersistentPropertyAccessor accessor = persistentEntity.getPropertyAccessor(entity);
PersistentProperty idProperty = persistentEntity.getIdProperty();
Key value = (Key) accessor.getProperty(idProperty);
builder.addKey(value);
}
return delete(entityClass, builder.build());
}
Aggregations