Skip to content

Fix loading of nested embedded entities #1685

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 3 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.3.0-SNAPSHOT</version>
<version>3.3.0-npe-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.3.0-SNAPSHOT</version>
<version>3.3.0-npe-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.3.0-SNAPSHOT</version>
<version>3.3.0-npe-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.3.0-SNAPSHOT</version>
<version>3.3.0-npe-SNAPSHOT</version>
</parent>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,21 @@ public void findAllFindsAllEntities() {
.containsExactlyInAnyOrder(entity.getId(), other.getId());
}

@Test // GH-1676
public void findAllFindsAllEntitiesWithOnlyReferenceNotNull() {

DummyEntity entity = createDummyEntity();
entity.prefixedEmbeddable.test = null;
entity = repository.save(entity);
DummyEntity other = repository.save(createDummyEntity());

Iterable<DummyEntity> all = repository.findAll();

assertThat(all)//
.extracting(DummyEntity::getId)//
.containsExactlyInAnyOrder(entity.getId(), other.getId());
}

@Test // DATAJDBC-111
public void findByIdReturnsEmptyWhenNoneFound() {

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.3.0-SNAPSHOT</version>
<version>3.3.0-npe-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.3.0-SNAPSHOT</version>
<version>3.3.0-npe-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.3.0-SNAPSHOT</version>
<version>3.3.0-npe-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.3.0-SNAPSHOT</version>
<version>3.3.0-npe-SNAPSHOT</version>
</parent>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,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.ConvertingPropertyAccessor;
import org.springframework.data.mapping.model.DefaultSpELExpressionEvaluator;
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.SpELExpressionEvaluator;
import org.springframework.data.mapping.model.SpELExpressionParameterValueProvider;
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 @@ -307,7 +298,7 @@ protected <S> S read(TypeInformation<S> type, RowDocument source) {
* @return the converted object, will never be {@literal null}.
*/
protected <S> S readAggregate(ConversionContext context, RowDocument document,
TypeInformation<? extends S> typeHint) {
TypeInformation<? extends S> typeHint) {
return readAggregate(context, new RowDocumentAccessor(document), typeHint);
}

Expand All @@ -321,7 +312,7 @@ protected <S> S readAggregate(ConversionContext context, RowDocument document,
*/
@SuppressWarnings("unchecked")
protected <S> S readAggregate(ConversionContext context, RowDocumentAccessor documentAccessor,
TypeInformation<? extends S> typeHint) {
TypeInformation<? extends S> typeHint) {

Class<? extends S> rawType = typeHint.getType();

Expand Down Expand Up @@ -430,8 +421,7 @@ private <T> T doConvert(Object value, Class<? extends T> target) {
}

@SuppressWarnings("ConstantConditions")
private <T> T doConvert(Object value, Class<? extends T> target,
@Nullable Class<? extends T> fallback) {
private <T> T doConvert(Object value, Class<? extends T> target, @Nullable Class<? extends T> fallback) {

if (getConversionService().canConvert(value.getClass(), target) || fallback == null) {
return getConversionService().convert(value, target);
Expand Down Expand Up @@ -504,7 +494,7 @@ public RelationalPropertyValueProvider withContext(ConversionContext context) {
entity, contextualizing, context.getPath().getCurrentObject());

return new ConverterAwareSpELExpressionParameterValueProvider(context, evaluator, getConversionService(),
new ConvertingParameterValueProvider<>( parameterProvider::getParameterValue));
new ConvertingParameterValueProvider<>(parameterProvider::getParameterValue));
}

private <S> S populateProperties(ConversionContext context, RelationalPersistentEntity<S> entity,
Expand Down Expand Up @@ -580,10 +570,19 @@ private boolean shouldReadEmbeddable(ConversionContext context, RelationalPersis

for (RelationalPersistentProperty persistentProperty : unwrappedEntity) {

RelationalPropertyValueProvider contextual = propertyValueProvider
.withContext(context.forProperty(persistentProperty));
ConversionContext nestedContext = context.forProperty(persistentProperty);
RelationalPropertyValueProvider contextual = propertyValueProvider.withContext(nestedContext);

if (contextual.hasValue(persistentProperty)) {
if (persistentProperty.isEmbedded()) {

RelationalPersistentEntity<?> nestedEntity = getMappingContext()
.getRequiredPersistentEntity(persistentProperty);

if (shouldReadEmbeddable(nestedContext, persistentProperty, nestedEntity, contextual)) {
return true;
}

} else if (contextual.hasValue(persistentProperty)) {
return true;
}
}
Expand Down Expand Up @@ -787,8 +786,7 @@ protected DefaultConversionContext(RelationalConverter sourceConverter,

@SuppressWarnings("unchecked")
@Override
public <S> S convert(Object source, TypeInformation<? extends S> typeHint,
ConversionContext context) {
public <S> S convert(Object source, TypeInformation<? extends S> typeHint, ConversionContext context) {

Assert.notNull(source, "Source must not be null");
Assert.notNull(typeHint, "TypeInformation must not be null");
Expand Down Expand Up @@ -1196,7 +1194,7 @@ protected <T> T potentiallyConvertSpelValue(Object object, Parameter<T, Relation
}
}

private record PropertyTranslatingPropertyAccessor<T> (PersistentPropertyAccessor<T> delegate,
private record PropertyTranslatingPropertyAccessor<T>(PersistentPropertyAccessor<T> delegate,
PersistentPropertyTranslator propertyTranslator) implements PersistentPropertyAccessor<T> {

static <T> PersistentPropertyAccessor<T> create(PersistentPropertyAccessor<T> delegate,
Expand Down