Skip to content

DATAJDBC-370 - Fixed handling of entities with no withers. #151

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 6 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>1.1.0.BUILD-SNAPSHOT</version>
<version>1.1.0.DATAJDBC-370-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>1.1.0.BUILD-SNAPSHOT</version>
<version>1.1.0.DATAJDBC-370-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 @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-jdbc</artifactId>
<version>1.1.0.BUILD-SNAPSHOT</version>
<version>1.1.0.DATAJDBC-370-SNAPSHOT</version>

<name>Spring Data JDBC</name>
<description>Spring Data module for JDBC repositories.</description>
Expand All @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>1.1.0.BUILD-SNAPSHOT</version>
<version>1.1.0.DATAJDBC-370-SNAPSHOT</version>
</parent>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.springframework.data.jdbc.core.mapping.AggregateReference;
import org.springframework.data.jdbc.support.JdbcUtil;
import org.springframework.data.mapping.MappingException;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PersistentPropertyAccessor;
import org.springframework.data.mapping.PreferredConstructor;
import org.springframework.data.mapping.context.MappingContext;
Expand All @@ -53,6 +52,7 @@
*
* @author Mark Paluch
* @author Jens Schauder
* @author Christoph Strobl
* @see MappingContext
* @see SimpleTypeHolder
* @see CustomConversions
Expand Down Expand Up @@ -285,30 +285,22 @@ public ReadingContext(RelationalPersistentEntity<T> entity, DataAccessStrategy a
}

private ReadingContext<?> extendBy(RelationalPersistentProperty property) {
return new ReadingContext<>(entity, accessStrategy, resultSet, path.extendBy(property));
return new ReadingContext(getMappingContext().getRequiredPersistentEntity(property.getActualType()),
accessStrategy, resultSet, path.extendBy(property));
}

T mapRow() {

RelationalPersistentProperty idProperty = entity.getIdProperty();

Object idValue = null;
if (idProperty != null) {
idValue = readFrom(idProperty);
}
Object idValue = idProperty == null ? null : readFrom(idProperty);

T result = createInstanceInternal(entity, idValue);

return entity.requiresPropertyPopulation() //
? populateProperties(result) //
: result;
return createInstanceInternal(idValue);
}

