Search in sources :

Example 1 with PersistentPropertyAccessor

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;
}
Also used : DefaultSpELExpressionEvaluator(org.springframework.data.mapping.model.DefaultSpELExpressionEvaluator) MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) SpELExpressionEvaluator(org.springframework.data.mapping.model.SpELExpressionEvaluator) DefaultSpELExpressionEvaluator(org.springframework.data.mapping.model.DefaultSpELExpressionEvaluator) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) Document(org.bson.Document)

Example 2 with PersistentPropertyAccessor

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;
}
Also used : MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) EntityInstantiator(org.springframework.data.convert.EntityInstantiator) DefaultSpELExpressionEvaluator(org.springframework.data.mapping.model.DefaultSpELExpressionEvaluator) ConvertingPropertyAccessor(org.springframework.data.mapping.model.ConvertingPropertyAccessor) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Nullable(org.springframework.lang.Nullable)

Example 3 with PersistentPropertyAccessor

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);
}
Also used : MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) ObjectId(org.bson.types.ObjectId) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) DBRef(com.mongodb.DBRef) Document(org.bson.Document) Test(org.junit.Test)

Example 4 with PersistentPropertyAccessor

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;
}
Also used : JdbcPersistentProperty(org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty) ConvertingPropertyAccessor(org.springframework.data.mapping.model.ConvertingPropertyAccessor) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) JdbcPersistentEntity(org.springframework.data.jdbc.mapping.model.JdbcPersistentEntity)

Example 5 with PersistentPropertyAccessor

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());
}
Also used : KeySet(com.google.cloud.spanner.KeySet) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) PersistentProperty(org.springframework.data.mapping.PersistentProperty) Key(com.google.cloud.spanner.Key)

Aggregations

PersistentPropertyAccessor (org.springframework.data.mapping.PersistentPropertyAccessor)17 Test (org.junit.Test)6 MongoPersistentProperty (org.springframework.data.mongodb.core.mapping.MongoPersistentProperty)6 Document (org.bson.Document)4 ConvertingPropertyAccessor (org.springframework.data.mapping.model.ConvertingPropertyAccessor)4 ByteArray (com.google.cloud.ByteArray)2 Date (com.google.cloud.Date)2 Timestamp (com.google.cloud.Timestamp)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 BasicDBObject (com.mongodb.BasicDBObject)2 DBObject (com.mongodb.DBObject)2 Constructor (java.lang.reflect.Constructor)2 Map (java.util.Map)2 Set (java.util.Set)2 BiFunction (java.util.function.BiFunction)2 SpannerDataException (org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerDataException)2 SpannerMappingContext (org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerMappingContext)2 SpannerPersistentEntity (org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentEntity)2 SpannerPersistentProperty (org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentProperty)2 ConversionService (org.springframework.core.convert.ConversionService)2