Skip to content

Commit 4c11fbc

Browse files
committed
Move to Spring Data Commons' association abstraction.
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.
1 parent 0058752 commit 4c11fbc

File tree

7 files changed

+42
-14
lines changed

7 files changed

+42
-14
lines changed

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/BasicJdbcConverter.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public Class<?> getColumnType(RelationalPersistentProperty property) {
183183

184184
private Class<?> doGetColumnType(RelationalPersistentProperty property) {
185185

186-
if (property.isReference()) {
186+
if (property.isAssociation()) {
187187
return getReferenceColumnType(property);
188188
}
189189

@@ -419,23 +419,23 @@ private T populateProperties(T instance, @Nullable Object idValue) {
419419
PersistentPropertyAccessor<T> propertyAccessor = getPropertyAccessor(entity, instance);
420420
PreferredConstructor<T, RelationalPersistentProperty> persistenceConstructor = entity.getPersistenceConstructor();
421421

422-
for (RelationalPersistentProperty property : entity) {
422+
entity.doWithAll(property -> {
423423

424424
if (persistenceConstructor != null && persistenceConstructor.isConstructorParameter(property)) {
425-
continue;
425+
return;
426426
}
427427

428428
// skip absent simple properties
429429
if (isSimpleProperty(property)) {
430430

431431
if (!propertyValueProvider.hasProperty(property)) {
432-
continue;
432+
return;
433433
}
434434
}
435435

436436
Object value = readOrLoadProperty(idValue, property);
437437
propertyAccessor.setProperty(property, value);
438-
}
438+
});
439439

440440
return propertyAccessor.getBean();
441441
}
@@ -513,7 +513,7 @@ private boolean hasInstanceValues(@Nullable Object idValue) {
513513
for (RelationalPersistentProperty embeddedProperty : persistentEntity) {
514514

515515
// if the embedded contains Lists, Sets or Maps we consider it non-empty
516-
if (embeddedProperty.isQualified() || embeddedProperty.isReference()) {
516+
if (embeddedProperty.isQualified() || embeddedProperty.isAssociation()) {
517517
return true;
518518
}
519519

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/DefaultDataAccessStrategy.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import org.springframework.data.mapping.PersistentProperty;
3737
import org.springframework.data.mapping.PersistentPropertyAccessor;
3838
import org.springframework.data.mapping.PersistentPropertyPath;
39-
import org.springframework.data.mapping.PropertyHandler;
4039
import org.springframework.data.relational.core.dialect.IdGeneration;
4140
import org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension;
4241
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
@@ -424,7 +423,7 @@ private <S, T> SqlIdentifierParameterSource getParameterSource(@Nullable S insta
424423
PersistentPropertyAccessor<S> propertyAccessor = instance != null ? persistentEntity.getPropertyAccessor(instance)
425424
: NoValuePropertyAccessor.instance();
426425

427-
persistentEntity.doWithProperties((PropertyHandler<RelationalPersistentProperty>) property -> {
426+
persistentEntity.doWithAll(property -> {
428427

429428
if (skipProperty.test(property) || !property.isWritable()) {
430429
return;

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/SqlGenerator.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import org.springframework.data.domain.Sort;
2020
import org.springframework.data.jdbc.repository.support.SimpleJdbcRepository;
2121
import org.springframework.data.mapping.PersistentPropertyPath;
22-
import org.springframework.data.mapping.PropertyHandler;
2322
import org.springframework.data.mapping.context.MappingContext;
2423
import org.springframework.data.relational.core.dialect.Dialect;
2524
import org.springframework.data.relational.core.dialect.RenderContextFactory;
@@ -818,7 +817,7 @@ static class Columns {
818817

819818
private void populateColumnNameCache(RelationalPersistentEntity<?> entity, String prefix) {
820819

821-
entity.doWithProperties((PropertyHandler<RelationalPersistentProperty>) property -> {
820+
entity.doWithAll(property -> {
822821

823822
// the referencing column of referenced entity is expected to be on the other side of the relation
824823
if (!property.isEntity()) {

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/mapping/BasicJdbcPersistentProperty.java

+14-2
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,21 @@ public BasicJdbcPersistentProperty(Property property, PersistentEntity<?, Relati
6060
super(property, owner, simpleTypeHolder, namingStrategy);
6161
}
6262

63+
/*
64+
* (non-Javadoc)
65+
* @see org.springframework.data.mapping.model.AnnotationBasedPersistentProperty#isAssociation()
66+
*/
6367
@Override
64-
public boolean isReference() {
65-
return AggregateReference.class.isAssignableFrom(getRawType());
68+
public boolean isAssociation() {
69+
return super.isAssociation() || AggregateReference.class.isAssignableFrom(getRawType());
6670
}
6771

72+
/*
73+
* (non-Javadoc)
74+
* @see org.springframework.data.relational.core.mapping.BasicRelationalPersistentProperty#isReference()
75+
*/
76+
@Override
77+
public boolean isReference() {
78+
return isAssociation();
79+
}
6880
}

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/mapping/BasicJdbcPersistentPropertyUnitTests.java

+13
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,15 @@ public void detectsKeyColumnOverrideNameFromMappedCollectionAnnotation() {
9595
assertThat(listProperty.getReverseColumnName(path)).isEqualTo(quoted("override_id"));
9696
}
9797

98+
@Test // #938
99+
void considersAggregateReferenceAnAssociation() {
100+
101+
RelationalPersistentEntity<?> entity = context.getRequiredPersistentEntity(WithAssociations.class);
102+
RelationalPersistentProperty property = entity.getRequiredPersistentProperty("association");
103+
104+
assertThat(property.isAssociation()).isTrue();
105+
}
106+
98107
private PersistentPropertyPathExtension getPersistentPropertyPath(Class<?> type, String propertyName) {
99108
PersistentPropertyPath<RelationalPersistentProperty> path = context
100109
.findPersistentPropertyPaths(type, p -> p.getName().equals(propertyName)).getFirst()
@@ -149,4 +158,8 @@ private static class WithCollections {
149158
@MappedCollection(idColumn = "override_id", keyColumn = "override_key") //
150159
List<Integer> overrideList;
151160
}
161+
162+
static class WithAssociations {
163+
AggregateReference<Object, Long> association;
164+
}
152165
}

spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/BasicRelationalPersistentProperty.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ private SqlIdentifier createDerivedSqlIdentifier(String name) {
124124
*/
125125
@Override
126126
protected Association<RelationalPersistentProperty> createAssociation() {
127-
throw new UnsupportedOperationException();
127+
return new Association<>(this, null);
128128
}
129129

130130
public boolean isForceQuote() {
@@ -137,7 +137,7 @@ public void setForceQuote(boolean forceQuote) {
137137

138138
@Override
139139
public boolean isEntity() {
140-
return super.isEntity() && !isReference();
140+
return super.isEntity() && !isAssociation();
141141
}
142142

143143
@Override

spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/RelationalPersistentProperty.java

+5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
*/
2929
public interface RelationalPersistentProperty extends PersistentProperty<RelationalPersistentProperty> {
3030

31+
/**
32+
* @deprecated since 2.2, in favor of {@link #isAssociation()}
33+
* @return
34+
*/
35+
@Deprecated
3136
boolean isReference();
3237

3338
/**

0 commit comments

Comments
 (0)