|
24 | 24 | import lombok.Data;
|
25 | 25 | import lombok.RequiredArgsConstructor;
|
26 | 26 |
|
| 27 | +import java.util.ArrayList; |
27 | 28 | import java.util.Arrays;
|
28 | 29 | import java.util.HashMap;
|
| 30 | +import java.util.List; |
29 | 31 |
|
30 | 32 | import lombok.Value;
|
31 | 33 | import org.junit.Before;
|
|
37 | 39 | import org.springframework.data.convert.ReadingConverter;
|
38 | 40 | import org.springframework.data.convert.WritingConverter;
|
39 | 41 | import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
|
| 42 | +import org.springframework.data.mapping.PersistentPropertyPath; |
40 | 43 | import org.springframework.data.relational.core.dialect.Dialect;
|
41 | 44 | import org.springframework.data.relational.core.dialect.HsqlDbDialect;
|
42 | 45 | import org.springframework.data.relational.core.mapping.RelationalMappingContext;
|
| 46 | +import org.springframework.data.relational.core.mapping.RelationalPersistentProperty; |
43 | 47 | import org.springframework.data.relational.core.sql.SqlIdentifier;
|
44 | 48 | import org.springframework.jdbc.core.JdbcOperations;
|
| 49 | +import org.springframework.jdbc.core.RowMapper; |
45 | 50 | import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
|
46 | 51 | import org.springframework.jdbc.core.namedparam.SqlParameterSource;
|
47 | 52 | import org.springframework.jdbc.support.KeyHolder;
|
|
52 | 57 | * @author Jens Schauder
|
53 | 58 | * @author Mark Paluch
|
54 | 59 | * @author Myeonghyeon Lee
|
| 60 | + * @author Myat Min |
55 | 61 | */
|
56 | 62 | public class DefaultDataAccessStrategyUnitTests {
|
57 | 63 |
|
@@ -181,12 +187,64 @@ public void considersConfiguredWriteConverterForIdValueObjects() {
|
181 | 187 | assertThat(paramSourceCaptor.getValue().getValue("id")).isEqualTo(rawId);
|
182 | 188 | }
|
183 | 189 |
|
| 190 | + @Test // DATAJDBC-587 |
| 191 | + public void considersConfiguredWriteConverterForIdValueObjectsWhichReferencedInOneToManyRelationship() { |
| 192 | + |
| 193 | + DelegatingDataAccessStrategy relationResolver = new DelegatingDataAccessStrategy(); |
| 194 | + |
| 195 | + Dialect dialect = HsqlDbDialect.INSTANCE; |
| 196 | + |
| 197 | + JdbcConverter converter = new BasicJdbcConverter(context, relationResolver, |
| 198 | + new JdbcCustomConversions(Arrays.asList(IdValueToStringConverter.INSTANCE)), |
| 199 | + new DefaultJdbcTypeFactory(jdbcOperations), dialect.getIdentifierProcessing()); |
| 200 | + |
| 201 | + DefaultDataAccessStrategy accessStrategy = new DefaultDataAccessStrategy( // |
| 202 | + new SqlGeneratorSource(context, converter, dialect), // |
| 203 | + context, // |
| 204 | + converter, // |
| 205 | + namedJdbcOperations); |
| 206 | + |
| 207 | + relationResolver.setDelegate(accessStrategy); |
| 208 | + |
| 209 | + String rawId = "batman"; |
| 210 | + IdValue rootIdValue = new IdValue(rawId); |
| 211 | + |
| 212 | + DummyEntityRoot root = new DummyEntityRoot(rootIdValue); |
| 213 | + DummyEntity child = new DummyEntity(ORIGINAL_ID); |
| 214 | + root.dummyEntities.add(child); |
| 215 | + |
| 216 | + additionalParameters.put(SqlIdentifier.quoted("DUMMYENTITYROOT"), rootIdValue); |
| 217 | + accessStrategy.insert(root, DummyEntityRoot.class, Identifier.from(additionalParameters)); |
| 218 | + |
| 219 | + verify(namedJdbcOperations).update(anyString(), paramSourceCaptor.capture(), |
| 220 | + any(KeyHolder.class)); |
| 221 | + |
| 222 | + assertThat(paramSourceCaptor.getValue().getValue("id")).isEqualTo(rawId); |
| 223 | + |
| 224 | + PersistentPropertyPath<RelationalPersistentProperty> path = |
| 225 | + context.getPersistentPropertyPath("dummyEntities", DummyEntityRoot.class); |
| 226 | + |
| 227 | + accessStrategy.findAllByPath(Identifier.from(additionalParameters), path); |
| 228 | + |
| 229 | + verify(namedJdbcOperations).query(anyString(), paramSourceCaptor.capture(), |
| 230 | + any(RowMapper.class)); |
| 231 | + |
| 232 | + assertThat(paramSourceCaptor.getValue().getValue("DUMMYENTITYROOT")).isEqualTo(rawId); |
| 233 | + } |
| 234 | + |
184 | 235 | @RequiredArgsConstructor
|
185 | 236 | private static class DummyEntity {
|
186 | 237 |
|
187 | 238 | @Id private final Long id;
|
188 | 239 | }
|
189 | 240 |
|
| 241 | + @RequiredArgsConstructor // DATAJDBC-587 |
| 242 | + private static class DummyEntityRoot { |
| 243 | + |
| 244 | + @Id private final IdValue id; |
| 245 | + List<DummyEntity> dummyEntities = new ArrayList<>(); |
| 246 | + } |
| 247 | + |
190 | 248 | @AllArgsConstructor
|
191 | 249 | private static class EntityWithBoolean {
|
192 | 250 |
|
|
0 commit comments