22
22
import static org .springframework .data .jdbc .testing .TestConfiguration .*;
23
23
import static org .springframework .data .jdbc .testing .TestDatabaseFeatures .Feature .*;
24
24
25
+ import java .sql .ResultSet ;
26
+ import java .sql .SQLException ;
25
27
import java .time .LocalDateTime ;
26
28
import java .util .ArrayList ;
27
29
import java .util .Collections ;
37
39
import java .util .stream .IntStream ;
38
40
39
41
import org .assertj .core .api .SoftAssertions ;
42
+ import org .junit .jupiter .api .Disabled ;
40
43
import org .junit .jupiter .api .Test ;
41
44
import org .springframework .beans .factory .annotation .Autowired ;
42
45
import org .springframework .context .ApplicationEventPublisher ;
43
46
import org .springframework .context .annotation .Bean ;
44
47
import org .springframework .context .annotation .Configuration ;
45
48
import org .springframework .context .annotation .Import ;
46
49
import org .springframework .dao .IncorrectResultSizeDataAccessException ;
50
+ import org .springframework .dao .DataAccessException ;
47
51
import org .springframework .dao .IncorrectUpdateSemanticsDataAccessException ;
48
52
import org .springframework .dao .OptimisticLockingFailureException ;
49
53
import org .springframework .data .annotation .Id ;
70
74
import org .springframework .data .relational .core .query .Criteria ;
71
75
import org .springframework .data .relational .core .query .CriteriaDefinition ;
72
76
import org .springframework .data .relational .core .query .Query ;
77
+ import org .springframework .jdbc .core .ResultSetExtractor ;
73
78
import org .springframework .jdbc .core .namedparam .NamedParameterJdbcOperations ;
74
79
import org .springframework .test .context .ActiveProfiles ;
75
80
import org .springframework .test .context .ContextConfiguration ;
@@ -95,6 +100,7 @@ abstract class AbstractJdbcAggregateTemplateIntegrationTests {
95
100
@ Autowired JdbcAggregateOperations template ;
96
101
@ Autowired NamedParameterJdbcOperations jdbcTemplate ;
97
102
@ Autowired RelationalMappingContext mappingContext ;
103
+ @ Autowired NamedParameterJdbcOperations jdbc ;
98
104
99
105
LegoSet legoSet = createLegoSet ("Star Destroyer" );
100
106
@@ -1202,6 +1208,115 @@ void readEnumArray() {
1202
1208
assertThat (template .findById (entity .id , EnumArrayOwner .class ).digits ).isEqualTo (new Color [] { Color .BLUE });
1203
1209
}
1204
1210
1211
+ @ Test // GH-1448
1212
+ void multipleCollections () {
1213
+
1214
+ MultipleCollections aggregate = new MultipleCollections ();
1215
+ aggregate .name = "aggregate" ;
1216
+
1217
+ aggregate .listElements .add (new ListElement ("one" ));
1218
+ aggregate .listElements .add (new ListElement ("two" ));
1219
+ aggregate .listElements .add (new ListElement ("three" ));
1220
+
1221
+ aggregate .setElements .add (new SetElement ("one" ));
1222
+ aggregate .setElements .add (new SetElement ("two" ));
1223
+
1224
+ aggregate .mapElements .put ("alpha" , new MapElement ("one" ));
1225
+ aggregate .mapElements .put ("beta" , new MapElement ("two" ));
1226
+ aggregate .mapElements .put ("gamma" , new MapElement ("three" ));
1227
+ aggregate .mapElements .put ("delta" , new MapElement ("four" ));
1228
+
1229
+ template .save (aggregate );
1230
+
1231
+ MultipleCollections reloaded = template .findById (aggregate .id , MultipleCollections .class );
1232
+
1233
+ assertSoftly (softly -> {
1234
+
1235
+ softly .assertThat (reloaded .name ).isEqualTo (aggregate .name );
1236
+
1237
+ softly .assertThat (reloaded .listElements ).containsExactly (aggregate .listElements .get (0 ),
1238
+ aggregate .listElements .get (1 ), aggregate .listElements .get (2 ));
1239
+
1240
+ softly .assertThat (reloaded .setElements )
1241
+ .containsExactlyInAnyOrder (aggregate .setElements .toArray (new SetElement [0 ]));
1242
+
1243
+ softly .assertThat (reloaded .mapElements .get ("alpha" )).isEqualTo (new MapElement ("one" ));
1244
+ softly .assertThat (reloaded .mapElements .get ("beta" )).isEqualTo (new MapElement ("two" ));
1245
+ softly .assertThat (reloaded .mapElements .get ("gamma" )).isEqualTo (new MapElement ("three" ));
1246
+ softly .assertThat (reloaded .mapElements .get ("delta" )).isEqualTo (new MapElement ("four" ));
1247
+ });
1248
+ }
1249
+
1250
+ @ Test // GH-1448
1251
+ void multipleCollectionsWithEmptySet () {
1252
+
1253
+ MultipleCollections aggregate = new MultipleCollections ();
1254
+ aggregate .name = "aggregate" ;
1255
+
1256
+ aggregate .listElements .add (new ListElement ("one" ));
1257
+ aggregate .listElements .add (new ListElement ("two" ));
1258
+ aggregate .listElements .add (new ListElement ("three" ));
1259
+
1260
+ aggregate .mapElements .put ("alpha" , new MapElement ("one" ));
1261
+ aggregate .mapElements .put ("beta" , new MapElement ("two" ));
1262
+ aggregate .mapElements .put ("gamma" , new MapElement ("three" ));
1263
+ aggregate .mapElements .put ("delta" , new MapElement ("four" ));
1264
+
1265
+ template .save (aggregate );
1266
+
1267
+ MultipleCollections reloaded = template .findById (aggregate .id , MultipleCollections .class );
1268
+
1269
+ assertSoftly (softly -> {
1270
+
1271
+ softly .assertThat (reloaded .name ).isEqualTo (aggregate .name );
1272
+
1273
+ softly .assertThat (reloaded .listElements ).containsExactly (aggregate .listElements .get (0 ),
1274
+ aggregate .listElements .get (1 ), aggregate .listElements .get (2 ));
1275
+
1276
+ softly .assertThat (reloaded .setElements )
1277
+ .containsExactlyInAnyOrder (aggregate .setElements .toArray (new SetElement [0 ]));
1278
+
1279
+ softly .assertThat (reloaded .mapElements .get ("alpha" )).isEqualTo (new MapElement ("one" ));
1280
+ softly .assertThat (reloaded .mapElements .get ("beta" )).isEqualTo (new MapElement ("two" ));
1281
+ softly .assertThat (reloaded .mapElements .get ("gamma" )).isEqualTo (new MapElement ("three" ));
1282
+ softly .assertThat (reloaded .mapElements .get ("delta" )).isEqualTo (new MapElement ("four" ));
1283
+ });
1284
+ }
1285
+
1286
+ @ Test // GH-1448
1287
+ void multipleCollectionsWithEmptyList () {
1288
+
1289
+ MultipleCollections aggregate = new MultipleCollections ();
1290
+ aggregate .name = "aggregate" ;
1291
+
1292
+ aggregate .setElements .add (new SetElement ("one" ));
1293
+ aggregate .setElements .add (new SetElement ("two" ));
1294
+
1295
+ aggregate .mapElements .put ("alpha" , new MapElement ("one" ));
1296
+ aggregate .mapElements .put ("beta" , new MapElement ("two" ));
1297
+ aggregate .mapElements .put ("gamma" , new MapElement ("three" ));
1298
+ aggregate .mapElements .put ("delta" , new MapElement ("four" ));
1299
+
1300
+ template .save (aggregate );
1301
+
1302
+ MultipleCollections reloaded = template .findById (aggregate .id , MultipleCollections .class );
1303
+
1304
+ assertSoftly (softly -> {
1305
+
1306
+ softly .assertThat (reloaded .name ).isEqualTo (aggregate .name );
1307
+
1308
+ softly .assertThat (reloaded .listElements ).containsExactly ();
1309
+
1310
+ softly .assertThat (reloaded .setElements )
1311
+ .containsExactlyInAnyOrder (aggregate .setElements .toArray (new SetElement [0 ]));
1312
+
1313
+ softly .assertThat (reloaded .mapElements .get ("alpha" )).isEqualTo (new MapElement ("one" ));
1314
+ softly .assertThat (reloaded .mapElements .get ("beta" )).isEqualTo (new MapElement ("two" ));
1315
+ softly .assertThat (reloaded .mapElements .get ("gamma" )).isEqualTo (new MapElement ("three" ));
1316
+ softly .assertThat (reloaded .mapElements .get ("delta" )).isEqualTo (new MapElement ("four" ));
1317
+ });
1318
+ }
1319
+
1205
1320
private <T extends Number > void saveAndUpdateAggregateWithVersion (VersionedAggregate aggregate ,
1206
1321
Function <Number , T > toConcreteNumber ) {
1207
1322
saveAndUpdateAggregateWithVersion (aggregate , toConcreteNumber , 0 );
@@ -1932,6 +2047,24 @@ static class WithInsertOnly {
1932
2047
@ InsertOnlyProperty String insertOnly ;
1933
2048
}
1934
2049
2050
+ @ Table
2051
+ static class MultipleCollections {
2052
+ @ Id Long id ;
2053
+ String name ;
2054
+ List <ListElement > listElements = new ArrayList <>();
2055
+ Set <SetElement > setElements = new HashSet <>();
2056
+ Map <String , MapElement > mapElements = new HashMap <>();
2057
+ }
2058
+
2059
+ record ListElement (String name ) {
2060
+ }
2061
+
2062
+ record SetElement (String name ) {
2063
+ }
2064
+
2065
+ record MapElement (String name ) {
2066
+ }
2067
+
1935
2068
@ Configuration
1936
2069
@ Import (TestConfiguration .class )
1937
2070
static class Config {
0 commit comments