Skip to content

Commit ebc976f

Browse files
committed
probably reproduced the issue in test
1 parent a5bca63 commit ebc976f

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryCrossAggregateHsqlIntegrationTests.java

+66-1
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,38 @@
1515
*/
1616
package org.springframework.data.jdbc.repository;
1717

18+
import static java.util.Arrays.*;
1819
import static org.assertj.core.api.Assertions.*;
1920

2021
import org.junit.jupiter.api.Test;
2122
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.context.annotation.Bean;
2224
import org.springframework.context.annotation.ComponentScan;
2325
import org.springframework.context.annotation.Configuration;
2426
import org.springframework.context.annotation.FilterType;
2527
import org.springframework.context.annotation.Import;
28+
import org.springframework.core.convert.converter.Converter;
2629
import org.springframework.data.annotation.Id;
30+
import org.springframework.data.convert.CustomConversions;
31+
import org.springframework.data.convert.ReadingConverter;
32+
import org.springframework.data.convert.WritingConverter;
33+
import org.springframework.data.jdbc.core.convert.JdbcCustomConversions;
2734
import org.springframework.data.jdbc.core.mapping.AggregateReference;
35+
import org.springframework.data.jdbc.core.mapping.JdbcSimpleTypes;
2836
import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories;
2937
import org.springframework.data.jdbc.testing.DatabaseType;
3038
import org.springframework.data.jdbc.testing.EnabledOnDatabase;
3139
import org.springframework.data.jdbc.testing.IntegrationTest;
3240
import org.springframework.data.jdbc.testing.TestConfiguration;
41+
import org.springframework.data.mapping.model.SimpleTypeHolder;
42+
import org.springframework.data.relational.core.dialect.Dialect;
3343
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
3444
import org.springframework.data.repository.CrudRepository;
3545
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
3646
import org.springframework.test.jdbc.JdbcTestUtils;
3747

48+
import java.util.Collections;
49+
3850
/**
3951
* Very simple use cases for creation and usage of JdbcRepositories.
4052
*
@@ -52,13 +64,18 @@ public class JdbcRepositoryCrossAggregateHsqlIntegrationTests {
5264
@Configuration
5365
@Import(TestConfiguration.class)
5466
@EnableJdbcRepositories(considerNestedRepositories = true,
55-
includeFilters = @ComponentScan.Filter(value = Ones.class, type = FilterType.ASSIGNABLE_TYPE))
67+
includeFilters = @ComponentScan.Filter(value = {Ones.class, ReferencingAggregateRepository.class}, type = FilterType.ASSIGNABLE_TYPE))
5668
static class Config {
5769

70+
@Bean
71+
JdbcCustomConversions jdbcCustomConversions() {
72+
return new JdbcCustomConversions(asList( AggregateIdToLong.INSTANCE, LongToAggregateId.INSTANCE ));
73+
}
5874
}
5975

6076
@Autowired NamedParameterJdbcTemplate template;
6177
@Autowired Ones ones;
78+
@Autowired ReferencingAggregateRepository referencingAggregates;
6279
@Autowired RelationalMappingContext context;
6380

6481
@SuppressWarnings("ConstantConditions")
@@ -95,6 +112,18 @@ public void savesAndUpdate() {
95112
).isEqualTo(1);
96113
}
97114

115+
@Test // DATAJDBC-221
116+
public void savesAndReadWithConvertableId() {
117+
118+
119+
AggregateReference<AggregateWithConvertableId, AggregateId> idReference = AggregateReference.to(new AggregateId(TWO_ID));
120+
ReferencingAggregate reference = referencingAggregates.save(new ReferencingAggregate(null, "Reference", idReference));
121+
122+
123+
ReferencingAggregate reloaded = referencingAggregates.findById(reference.id).get();
124+
assertThat(reloaded.id()).isEqualTo(idReference);
125+
}
126+
98127
interface Ones extends CrudRepository<AggregateOne, Long> {}
99128

100129
static class AggregateOne {
@@ -109,4 +138,40 @@ static class AggregateTwo {
109138
@Id Long id;
110139
String name;
111140
}
141+
142+
interface ReferencingAggregateRepository extends CrudRepository<ReferencingAggregate, Long> {
143+
144+
}
145+
146+
record AggregateWithConvertableId(@Id AggregateId id, String name) {
147+
148+
}
149+
150+
record AggregateId(Long value) {
151+
152+
}
153+
154+
record ReferencingAggregate(@Id Long id, String name,
155+
AggregateReference<AggregateWithConvertableId, AggregateId> ref) {
156+
}
157+
158+
@WritingConverter
159+
enum AggregateIdToLong implements Converter<AggregateId, Long> {
160+
INSTANCE;
161+
162+
@Override
163+
public Long convert(AggregateId source) {
164+
return source.value;
165+
}
166+
}
167+
168+
@ReadingConverter
169+
enum LongToAggregateId implements Converter< Long,AggregateId> {
170+
INSTANCE;
171+
172+
@Override
173+
public AggregateId convert(Long source) {
174+
return new AggregateId(source);
175+
}
176+
}
112177
}

spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryCrossAggregateHsqlIntegrationTests-hsql.sql

+7
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,10 @@ CREATE TABLE AGGREGATE_ONE
44
NAME VARCHAR(100),
55
TWO INTEGER
66
);
7+
8+
CREATE TABLE REFERENCING_AGGREGATE
9+
(
10+
ID BIGINT GENERATED BY DEFAULT AS IDENTITY ( START WITH 1 ) PRIMARY KEY,
11+
NAME VARCHAR(100),
12+
REF INTEGER
13+
);

0 commit comments

Comments
 (0)