Skip to content

Commit 0a1bb2b

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 cf26d7e commit 0a1bb2b

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
@@ -44,16 +44,7 @@
4444
import org.springframework.data.mapping.PersistentPropertyAccessor;
4545
import org.springframework.data.mapping.PersistentPropertyPathAccessor;
4646
import org.springframework.data.mapping.context.MappingContext;
47-
import org.springframework.data.mapping.model.CachingValueExpressionEvaluatorFactory;
48-
import org.springframework.data.mapping.model.ConvertingPropertyAccessor;
49-
import org.springframework.data.mapping.model.EntityInstantiator;
50-
import org.springframework.data.mapping.model.ParameterValueProvider;
51-
import org.springframework.data.mapping.model.PersistentEntityParameterValueProvider;
52-
import org.springframework.data.mapping.model.PropertyValueProvider;
53-
import org.springframework.data.mapping.model.SimpleTypeHolder;
54-
import org.springframework.data.mapping.model.SpELContext;
55-
import org.springframework.data.mapping.model.ValueExpressionEvaluator;
56-
import org.springframework.data.mapping.model.ValueExpressionParameterValueProvider;
47+
import org.springframework.data.mapping.model.*;
5748
import org.springframework.data.projection.EntityProjection;
5849
import org.springframework.data.projection.EntityProjectionIntrospector;
5950
import org.springframework.data.projection.EntityProjectionIntrospector.ProjectionPredicate;
@@ -1168,7 +1159,43 @@ public Object getValue(AggregatePath path) {
11681159

11691160
@Override
11701161
public boolean hasValue(AggregatePath path) {
1171-
return document.get(path.getColumnInfo().alias().getReference()) != null;
1162+
Object value = document.get(path.getColumnInfo().alias().getReference());
1163+
1164+
if (value == null) {
1165+
return false;
1166+
}
1167+
if (!path.isCollectionLike()) {
1168+
return true;
1169+
}
1170+
1171+
if (value instanceof char[] ar) {
1172+
return ar.length != 0;
1173+
}
1174+
if (value instanceof byte[] ar) {
1175+
return ar.length != 0;
1176+
}
1177+
if (value instanceof short[] ar) {
1178+
return ar.length != 0;
1179+
}
1180+
if (value instanceof int[] ar) {
1181+
return ar.length != 0;
1182+
}
1183+
if (value instanceof long[] ar) {
1184+
return ar.length != 0;
1185+
}
1186+
if (value instanceof float[] ar) {
1187+
return ar.length != 0;
1188+
}
1189+
if (value instanceof double[] ar) {
1190+
return ar.length != 0;
1191+
}
1192+
if (value instanceof Object[] ar) {
1193+
return ar.length != 0;
1194+
}
1195+
if (value instanceof Collection<?> col) {
1196+
return !col.isEmpty();
1197+
}
1198+
return true;
11721199
}
11731200

11741201
@Override

0 commit comments

Comments
 (0)