23
23
import org .springframework .core .convert .support .GenericConversionService ;
24
24
import org .springframework .data .annotation .Id ;
25
25
import org .springframework .data .convert .Jsr310Converters ;
26
+ import org .springframework .data .jdbc .mapping .model .DefaultNamingStrategy ;
26
27
import org .springframework .data .jdbc .mapping .model .JdbcMappingContext ;
27
28
import org .springframework .data .jdbc .mapping .model .JdbcPersistentEntity ;
28
29
import org .springframework .data .jdbc .mapping .model .JdbcPersistentProperty ;
30
+ import org .springframework .data .jdbc .mapping .model .NamingStrategy ;
31
+ import org .springframework .data .repository .query .Param ;
29
32
import org .springframework .jdbc .core .namedparam .NamedParameterJdbcOperations ;
30
33
import org .springframework .util .Assert ;
31
34
@@ -54,6 +57,12 @@ public class EntityRowMapperUnitTests {
54
57
public static final long ID_FOR_ENTITY_REFERENCING_MAP = 42L ;
55
58
public static final long ID_FOR_ENTITY_REFERENCING_LIST = 4711L ;
56
59
public static final long ID_FOR_ENTITY_NOT_REFERENCING_MAP = 23L ;
60
+ public static final DefaultNamingStrategy X_APPENDING_NAMINGSTRATEGY = new DefaultNamingStrategy () {
61
+ @ Override
62
+ public String getColumnName (JdbcPersistentProperty property ) {
63
+ return super .getColumnName (property ) + "x" ;
64
+ }
65
+ };
57
66
58
67
@ Test // DATAJDBC-113
59
68
public void simpleEntitiesGetProperlyExtracted () throws SQLException {
@@ -70,6 +79,36 @@ public void simpleEntitiesGetProperlyExtracted() throws SQLException {
70
79
.containsExactly (ID_FOR_ENTITY_NOT_REFERENCING_MAP , "alpha" );
71
80
}
72
81
82
+ @ Test // DATAJDBC-181
83
+ public void namingStrategyGetsHonored () throws SQLException {
84
+
85
+ ResultSet rs = mockResultSet (asList ("idx" , "namex" ), //
86
+ ID_FOR_ENTITY_NOT_REFERENCING_MAP , "alpha" );
87
+ rs .next ();
88
+
89
+ Trivial extracted = createRowMapper (Trivial .class , X_APPENDING_NAMINGSTRATEGY ).mapRow (rs , 1 );
90
+
91
+ assertThat (extracted ) //
92
+ .isNotNull () //
93
+ .extracting (e -> e .id , e -> e .name ) //
94
+ .containsExactly (ID_FOR_ENTITY_NOT_REFERENCING_MAP , "alpha" );
95
+ }
96
+
97
+ @ Test // DATAJDBC-181
98
+ public void namingStrategyGetsHonoredForConstructor () throws SQLException {
99
+
100
+ ResultSet rs = mockResultSet (asList ("idx" , "namex" ), //
101
+ ID_FOR_ENTITY_NOT_REFERENCING_MAP , "alpha" );
102
+ rs .next ();
103
+
104
+ TrivialImmutable extracted = createRowMapper (TrivialImmutable .class , X_APPENDING_NAMINGSTRATEGY ).mapRow (rs , 1 );
105
+
106
+ assertThat (extracted ) //
107
+ .isNotNull () //
108
+ .extracting (e -> e .id , e -> e .name ) //
109
+ .containsExactly (ID_FOR_ENTITY_NOT_REFERENCING_MAP , "alpha" );
110
+ }
111
+
73
112
@ Test // DATAJDBC-113
74
113
public void simpleOneToOneGetsProperlyExtracted () throws SQLException {
75
114
@@ -131,8 +170,18 @@ public void listReferenceGetsLoadedWithAdditionalSelect() throws SQLException {
131
170
}
132
171
133
172
private <T > EntityRowMapper <T > createRowMapper (Class <T > type ) {
173
+ return createRowMapper (type , new DefaultNamingStrategy ());
174
+ }
175
+
176
+ private <T > EntityRowMapper <T > createRowMapper (Class <T > type , NamingStrategy namingStrategy ) {
177
+
178
+ JdbcMappingContext context = new JdbcMappingContext ( //
179
+ namingStrategy , //
180
+ mock (NamedParameterJdbcOperations .class ), //
181
+ __ -> {
182
+ } //
183
+ );
134
184
135
- JdbcMappingContext context = new JdbcMappingContext (mock (NamedParameterJdbcOperations .class ));
136
185
DataAccessStrategy accessStrategy = mock (DataAccessStrategy .class );
137
186
138
187
// the ID of the entity is used to determine what kind of ResultSet is needed for subsequent selects.
@@ -240,7 +289,12 @@ private boolean isBeforeFirst() {
240
289
}
241
290
242
291
private Object getObject (String column ) {
243
- return values .get (index ).get (column );
292
+
293
+ Map <String , Object > rowMap = values .get (index );
294
+
295
+ Assert .isTrue (rowMap .containsKey (column ), String .format ("Trying to access a column (%s) that does not exist" , column ));
296
+
297
+ return rowMap .get (column );
244
298
}
245
299
246
300
private boolean next () {
@@ -251,6 +305,13 @@ private boolean next() {
251
305
}
252
306
253
307
@ RequiredArgsConstructor
308
+ static class TrivialImmutable {
309
+
310
+ @ Id
311
+ private final Long id ;
312
+ private final String name ;
313
+ }
314
+
254
315
static class Trivial {
255
316
256
317
@ Id
0 commit comments