From 4c11fbcb5761878d5ac11f2e7551f93a92609bf3 Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Thu, 11 Mar 2021 11:57:26 +0100 Subject: [PATCH] Move to Spring Data Commons' association abstraction. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Deprecated Spring Data JDBC's custom PersistentProperty.isReference() in favor of the already existing ….isAssociation() with the same semantics to benefit from association definitions implemented in Spring Data Commons. Make use of the newly introduced PersistentEntity.doWithAll(…) to apply the previously existing property handling to both properties and now associations, too. --- .../jdbc/core/convert/BasicJdbcConverter.java | 12 ++++++------ .../core/convert/DefaultDataAccessStrategy.java | 3 +-- .../data/jdbc/core/convert/SqlGenerator.java | 3 +-- .../mapping/BasicJdbcPersistentProperty.java | 16 ++++++++++++++-- .../BasicJdbcPersistentPropertyUnitTests.java | 13 +++++++++++++ .../BasicRelationalPersistentProperty.java | 4 ++-- .../mapping/RelationalPersistentProperty.java | 5 +++++ 7 files changed, 42 insertions(+), 14 deletions(-) diff --git a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/BasicJdbcConverter.java b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/BasicJdbcConverter.java index 0a8bce9dc7..c184d5e6e1 100644 --- a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/BasicJdbcConverter.java +++ b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/BasicJdbcConverter.java @@ -183,7 +183,7 @@ public Class getColumnType(RelationalPersistentProperty property) { private Class doGetColumnType(RelationalPersistentProperty property) { - if (property.isReference()) { + if (property.isAssociation()) { return getReferenceColumnType(property); } @@ -419,23 +419,23 @@ private T populateProperties(T instance, @Nullable Object idValue) { PersistentPropertyAccessor propertyAccessor = getPropertyAccessor(entity, instance); PreferredConstructor persistenceConstructor = entity.getPersistenceConstructor(); - for (RelationalPersistentProperty property : entity) { + entity.doWithAll(property -> { if (persistenceConstructor != null && persistenceConstructor.isConstructorParameter(property)) { - continue; + return; } // skip absent simple properties if (isSimpleProperty(property)) { if (!propertyValueProvider.hasProperty(property)) { - continue; + return; } } Object value = readOrLoadProperty(idValue, property); propertyAccessor.setProperty(property, value); - } + }); return propertyAccessor.getBean(); } @@ -513,7 +513,7 @@ private boolean hasInstanceValues(@Nullable Object idValue) { for (RelationalPersistentProperty embeddedProperty : persistentEntity) { // if the embedded contains Lists, Sets or Maps we consider it non-empty - if (embeddedProperty.isQualified() || embeddedProperty.isReference()) { + if (embeddedProperty.isQualified() || embeddedProperty.isAssociation()) { return true; } diff --git a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/DefaultDataAccessStrategy.java b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/DefaultDataAccessStrategy.java index 939dde047c..33dd6fa439 100644 --- a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/DefaultDataAccessStrategy.java +++ b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/DefaultDataAccessStrategy.java @@ -36,7 +36,6 @@ import org.springframework.data.mapping.PersistentProperty; import org.springframework.data.mapping.PersistentPropertyAccessor; import org.springframework.data.mapping.PersistentPropertyPath; -import org.springframework.data.mapping.PropertyHandler; import org.springframework.data.relational.core.dialect.IdGeneration; import org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension; import org.springframework.data.relational.core.mapping.RelationalMappingContext; @@ -424,7 +423,7 @@ private SqlIdentifierParameterSource getParameterSource(@Nullable S insta PersistentPropertyAccessor propertyAccessor = instance != null ? persistentEntity.getPropertyAccessor(instance) : NoValuePropertyAccessor.instance(); - persistentEntity.doWithProperties((PropertyHandler) property -> { + persistentEntity.doWithAll(property -> { if (skipProperty.test(property) || !property.isWritable()) { return; diff --git a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/SqlGenerator.java b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/SqlGenerator.java index b61713891e..06bfaf0a52 100644 --- a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/SqlGenerator.java +++ b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/SqlGenerator.java @@ -19,7 +19,6 @@ import org.springframework.data.domain.Sort; import org.springframework.data.jdbc.repository.support.SimpleJdbcRepository; import org.springframework.data.mapping.PersistentPropertyPath; -import org.springframework.data.mapping.PropertyHandler; import org.springframework.data.mapping.context.MappingContext; import org.springframework.data.relational.core.dialect.Dialect; import org.springframework.data.relational.core.dialect.RenderContextFactory; @@ -818,7 +817,7 @@ static class Columns { private void populateColumnNameCache(RelationalPersistentEntity entity, String prefix) { - entity.doWithProperties((PropertyHandler) property -> { + entity.doWithAll(property -> { // the referencing column of referenced entity is expected to be on the other side of the relation if (!property.isEntity()) { diff --git a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/mapping/BasicJdbcPersistentProperty.java b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/mapping/BasicJdbcPersistentProperty.java index 2fab7e0b5f..c7297d8981 100644 --- a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/mapping/BasicJdbcPersistentProperty.java +++ b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/mapping/BasicJdbcPersistentProperty.java @@ -60,9 +60,21 @@ public BasicJdbcPersistentProperty(Property property, PersistentEntity entity = context.getRequiredPersistentEntity(WithAssociations.class); + RelationalPersistentProperty property = entity.getRequiredPersistentProperty("association"); + + assertThat(property.isAssociation()).isTrue(); + } + private PersistentPropertyPathExtension getPersistentPropertyPath(Class type, String propertyName) { PersistentPropertyPath path = context .findPersistentPropertyPaths(type, p -> p.getName().equals(propertyName)).getFirst() @@ -149,4 +158,8 @@ private static class WithCollections { @MappedCollection(idColumn = "override_id", keyColumn = "override_key") // List overrideList; } + + static class WithAssociations { + AggregateReference association; + } } diff --git a/spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/BasicRelationalPersistentProperty.java b/spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/BasicRelationalPersistentProperty.java index 1f4fb0378e..1cd392b85f 100644 --- a/spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/BasicRelationalPersistentProperty.java +++ b/spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/BasicRelationalPersistentProperty.java @@ -124,7 +124,7 @@ private SqlIdentifier createDerivedSqlIdentifier(String name) { */ @Override protected Association createAssociation() { - throw new UnsupportedOperationException(); + return new Association<>(this, null); } public boolean isForceQuote() { @@ -137,7 +137,7 @@ public void setForceQuote(boolean forceQuote) { @Override public boolean isEntity() { - return super.isEntity() && !isReference(); + return super.isEntity() && !isAssociation(); } @Override diff --git a/spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/RelationalPersistentProperty.java b/spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/RelationalPersistentProperty.java index a809cc13e9..96fe88d03d 100644 --- a/spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/RelationalPersistentProperty.java +++ b/spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/RelationalPersistentProperty.java @@ -28,6 +28,11 @@ */ public interface RelationalPersistentProperty extends PersistentProperty { + /** + * @deprecated since 2.2, in favor of {@link #isAssociation()} + * @return + */ + @Deprecated boolean isReference(); /**