Skip to content

Commit da64048

Browse files
schaudermp911de
authored andcommitted
Refine embeddable emptiness checks to consider whether the collection holds entities.
Embedded entities which contain a empty collection of values that aren't entities are now considered empty, if this collection is empty. Closes #1737 Original pull request: #1812
1 parent b76bd37 commit da64048

File tree

3 files changed

+81
-12
lines changed

3 files changed

+81
-12
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ public <T> T getPropertyValue(RelationalPersistentProperty property) {
422422
@Override
423423
public boolean hasValue(RelationalPersistentProperty property) {
424424

425-
if (property.isCollectionLike() || property.isMap()) {
425+
if ((property.isCollectionLike() && property.isEntity())|| property.isMap()) {
426426
// attempt relation fetch
427427
return true;
428428
}

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

+42
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import org.springframework.data.mapping.context.InvalidPersistentPropertyPath;
5656
import org.springframework.data.relational.core.conversion.DbActionExecutionException;
5757
import org.springframework.data.relational.core.mapping.Column;
58+
import org.springframework.data.relational.core.mapping.Embedded;
5859
import org.springframework.data.relational.core.mapping.InsertOnlyProperty;
5960
import org.springframework.data.relational.core.mapping.MappedCollection;
6061
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
@@ -772,6 +773,36 @@ void saveAndLoadAnEntityWithSet() {
772773
assertThat(reloaded.digits).isEqualTo(new HashSet<>(asList("one", "two", "three")));
773774
}
774775

776+
@Test //GH-1737
777+
@EnabledOnFeature(SUPPORTS_ARRAYS)
778+
void saveAndLoadEmbeddedArray() {
779+
780+
EmbeddedStringListOwner embeddedStringListOwner = new EmbeddedStringListOwner();
781+
embeddedStringListOwner.embeddedStringList = new EmbeddedStringList();
782+
embeddedStringListOwner.embeddedStringList.digits = List.of("one", "two", "three");
783+
784+
EmbeddedStringListOwner saved = template.save(embeddedStringListOwner);
785+
786+
EmbeddedStringListOwner reloaded = template.findById(saved.id, EmbeddedStringListOwner.class);
787+
788+
assertThat(reloaded.embeddedStringList.digits).containsExactly("one", "two", "three");
789+
}
790+
791+
@Test //GH-1737
792+
@EnabledOnFeature(SUPPORTS_ARRAYS)
793+
void saveAndLoadEmptyEmbeddedArray() {
794+
795+
EmbeddedStringListOwner embeddedStringListOwner = new EmbeddedStringListOwner();
796+
embeddedStringListOwner.embeddedStringList = new EmbeddedStringList();
797+
embeddedStringListOwner.embeddedStringList.digits = emptyList();
798+
799+
EmbeddedStringListOwner saved = template.save(embeddedStringListOwner);
800+
801+
EmbeddedStringListOwner reloaded = template.findById(saved.id, EmbeddedStringListOwner.class);
802+
803+
assertThat(reloaded.embeddedStringList).isNull();
804+
}
805+
775806
@Test
776807
// DATAJDBC-327
777808
void saveAndLoadAnEntityWithByteArray() {
@@ -1395,6 +1426,17 @@ private static class FloatListOwner {
13951426
List<Float> digits = new ArrayList<>();
13961427
}
13971428

1429+
@Table("ARRAY_OWNER")
1430+
private static class EmbeddedStringListOwner {
1431+
@Id Long id;
1432+
1433+
@Embedded(onEmpty = Embedded.OnEmpty.USE_NULL, prefix = "") EmbeddedStringList embeddedStringList;
1434+
}
1435+
1436+
private static class EmbeddedStringList {
1437+
List<String> digits = new ArrayList<>();
1438+
}
1439+
13981440
static class LegoSet {
13991441

14001442
@Column("id1")

spring-data-relational/src/main/java/org/springframework/data/relational/core/conversion/MappingRelationalConverter.java

+38-11
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,7 @@
4141
import org.springframework.data.mapping.PersistentPropertyAccessor;
4242
import org.springframework.data.mapping.PersistentPropertyPathAccessor;
4343
import org.springframework.data.mapping.context.MappingContext;
44-
import org.springframework.data.mapping.model.ConvertingPropertyAccessor;
45-
import org.springframework.data.mapping.model.DefaultSpELExpressionEvaluator;
46-
import org.springframework.data.mapping.model.EntityInstantiator;
47-
import org.springframework.data.mapping.model.ParameterValueProvider;
48-
import org.springframework.data.mapping.model.PersistentEntityParameterValueProvider;
49-
import org.springframework.data.mapping.model.PropertyValueProvider;
50-
import org.springframework.data.mapping.model.SimpleTypeHolder;
51-
import org.springframework.data.mapping.model.SpELContext;
52-
import org.springframework.data.mapping.model.SpELExpressionEvaluator;
53-
import org.springframework.data.mapping.model.SpELExpressionParameterValueProvider;
44+
import org.springframework.data.mapping.model.*;
5445
import org.springframework.data.projection.EntityProjection;
5546
import org.springframework.data.projection.EntityProjectionIntrospector;
5647
import org.springframework.data.projection.EntityProjectionIntrospector.ProjectionPredicate;
@@ -1143,7 +1134,43 @@ public Object getValue(AggregatePath path) {
11431134

11441135
@Override
11451136
public boolean hasValue(AggregatePath path) {
1146-
return document.get(path.getColumnInfo().alias().getReference()) != null;
1137+
Object value = document.get(path.getColumnInfo().alias().getReference());
1138+
1139+
if (value == null) {
1140+
return false;
1141+
}
1142+
if (!path.isCollectionLike()) {
1143+
return true;
1144+
}
1145+
1146+
if (value instanceof char[] ar) {
1147+
return ar.length != 0;
1148+
}
1149+
if (value instanceof byte[] ar) {
1150+
return ar.length != 0;
1151+
}
1152+
if (value instanceof short[] ar) {
1153+
return ar.length != 0;
1154+
}
1155+
if (value instanceof int[] ar) {
1156+
return ar.length != 0;
1157+
}
1158+
if (value instanceof long[] ar) {
1159+
return ar.length != 0;
1160+
}
1161+
if (value instanceof float[] ar) {
1162+
return ar.length != 0;
1163+
}
1164+
if (value instanceof double[] ar) {
1165+
return ar.length != 0;
1166+
}
1167+
if (value instanceof Object[] ar) {
1168+
return ar.length != 0;
1169+
}
1170+
if (value instanceof Collection<?> col) {
1171+
return !col.isEmpty();
1172+
}
1173+
return true;
11471174
}
11481175

11491176
@Override

0 commit comments

Comments
 (0)