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 0a8bce9d..c184d5e6 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 BasicJdbcConverter extends BasicRelationalConverter implements Jdbc private Class doGetColumnType(RelationalPersistentProperty property) { - if (property.isReference()) { + if (property.isAssociation()) { return getReferenceColumnType(property); } @@ -419,23 +419,23 @@ public class BasicJdbcConverter extends BasicRelationalConverter implements Jdbc 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 @@ public class BasicJdbcConverter extends BasicRelationalConverter implements Jdbc 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 939dde04..33dd6fa4 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.jdbc.support.JdbcUtil; 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 @@ public class DefaultDataAccessStrategy implements DataAccessStrategy { 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 b6171389..06bfaf0a 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.Pageable; 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 @@ class SqlGenerator { 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 2fab7e0b..c7297d89 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 class BasicJdbcPersistentProperty extends BasicRelationalPersistentProper super(property, owner, simpleTypeHolder, namingStrategy); } + /* + * (non-Javadoc) + * @see org.springframework.data.mapping.model.AnnotationBasedPersistentProperty#isAssociation() + */ @Override - public boolean isReference() { - return AggregateReference.class.isAssignableFrom(getRawType()); + public boolean isAssociation() { + return super.isAssociation() || AggregateReference.class.isAssignableFrom(getRawType()); } + /* + * (non-Javadoc) + * @see org.springframework.data.relational.core.mapping.BasicRelationalPersistentProperty#isReference() + */ + @Override + public boolean isReference() { + return isAssociation(); + } } diff --git a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/mapping/BasicJdbcPersistentPropertyUnitTests.java b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/mapping/BasicJdbcPersistentPropertyUnitTests.java index 5f748a24..5e5ad09b 100644 --- a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/mapping/BasicJdbcPersistentPropertyUnitTests.java +++ b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/mapping/BasicJdbcPersistentPropertyUnitTests.java @@ -95,6 +95,15 @@ public class BasicJdbcPersistentPropertyUnitTests { assertThat(listProperty.getReverseColumnName(path)).isEqualTo(quoted("override_id")); } + @Test // #938 + void considersAggregateReferenceAnAssociation() { + + RelationalPersistentEntity 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 @@ public class BasicJdbcPersistentPropertyUnitTests { @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 1f4fb037..1cd392b8 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 @@ public class BasicRelationalPersistentProperty extends AnnotationBasedPersistent */ @Override protected Association createAssociation() { - throw new UnsupportedOperationException(); + return new Association<>(this, null); } public boolean isForceQuote() { @@ -137,7 +137,7 @@ public class BasicRelationalPersistentProperty extends AnnotationBasedPersistent @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 a809cc13..96fe88d0 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 @@ import org.springframework.lang.Nullable; */ public interface RelationalPersistentProperty extends PersistentProperty { + /** + * @deprecated since 2.2, in favor of {@link #isAssociation()} + * @return + */ + @Deprecated boolean isReference(); /**