Skip to content

Consider embedded entities with empty non entity collections empty #1812

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.0-1737-nullable-embedded-with-collection-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data Relational Parent</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-jdbc-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.0-1737-nullable-embedded-with-collection-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
4 changes: 2 additions & 2 deletions spring-data-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-jdbc</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.0-1737-nullable-embedded-with-collection-SNAPSHOT</version>

<name>Spring Data JDBC</name>
<description>Spring Data module for JDBC repositories.</description>
Expand All @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.0-1737-nullable-embedded-with-collection-SNAPSHOT</version>
</parent>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ public <T> T getPropertyValue(RelationalPersistentProperty property) {
@Override
public boolean hasValue(RelationalPersistentProperty property) {

if (property.isCollectionLike() || property.isMap()) {
if ((property.isCollectionLike() && property.isEntity())|| property.isMap()) {
// attempt relation fetch
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,8 @@
import static org.springframework.data.jdbc.testing.TestDatabaseFeatures.Feature.*;

import java.time.LocalDateTime;
import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.IntStream;

Expand Down Expand Up @@ -61,6 +53,7 @@
import org.springframework.data.mapping.context.InvalidPersistentPropertyPath;
import org.springframework.data.relational.core.conversion.DbActionExecutionException;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Embedded;
import org.springframework.data.relational.core.mapping.InsertOnlyProperty;
import org.springframework.data.relational.core.mapping.MappedCollection;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
Expand Down Expand Up @@ -778,6 +771,36 @@ void saveAndLoadAnEntityWithSet() {
assertThat(reloaded.digits).isEqualTo(new HashSet<>(asList("one", "two", "three")));
}

@Test //GH-1737
@EnabledOnFeature(SUPPORTS_ARRAYS)
void saveAndLoadEmbeddedArray() {

EmbeddedStringListOwner embeddedStringListOwner = new EmbeddedStringListOwner();
embeddedStringListOwner.embeddedStringList = new EmbeddedStringList();
embeddedStringListOwner.embeddedStringList.digits = List.of("one", "two", "three");

EmbeddedStringListOwner saved = template.save(embeddedStringListOwner);

EmbeddedStringListOwner reloaded = template.findById(saved.id, EmbeddedStringListOwner.class);

assertThat(reloaded.embeddedStringList.digits).containsExactly("one", "two", "three");
}

@Test //GH-1737
@EnabledOnFeature(SUPPORTS_ARRAYS)
void saveAndLoadEmptyEmbeddedArray() {

EmbeddedStringListOwner embeddedStringListOwner = new EmbeddedStringListOwner();
embeddedStringListOwner.embeddedStringList = new EmbeddedStringList();
embeddedStringListOwner.embeddedStringList.digits = emptyList();

EmbeddedStringListOwner saved = template.save(embeddedStringListOwner);

EmbeddedStringListOwner reloaded = template.findById(saved.id, EmbeddedStringListOwner.class);

assertThat(reloaded.embeddedStringList).isNull();
}

@Test
// DATAJDBC-327
void saveAndLoadAnEntityWithByteArray() {
Expand Down Expand Up @@ -919,7 +942,7 @@ void readOnlyGetsLoadedButNotWritten() {

assertThat(
jdbcTemplate.queryForObject("SELECT read_only FROM with_read_only", Collections.emptyMap(), String.class))
.isEqualTo("from-db");
.isEqualTo("from-db");
}

@Test
Expand Down Expand Up @@ -1258,15 +1281,16 @@ void recordOfSet() {
@Test // GH-1656
void mapWithEnumKey() {

EnumMapOwner enumMapOwner = template.save(new EnumMapOwner(null, "OwnerName", Map.of(Color.BLUE, new MapElement("Element"))));
EnumMapOwner enumMapOwner = template
.save(new EnumMapOwner(null, "OwnerName", Map.of(Color.BLUE, new MapElement("Element"))));

Iterable<EnumMapOwner> enumMapOwners = template.findAll(EnumMapOwner.class);

assertThat(enumMapOwners).containsExactly(enumMapOwner);
}

@Test // GH-1684
void oneToOneWithIdenticalIdColumnName(){
void oneToOneWithIdenticalIdColumnName() {

WithOneToOne saved = template.insert(new WithOneToOne("one", new Referenced(23L)));

Expand Down Expand Up @@ -1369,6 +1393,17 @@ private static class FloatListOwner {
List<Float> digits = new ArrayList<>();
}

@Table("ARRAY_OWNER")
private static class EmbeddedStringListOwner {
@Id Long id;

@Embedded(onEmpty = Embedded.OnEmpty.USE_NULL, prefix = "") EmbeddedStringList embeddedStringList;
}

private static class EmbeddedStringList {
List<String> digits = new ArrayList<>();
}

static class LegoSet {

@Column("id1")
Expand Down Expand Up @@ -2096,7 +2131,8 @@ record Book(String name) {
record EnumMapOwner(@Id Long id, String name, Map<Color, MapElement> map) {
}

record WithOneToOne(@Id String id,@MappedCollection(idColumn = "renamed") Referenced referenced){}
record WithOneToOne(@Id String id, @MappedCollection(idColumn = "renamed") Referenced referenced) {
}

record Referenced(@Id Long id) {
}
Expand Down
4 changes: 2 additions & 2 deletions spring-data-r2dbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-r2dbc</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.0-1737-nullable-embedded-with-collection-SNAPSHOT</version>

<name>Spring Data R2DBC</name>
<description>Spring Data module for R2DBC</description>
Expand All @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.0-1737-nullable-embedded-with-collection-SNAPSHOT</version>
</parent>

<properties>
Expand Down
4 changes: 2 additions & 2 deletions spring-data-relational/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-relational</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.0-1737-nullable-embedded-with-collection-SNAPSHOT</version>

<name>Spring Data Relational</name>
<description>Spring Data Relational support</description>

<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.0-1737-nullable-embedded-with-collection-SNAPSHOT</version>
</parent>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,7 @@
import org.springframework.data.mapping.PersistentPropertyAccessor;
import org.springframework.data.mapping.PersistentPropertyPathAccessor;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.mapping.model.CachingValueExpressionEvaluatorFactory;
import org.springframework.data.mapping.model.ConvertingPropertyAccessor;
import org.springframework.data.mapping.model.EntityInstantiator;
import org.springframework.data.mapping.model.ParameterValueProvider;
import org.springframework.data.mapping.model.PersistentEntityParameterValueProvider;
import org.springframework.data.mapping.model.PropertyValueProvider;
import org.springframework.data.mapping.model.SimpleTypeHolder;
import org.springframework.data.mapping.model.SpELContext;
import org.springframework.data.mapping.model.ValueExpressionEvaluator;
import org.springframework.data.mapping.model.ValueExpressionParameterValueProvider;
import org.springframework.data.mapping.model.*;
import org.springframework.data.projection.EntityProjection;
import org.springframework.data.projection.EntityProjectionIntrospector;
import org.springframework.data.projection.EntityProjectionIntrospector.ProjectionPredicate;
Expand Down Expand Up @@ -1168,7 +1159,43 @@ public Object getValue(AggregatePath path) {

@Override
public boolean hasValue(AggregatePath path) {
return document.get(path.getColumnInfo().alias().getReference()) != null;
Object value = document.get(path.getColumnInfo().alias().getReference());

if (value == null) {
return false;
}
if (!path.isCollectionLike()) {
return true;
}

if (value instanceof char[] ar) {
return ar.length != 0;
}
if (value instanceof byte[] ar) {
return ar.length != 0;
}
if (value instanceof short[] ar) {
return ar.length != 0;
}
if (value instanceof int[] ar) {
return ar.length != 0;
}
if (value instanceof long[] ar) {
return ar.length != 0;
}
if (value instanceof float[] ar) {
return ar.length != 0;
}
if (value instanceof double[] ar) {
return ar.length != 0;
}
if (value instanceof Object[] ar) {
return ar.length != 0;
}
if (value instanceof Collection<?> col) {
return !col.isEmpty();
}
return true;
}

@Override
Expand Down
Loading