33
33
import org .springframework .context .annotation .Bean ;
34
34
import org .springframework .context .annotation .ComponentScan ;
35
35
import org .springframework .context .annotation .FilterType ;
36
+ import org .springframework .context .annotation .Lazy ;
36
37
import org .springframework .data .annotation .Id ;
37
38
import org .springframework .data .jdbc .core .JdbcAggregateTemplate ;
39
+ import org .springframework .data .jdbc .core .convert .BasicJdbcConverter ;
38
40
import org .springframework .data .jdbc .core .convert .DataAccessStrategy ;
39
41
import org .springframework .data .jdbc .core .convert .DefaultDataAccessStrategy ;
42
+ import org .springframework .data .jdbc .core .convert .DefaultJdbcTypeFactory ;
40
43
import org .springframework .data .jdbc .core .convert .JdbcConverter ;
44
+ import org .springframework .data .jdbc .core .convert .JdbcCustomConversions ;
41
45
import org .springframework .data .jdbc .core .convert .SqlGeneratorSource ;
46
+ import org .springframework .data .jdbc .core .mapping .JdbcMappingContext ;
42
47
import org .springframework .data .jdbc .repository .QueryMappingConfiguration ;
43
48
import org .springframework .data .jdbc .repository .config .EnableJdbcRepositoriesIntegrationTests .TestConfiguration ;
44
49
import org .springframework .data .jdbc .repository .support .JdbcRepositoryFactoryBean ;
45
50
import org .springframework .data .mapping .PersistentEntity ;
46
51
import org .springframework .data .relational .core .dialect .Dialect ;
52
+ import org .springframework .data .relational .core .mapping .NamingStrategy ;
47
53
import org .springframework .data .relational .core .mapping .RelationalMappingContext ;
48
54
import org .springframework .data .repository .CrudRepository ;
49
55
import org .springframework .jdbc .core .RowMapper ;
60
66
* @author Greg Turnquist
61
67
* @author Evgeni Dimitrov
62
68
* @author Fei Dong
69
+ * @author Tomohiko Ozawa
63
70
*/
64
71
@ ExtendWith (SpringExtension .class )
65
72
@ ContextConfiguration (classes = TestConfiguration .class )
@@ -70,22 +77,26 @@ public class EnableJdbcRepositoriesIntegrationTests {
70
77
static final Field OPERATIONS = ReflectionUtils .findField (JdbcRepositoryFactoryBean .class , "operations" );
71
78
static final Field DATA_ACCESS_STRATEGY = ReflectionUtils .findField (JdbcRepositoryFactoryBean .class ,
72
79
"dataAccessStrategy" );
80
+ static final Field CONVERTER = ReflectionUtils .findField (JdbcRepositoryFactoryBean .class , "converter" );
73
81
public static final RowMapper DUMMY_ENTITY_ROW_MAPPER = mock (RowMapper .class );
74
82
public static final RowMapper STRING_ROW_MAPPER = mock (RowMapper .class );
75
83
76
84
@ Autowired JdbcRepositoryFactoryBean factoryBean ;
77
85
@ Autowired DummyRepository repository ;
78
86
@ Autowired @ Qualifier ("namedParameterJdbcTemplate" ) NamedParameterJdbcOperations defaultOperations ;
79
87
@ Autowired @ Qualifier ("defaultDataAccessStrategy" ) DataAccessStrategy defaultDataAccessStrategy ;
88
+ @ Autowired @ Qualifier ("converter" ) JdbcConverter defaultJdbcConverter ;
80
89
@ Autowired @ Qualifier ("qualifierJdbcOperations" ) NamedParameterJdbcOperations qualifierJdbcOperations ;
81
90
@ Autowired @ Qualifier ("qualifierDataAccessStrategy" ) DataAccessStrategy qualifierDataAccessStrategy ;
91
+ @ Autowired @ Qualifier ("qualifierJdbcConverter" ) JdbcConverter qualifierJdbcConverter ;
82
92
83
93
@ BeforeAll
84
94
public static void setup () {
85
95
86
96
MAPPER_MAP .setAccessible (true );
87
97
OPERATIONS .setAccessible (true );
88
98
DATA_ACCESS_STRATEGY .setAccessible (true );
99
+ CONVERTER .setAccessible (true );
89
100
}
90
101
91
102
@ Test // DATAJDBC-100
@@ -118,6 +129,9 @@ public void jdbcOperationsRef() {
118
129
DataAccessStrategy dataAccessStrategy = (DataAccessStrategy ) ReflectionUtils .getField (DATA_ACCESS_STRATEGY ,
119
130
factoryBean );
120
131
assertThat (dataAccessStrategy ).isNotSameAs (defaultDataAccessStrategy ).isSameAs (qualifierDataAccessStrategy );
132
+
133
+ JdbcConverter converter = (JdbcConverter ) ReflectionUtils .getField (CONVERTER , factoryBean );
134
+ assertThat (converter ).isNotSameAs (defaultJdbcConverter ).isSameAs (qualifierJdbcConverter );
121
135
}
122
136
123
137
interface DummyRepository extends CrudRepository <DummyEntity , Long > {
@@ -133,7 +147,7 @@ static class DummyEntity {
133
147
@ EnableJdbcRepositories (considerNestedRepositories = true ,
134
148
includeFilters = @ ComponentScan .Filter (type = FilterType .ASSIGNABLE_TYPE , classes = DummyRepository .class ),
135
149
jdbcOperationsRef = "qualifierJdbcOperations" , dataAccessStrategyRef = "qualifierDataAccessStrategy" ,
136
- repositoryBaseClass = DummyRepositoryBaseClass .class )
150
+ jdbcConverterRef = "qualifierJdbcConverter" , repositoryBaseClass = DummyRepositoryBaseClass .class )
137
151
static class TestConfiguration {
138
152
139
153
@ Bean
@@ -166,6 +180,19 @@ DataAccessStrategy defaultDataAccessStrategy(
166
180
Dialect jdbcDialect (@ Qualifier ("qualifierJdbcOperations" ) NamedParameterJdbcOperations operations ) {
167
181
return DialectResolver .getDialect (operations .getJdbcOperations ());
168
182
}
183
+
184
+ @ Bean ("qualifierJdbcConverter" )
185
+ JdbcConverter qualifierJdbcConverter (Optional <NamingStrategy > namingStrategy ,
186
+ @ Qualifier ("qualifierJdbcOperations" ) NamedParameterJdbcOperations operations ,
187
+ @ Lazy @ Qualifier ("qualifierDataAccessStrategy" ) DataAccessStrategy dataAccessStrategy ) {
188
+ JdbcCustomConversions conversions = new JdbcCustomConversions ();
189
+ JdbcMappingContext mappingContext = new JdbcMappingContext (namingStrategy .orElse (NamingStrategy .INSTANCE ));
190
+ mappingContext .setSimpleTypeHolder (conversions .getSimpleTypeHolder ());
191
+ DefaultJdbcTypeFactory jdbcTypeFactory = new DefaultJdbcTypeFactory (operations .getJdbcOperations ());
192
+ Dialect dialect = DialectResolver .getDialect (operations .getJdbcOperations ());
193
+ return new BasicJdbcConverter (mappingContext , dataAccessStrategy , conversions , jdbcTypeFactory ,
194
+ dialect .getIdentifierProcessing ());
195
+ }
169
196
}
170
197
171
198
private static class DummyRepositoryBaseClass <T , ID > implements CrudRepository <T , ID > {
0 commit comments