Skip to content

DATAJDBC-587 - Convert identity value object to SQL type with custom converter in Finding and Deletion by property path. #242

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
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
* @author Tyler Van Gorder
* @author Milan Milanov
* @author Myeonghyeon Lee
* @author Myat Min
* @since 1.1
*/
public class DefaultDataAccessStrategy implements DataAccessStrategy {
Expand Down Expand Up @@ -227,7 +228,7 @@ public void delete(Object rootId, PersistentPropertyPath<RelationalPersistentPro
String delete = sql(rootEntity.getType()).createDeleteByPath(propertyPath);

SqlIdentifierParameterSource parameters = new SqlIdentifierParameterSource(getIdentifierProcessing());
parameters.addValue(ROOT_ID_PARAMETER, rootId);
addConvertedPropertyValue(parameters, ROOT_ID_PARAMETER, rootId, rootId.getClass());
operations.update(delete, parameters);
}

Expand Down Expand Up @@ -367,7 +368,9 @@ private SqlParameterSource createParameterSource(Identifier identifier, Identifi

SqlIdentifierParameterSource parameterSource = new SqlIdentifierParameterSource(identifierProcessing);

identifier.toMap().forEach(parameterSource::addValue);
identifier.toMap()
.forEach((sqlIdentifier, value) -> addConvertedPropertyValue(parameterSource,
sqlIdentifier, value, value.getClass()));

return parameterSource;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
import lombok.Data;
import lombok.RequiredArgsConstructor;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import lombok.Value;
import org.junit.Before;
Expand All @@ -37,11 +39,14 @@
import org.springframework.data.convert.ReadingConverter;
import org.springframework.data.convert.WritingConverter;
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
import org.springframework.data.mapping.PersistentPropertyPath;
import org.springframework.data.relational.core.dialect.Dialect;
import org.springframework.data.relational.core.dialect.HsqlDbDialect;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
import org.springframework.data.relational.core.sql.SqlIdentifier;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.KeyHolder;
Expand All @@ -52,6 +57,7 @@
* @author Jens Schauder
* @author Mark Paluch
* @author Myeonghyeon Lee
* @author Myat Min
*/
public class DefaultDataAccessStrategyUnitTests {

Expand Down Expand Up @@ -181,12 +187,64 @@ public void considersConfiguredWriteConverterForIdValueObjects() {
assertThat(paramSourceCaptor.getValue().getValue("id")).isEqualTo(rawId);
}

@Test // DATAJDBC-587
public void considersConfiguredWriteConverterForIdValueObjectsWhichReferencedInOneToManyRelationship() {

DelegatingDataAccessStrategy relationResolver = new DelegatingDataAccessStrategy();

Dialect dialect = HsqlDbDialect.INSTANCE;

JdbcConverter converter = new BasicJdbcConverter(context, relationResolver,
new JdbcCustomConversions(Arrays.asList(IdValueToStringConverter.INSTANCE)),
new DefaultJdbcTypeFactory(jdbcOperations), dialect.getIdentifierProcessing());

DefaultDataAccessStrategy accessStrategy = new DefaultDataAccessStrategy( //
new SqlGeneratorSource(context, converter, dialect), //
context, //
converter, //
namedJdbcOperations);

relationResolver.setDelegate(accessStrategy);

String rawId = "batman";
IdValue rootIdValue = new IdValue(rawId);

DummyEntityRoot root = new DummyEntityRoot(rootIdValue);
DummyEntity child = new DummyEntity(ORIGINAL_ID);
root.dummyEntities.add(child);

additionalParameters.put(SqlIdentifier.quoted("DUMMYENTITYROOT"), rootIdValue);
accessStrategy.insert(root, DummyEntityRoot.class, Identifier.from(additionalParameters));

verify(namedJdbcOperations).update(anyString(), paramSourceCaptor.capture(),
any(KeyHolder.class));

assertThat(paramSourceCaptor.getValue().getValue("id")).isEqualTo(rawId);

PersistentPropertyPath<RelationalPersistentProperty> path =
context.getPersistentPropertyPath("dummyEntities", DummyEntityRoot.class);

accessStrategy.findAllByPath(Identifier.from(additionalParameters), path);

verify(namedJdbcOperations).query(anyString(), paramSourceCaptor.capture(),
any(RowMapper.class));

assertThat(paramSourceCaptor.getValue().getValue("DUMMYENTITYROOT")).isEqualTo(rawId);
}

@RequiredArgsConstructor
private static class DummyEntity {

@Id private final Long id;
}

@RequiredArgsConstructor // DATAJDBC-587
private static class DummyEntityRoot {

@Id private final IdValue id;
List<DummyEntity> dummyEntities = new ArrayList<>();
}

@AllArgsConstructor
private static class EntityWithBoolean {

Expand Down