From 6430790e63df6b7a5f3415fd152e58a6c083a2f7 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Wed, 25 Oct 2023 12:53:13 +0200 Subject: [PATCH] Adopt immutables test to newly introduced converter infrastructure. See #669 --- .../jdbc/immutables/Application.java | 57 ++++--------------- 1 file changed, 10 insertions(+), 47 deletions(-) diff --git a/jdbc/immutables/src/main/java/example/springdata/jdbc/immutables/Application.java b/jdbc/immutables/src/main/java/example/springdata/jdbc/immutables/Application.java index 9b76b34af..47f456eb5 100644 --- a/jdbc/immutables/src/main/java/example/springdata/jdbc/immutables/Application.java +++ b/jdbc/immutables/src/main/java/example/springdata/jdbc/immutables/Application.java @@ -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; @@ -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 mapRow(RelationalPersistentEntity entity, ResultSet resultSet, Object key) { - - // rows will not be mapped to the entity interface, but to its implementation generated by Immutable - RelationalPersistentEntity implementationEntity = getImplementationEntity(mappingContext, entity); - return super.mapRow(implementationEntity, resultSet, key); - } + return new MappingJdbcConverter(mappingContext, relationResolver, conversions, jdbcTypeFactory) { @Override - public T mapRow(PersistentPropertyPathExtension path, ResultSet resultSet, Identifier identifier, - Object key) { + @SuppressWarnings("all") + protected S readAggregate(ConversionContext context, RowDocumentAccessor documentAccessor, + TypeInformation 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()); } }; } @@ -126,26 +111,4 @@ private RelationalPersistentEntity getImplementationEntity(JdbcMappingCon } } - /** - * Redirect {@link #getLeafEntity()} to a different entity type. - */ - static class DelegatePersistentPropertyPathExtension extends PersistentPropertyPathExtension { - - private final RelationalPersistentEntity leafEntity; - - public DelegatePersistentPropertyPathExtension( - MappingContext, ? extends RelationalPersistentProperty> context, - PersistentPropertyPath path, RelationalPersistentEntity leafEntity) { - - super(context, path); - - this.leafEntity = leafEntity; - } - - @Override - public RelationalPersistentEntity getLeafEntity() { - return leafEntity; - } - } - }