Search in sources :

Example 11 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)

Example 12 with PersistentPropertyAccessor

use of org.springframework.data.mapping.PersistentPropertyAccessor in project spring-cloud-gcp by spring-cloud.

the class DatastoreTemplate method getIdValue.

private <T> Value<?> getIdValue(Example<T> example, DatastorePersistentEntity<?> persistentEntity, DatastorePersistentProperty persistentProperty) {
    // ID properties are not stored as regular fields in Datastore.
    Value<?> value;
    PersistentPropertyAccessor accessor = persistentEntity.getPropertyAccessor(example.getProbe());
    Object property = accessor.getProperty(persistentProperty);
    value = property != null ? KeyValue.of(createKey(persistentEntity.kindName(), property)) : NullValue.of();
    return value;
}
Also used : PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor)

Example 13 with PersistentPropertyAccessor

use of org.springframework.data.mapping.PersistentPropertyAccessor in project spring-cloud-gcp by spring-cloud.

the class SpannerPersistentEntityImplTests method testIgnoredProperty.

@Test
public void testIgnoredProperty() {
    TestEntity t = new TestEntity();
    t.id = "a";
    t.something = "a";
    t.notMapped = "b";
    SpannerPersistentEntity p = new SpannerMappingContext().getPersistentEntity(TestEntity.class);
    PersistentPropertyAccessor accessor = p.getPropertyAccessor(t);
    p.doWithProperties((SimplePropertyHandler) (property) -> assertThat(accessor.getProperty(property)).isNotEqualTo("b"));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) TypeCode(com.google.spanner.v1.TypeCode) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) ApplicationContext(org.springframework.context.ApplicationContext) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) PropertyHandler(org.springframework.data.mapping.PropertyHandler) Mockito.verify(org.mockito.Mockito.verify) List(java.util.List) ClassTypeInformation(org.springframework.data.util.ClassTypeInformation) Rule(org.junit.Rule) Key(com.google.cloud.spanner.Key) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ExpectedException(org.junit.rules.ExpectedException) SimplePropertyHandler(org.springframework.data.mapping.SimplePropertyHandler) PersistentProperty(org.springframework.data.mapping.PersistentProperty) Mockito.mock(org.mockito.Mockito.mock) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) Test(org.junit.Test)

Example 14 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 15 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
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)).isNotNull();
    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.jupiter.api.Test)

Aggregations

PersistentPropertyAccessor (org.springframework.data.mapping.PersistentPropertyAccessor)29 Test (org.junit.Test)8 PersistentProperty (org.springframework.data.mapping.PersistentProperty)7 List (java.util.List)6 Key (com.google.cloud.spanner.Key)5 ArrayList (java.util.ArrayList)5 Set (java.util.Set)5 DatastorePersistentProperty (org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentProperty)5 Map (java.util.Map)4 BaseEntity (com.google.cloud.datastore.BaseEntity)3 Entity (com.google.cloud.datastore.Entity)3 FullEntity (com.google.cloud.datastore.FullEntity)3 ListValue (com.google.cloud.datastore.ListValue)3 Value (com.google.cloud.datastore.Value)3 DatastorePersistentEntity (org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentEntity)3 EntityInstantiator (org.springframework.data.convert.EntityInstantiator)3 PropertyHandler (org.springframework.data.mapping.PropertyHandler)3 MongoPersistentProperty (org.springframework.data.mongodb.core.mapping.MongoPersistentProperty)3 ClassTypeInformation (org.springframework.data.util.ClassTypeInformation)3 ByteArray (com.google.cloud.ByteArray)2