private T populateProperties(T result) {

PersistentPropertyAccessor<T> propertyAccessor = getPropertyAccessor(entity, result);
private T populateProperties(T instance, @Nullable Object idValue) {

Object id = idProperty == null ? null : readFrom(idProperty);
PersistentPropertyAccessor<T> propertyAccessor = getPropertyAccessor(entity, instance);

PreferredConstructor<T, RelationalPersistentProperty> persistenceConstructor = entity.getPersistenceConstructor();

Expand All @@ -318,7 +310,7 @@ private T populateProperties(T result) {
continue;
}

propertyAccessor.setProperty(property, readOrLoadProperty(id, property));
propertyAccessor.setProperty(property, readOrLoadProperty(idValue, property));
}

return propertyAccessor.getBean();
Expand Down Expand Up @@ -354,31 +346,38 @@ private Object readFrom(RelationalPersistentProperty property) {

Object value = getObjectFromResultSet(path.extendBy(property).getColumnAlias());
return readValue(value, property.getTypeInformation());

}

@SuppressWarnings("unchecked")
private Object readEmbeddedEntityFrom(@Nullable Object id, RelationalPersistentProperty property) {
@Nullable
private Object readEmbeddedEntityFrom(@Nullable Object idValue, RelationalPersistentProperty property) {

ReadingContext newContext = extendBy(property);
ReadingContext<?> newContext = extendBy(property);
return newContext.hasInstanceValues(idValue) ? newContext.createInstanceInternal(idValue) : null;
}

RelationalPersistentEntity<?> entity = getMappingContext().getRequiredPersistentEntity(property.getActualType());
private boolean hasInstanceValues(Object idValue) {

Object instance = newContext.createInstanceInternal(entity, null);
RelationalPersistentEntity<?> persistentEntity = path.getLeafEntity();

PersistentPropertyAccessor<?> accessor = getPropertyAccessor((PersistentEntity<Object, ?>) entity, instance);
for (RelationalPersistentProperty embeddedProperty : persistentEntity) {

for (RelationalPersistentProperty p : entity) {
accessor.setProperty(p, newContext.readOrLoadProperty(id, p));
// if the embedded contains Lists, Sets or Maps we consider it non-empty
if (embeddedProperty.isQualified() || embeddedProperty.isReference()) {
return true;
}

if (readOrLoadProperty(idValue, embeddedProperty) != null) {
return true;
}
}

return instance;
return false;
}

@Nullable
private <S> S readEntityFrom(RelationalPersistentProperty property, PersistentPropertyPathExtension path) {

ReadingContext<?> newContext = extendBy(property);
ReadingContext<S> newContext = (ReadingContext<S>) extendBy(property);

RelationalPersistentEntity<S> entity = (RelationalPersistentEntity<S>) getMappingContext()
.getRequiredPersistentEntity(property.getActualType());
Expand All @@ -398,15 +397,7 @@ private <S> S readEntityFrom(RelationalPersistentProperty property, PersistentPr
return null;
}

S instance = newContext.createInstanceInternal(entity, idValue);

PersistentPropertyAccessor<S> accessor = getPropertyAccessor(entity, instance);

for (RelationalPersistentProperty p : entity) {
accessor.setProperty(p, newContext.readOrLoadProperty(idValue, p));
}

return instance;
return newContext.createInstanceInternal(idValue);
}

@Nullable
Expand All @@ -419,9 +410,9 @@ private Object getObjectFromResultSet(String backreferenceName) {
}
}

private <S> S createInstanceInternal(RelationalPersistentEntity<S> entity, @Nullable Object idValue) {
private T createInstanceInternal(@Nullable Object idValue) {

return createInstance(entity, parameter -> {
T instance = createInstance(entity, parameter -> {

String parameterName = parameter.getName();

Expand All @@ -431,6 +422,7 @@ private <S> S createInstanceInternal(RelationalPersistentEntity<S> entity, @Null

return readOrLoadProperty(idValue, property);
});
return populateProperties(instance, idValue);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
* @author Mark Paluch
* @author Thomas Lang
* @author Bastian Wilhelm
* @author Christoph Strobl
* @since 1.1
*/
public class DefaultDataAccessStrategy implements DataAccessStrategy {
Expand Down Expand Up @@ -313,7 +314,8 @@ private <S, T> MapSqlParameterSource getParameterSource(S instance, RelationalPe

MapSqlParameterSource parameters = new MapSqlParameterSource();

PersistentPropertyAccessor<S> propertyAccessor = persistentEntity.getPropertyAccessor(instance);
PersistentPropertyAccessor<S> propertyAccessor = instance != null ? persistentEntity.getPropertyAccessor(instance)
: NoValuePropertyAccessor.instance();

persistentEntity.doWithProperties((PropertyHandler<RelationalPersistentProperty>) property -> {

Expand Down Expand Up @@ -480,4 +482,33 @@ static Predicate<RelationalPersistentProperty> includeAll() {
return it -> false;
}
}

/**
* A {@link PersistentPropertyAccessor} implementation always returning null
*
* @param <T>
*/
static class NoValuePropertyAccessor<T> implements PersistentPropertyAccessor<T> {

private static final NoValuePropertyAccessor INSTANCE = new NoValuePropertyAccessor();

static <T> NoValuePropertyAccessor<T> instance() {
return INSTANCE;
}

@Override
public void setProperty(PersistentProperty<?> property, Object value) {
throw new UnsupportedOperationException("Cannot set value on 'null' target object.");
}

@Override
public Object getProperty(PersistentProperty<?> property) {
return null;
}

@Override
public T getBean() {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ public void saveAndLoadAnEntityWithByteArray() {
assertThat(reloaded.binaryData).isEqualTo(new byte[] { 1, 23, 42 });
}

@Test
@Test // DATAJDBC-340
public void saveAndLoadLongChain() {

Chain4 chain4 = new Chain4();
Expand Down
Loading