Skip to content

Commit

Permalink
Adopt immutables test to newly introduced converter infrastructure.
Browse files Browse the repository at this point in the history
See #669
  • Loading branch information
mp911de committed Oct 25, 2023
1 parent 20b779e commit 6430790
Showing 1 changed file with 10 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,21 @@
*/
package example.springdata.jdbc.immutables;

import java.sql.ResultSet;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.io.ResourceLoader;
import org.springframework.data.jdbc.core.convert.BasicJdbcConverter;
import org.springframework.data.jdbc.core.convert.DefaultJdbcTypeFactory;
import org.springframework.data.jdbc.core.convert.Identifier;
import org.springframework.data.jdbc.core.convert.JdbcConverter;
import org.springframework.data.jdbc.core.convert.JdbcCustomConversions;
import org.springframework.data.jdbc.core.convert.MappingJdbcConverter;
import org.springframework.data.jdbc.core.convert.RelationResolver;
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
import org.springframework.data.jdbc.repository.config.AbstractJdbcConfiguration;
import org.springframework.data.mapping.PersistentPropertyPath;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.relational.core.conversion.RowDocumentAccessor;
import org.springframework.data.relational.core.dialect.Dialect;
import org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
import org.springframework.data.util.TypeInformation;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
import org.springframework.util.ClassUtils;

Expand Down Expand Up @@ -78,27 +73,17 @@ public JdbcConverter jdbcConverter(JdbcMappingContext mappingContext, NamedParam

var jdbcTypeFactory = new DefaultJdbcTypeFactory(operations.getJdbcOperations());

return new BasicJdbcConverter(mappingContext, relationResolver, conversions, jdbcTypeFactory,
dialect.getIdentifierProcessing()) {

@Override
public <T> T mapRow(RelationalPersistentEntity<T> entity, ResultSet resultSet, Object key) {

// rows will not be mapped to the entity interface, but to its implementation generated by Immutable
RelationalPersistentEntity<T> implementationEntity = getImplementationEntity(mappingContext, entity);
return super.mapRow(implementationEntity, resultSet, key);
}
return new MappingJdbcConverter(mappingContext, relationResolver, conversions, jdbcTypeFactory) {

@Override
public <T> T mapRow(PersistentPropertyPathExtension path, ResultSet resultSet, Identifier identifier,
Object key) {
@SuppressWarnings("all")
protected <S> S readAggregate(ConversionContext context, RowDocumentAccessor documentAccessor,
TypeInformation<? extends S> typeHint) {

// rows will not be mapped to the entity interface, but to its implementation generated by Immutable
RelationalPersistentEntity<?> implementationEntity = getImplementationEntity(mappingContext,
path.getLeafEntity());
var propertyPath = new DelegatePersistentPropertyPathExtension(mappingContext,
path.getRequiredPersistentPropertyPath(), implementationEntity);
return super.mapRow(propertyPath, resultSet, identifier, key);
mappingContext.getRequiredPersistentEntity(typeHint));

return (S) super.readAggregate(context, documentAccessor, implementationEntity.getTypeInformation());
}
};
}
Expand Down Expand Up @@ -126,26 +111,4 @@ private <T> RelationalPersistentEntity<T> getImplementationEntity(JdbcMappingCon
}
}

/**
* Redirect {@link #getLeafEntity()} to a different entity type.
*/
static class DelegatePersistentPropertyPathExtension extends PersistentPropertyPathExtension {

private final RelationalPersistentEntity<?> leafEntity;

public DelegatePersistentPropertyPathExtension(
MappingContext<? extends RelationalPersistentEntity<?>, ? extends RelationalPersistentProperty> context,
PersistentPropertyPath<? extends RelationalPersistentProperty> path, RelationalPersistentEntity<?> leafEntity) {

super(context, path);

this.leafEntity = leafEntity;
}

@Override
public RelationalPersistentEntity<?> getLeafEntity() {
return leafEntity;
}
}

}

0 comments on commit 6430790

Please sign in to comment.