2929import java .util .stream .IntStream ;
3030import java .util .stream .Stream ;
3131
32+ import org .junit .jupiter .api .Disabled ;
3233import org .junit .jupiter .api .Test ;
3334import org .springframework .beans .factory .annotation .Autowired ;
34- import org .springframework .context .ApplicationEventPublisher ;
35+ import org .springframework .context .ApplicationContext ;
3536import org .springframework .context .annotation .Bean ;
3637import org .springframework .context .annotation .Configuration ;
3738import org .springframework .context .annotation .Import ;
6061import org .springframework .data .relational .core .mapping .MappedCollection ;
6162import org .springframework .data .relational .core .mapping .RelationalMappingContext ;
6263import org .springframework .data .relational .core .mapping .Table ;
64+ import org .springframework .data .relational .core .mapping .event .BeforeConvertCallback ;
6365import org .springframework .data .relational .core .query .Criteria ;
6466import org .springframework .data .relational .core .query .CriteriaDefinition ;
6567import org .springframework .data .relational .core .query .Query ;
@@ -236,7 +238,25 @@ void findAllByQuery() {
236238 Query query = Query .query (criteria );
237239 Iterable <SimpleListParent > reloadedById = template .findAll (query , SimpleListParent .class );
238240
239- assertThat (reloadedById ).extracting (e -> e .id , e -> e .content .size ()).containsExactly (tuple (two .id , 2 ));
241+ assertThat (reloadedById ) //
242+ .extracting (e -> e .id , e -> e .name , e -> e .content .size ()) //
243+ .containsExactly (tuple (two .id , two .name , 2 ));
244+ }
245+
246+ @ Test // GH-1803
247+ void findAllByQueryWithColumns () {
248+
249+ template .save (SimpleListParent .of ("one" , "one_1" ));
250+ SimpleListParent two = template .save (SimpleListParent .of ("two" , "two_1" , "two_2" ));
251+ template .save (SimpleListParent .of ("three" , "three_1" , "three_2" , "three_3" ));
252+
253+ CriteriaDefinition criteria = CriteriaDefinition .from (Criteria .where ("id" ).is (two .id ));
254+ Query query = Query .query (criteria ).columns ("id" );
255+ Iterable <SimpleListParent > reloadedById = template .findAll (query , SimpleListParent .class );
256+
257+ assertThat (reloadedById ) //
258+ .extracting (e -> e .id , e -> e .name , e -> e .content .size ()) //
259+ .containsExactly (tuple (two .id , null , 2 ));
240260 }
241261
242262 @ Test // GH-1601
@@ -1373,6 +1393,22 @@ void mapWithEnumKey() {
13731393 assertThat (enumMapOwners ).containsExactly (enumMapOwner );
13741394 }
13751395
1396+ @ Test // GH-2064
1397+ void saveAllBeforeConvertCallback () {
1398+
1399+ BeforeConvertCallbackForSaveBatch first = new BeforeConvertCallbackForSaveBatch ("first" );
1400+ BeforeConvertCallbackForSaveBatch second = new BeforeConvertCallbackForSaveBatch ("second" );
1401+ BeforeConvertCallbackForSaveBatch third = new BeforeConvertCallbackForSaveBatch ("third" );
1402+
1403+ template .saveAll (List .of (first , second , third ));
1404+
1405+ List <BeforeConvertCallbackForSaveBatch > allEntriesInTable = template
1406+ .findAll (BeforeConvertCallbackForSaveBatch .class );
1407+
1408+ assertThat (allEntriesInTable ).hasSize (3 ).extracting (BeforeConvertCallbackForSaveBatch ::getName )
1409+ .containsExactlyInAnyOrder ("first" , "second" , "third" );
1410+ }
1411+
13761412 @ Test // GH-1684
13771413 void oneToOneWithIdenticalIdColumnName () {
13781414
@@ -2184,6 +2220,31 @@ public Short getVersion() {
21842220 }
21852221 }
21862222
2223+ @ Table ("BEFORE_CONVERT_CALLBACK_FOR_SAVE_BATCH" )
2224+ static class BeforeConvertCallbackForSaveBatch {
2225+
2226+ @ Id
2227+ private String id ;
2228+
2229+ private String name ;
2230+
2231+ public BeforeConvertCallbackForSaveBatch (String name ) {
2232+ this .name = name ;
2233+ }
2234+
2235+ public String getId () {
2236+ return id ;
2237+ }
2238+
2239+ public void setId (String id ) {
2240+ this .id = id ;
2241+ }
2242+
2243+ public String getName () {
2244+ return name ;
2245+ }
2246+ }
2247+
21872248 @ Table ("VERSIONED_AGGREGATE" )
21882249 static class AggregateWithPrimitiveShortVersion extends VersionedAggregate {
21892250
@@ -2214,12 +2275,14 @@ static class WithIdOnly {
22142275
22152276 @ Table
22162277 static class WithInsertOnly {
2278+
22172279 @ Id Long id ;
22182280 @ InsertOnlyProperty String insertOnly ;
22192281 }
22202282
22212283 @ Table
22222284 static class MultipleCollections {
2285+
22232286 @ Id Long id ;
22242287 String name ;
22252288 List <ListElement > listElements = new ArrayList <>();
@@ -2271,9 +2334,17 @@ TestClass testClass() {
22712334 }
22722335
22732336 @ Bean
2274- JdbcAggregateOperations operations (ApplicationEventPublisher publisher , RelationalMappingContext context ,
2337+ BeforeConvertCallback <BeforeConvertCallbackForSaveBatch > callback () {
2338+ return aggregate -> {
2339+ aggregate .setId (UUID .randomUUID ().toString ());
2340+ return aggregate ;
2341+ };
2342+ }
2343+
2344+ @ Bean
2345+ JdbcAggregateOperations operations (ApplicationContext applicationContext , RelationalMappingContext context ,
22752346 DataAccessStrategy dataAccessStrategy , JdbcConverter converter ) {
2276- return new JdbcAggregateTemplate (publisher , context , converter , dataAccessStrategy );
2347+ return new JdbcAggregateTemplate (applicationContext , context , converter , dataAccessStrategy );
22772348 }
22782349 }
22792350
@@ -2285,5 +2356,10 @@ static class JdbcAggregateTemplateIntegrationTests extends AbstractJdbcAggregate
22852356 static class JdbcAggregateTemplateSingleQueryLoadingIntegrationTests
22862357 extends AbstractJdbcAggregateTemplateIntegrationTests {
22872358
2359+ @ Disabled
2360+ @ Override
2361+ void findAllByQueryWithColumns () {
2362+ super .findAllByQueryWithColumns ();
2363+ }
22882364 }
22892365}
0 commit comments