2020package org .apache .cayenne .access ;
2121
2222import org .apache .cayenne .Fault ;
23+ import org .apache .cayenne .ObjectContext ;
2324import org .apache .cayenne .ObjectId ;
2425import org .apache .cayenne .PersistenceState ;
2526import org .apache .cayenne .Persistent ;
2627import org .apache .cayenne .ValueHolder ;
2728import org .apache .cayenne .di .Inject ;
2829import org .apache .cayenne .query .ObjectSelect ;
30+ import org .apache .cayenne .runtime .CayenneRuntime ;
2931import org .apache .cayenne .test .jdbc .DBHelper ;
3032import org .apache .cayenne .test .jdbc .TableHelper ;
33+ import org .apache .cayenne .testdo .testmap .Artist ;
3134import org .apache .cayenne .testdo .testmap .ArtistExhibit ;
3235import org .apache .cayenne .testdo .testmap .Exhibit ;
3336import org .apache .cayenne .testdo .testmap .Gallery ;
@@ -51,6 +54,9 @@ public class DataContextPrefetchMultistepIT extends RuntimeCase {
5154 @ Inject
5255 protected DataContext context ;
5356
57+ @ Inject
58+ protected CayenneRuntime runtime ;
59+
5460 @ Inject
5561 protected DBHelper dbHelper ;
5662
@@ -286,4 +292,159 @@ public void testToManyToOne_EmptyToMany_NoRootQualifier() throws Exception {
286292 assertFalse (((ValueHolder ) exhibits ).isFault ());
287293 assertEquals (0 , exhibits .size ());
288294 }
295+
296+
297+ private Gallery createArtistWithPaintingInGallery () {
298+ Artist artist = context .newObject (Artist .class );
299+ artist .setArtistName ("Picasso" );
300+
301+ Painting painting = context .newObject (Painting .class );
302+ painting .setPaintingTitle ("Guernica" );
303+ artist .addToPaintingArray (painting );
304+
305+ Gallery gallery = context .newObject (Gallery .class );
306+ gallery .setGalleryName ("MOMA" );
307+ painting .setToGallery (gallery );
308+
309+ context .commitChanges ();
310+ return gallery ;
311+ }
312+
313+ @ Test
314+ public void testPrefetchAcross2RelationshipsKeepingBoth_JointAndJoint () {
315+ Gallery gallery = createArtistWithPaintingInGallery ();
316+
317+ assertNotNull (gallery .getPaintingArray ().get (0 ).getToArtist ());
318+
319+ // Prefetch the artist through the two relationships
320+ ObjectSelect .query (Gallery .class )
321+ .prefetch (Gallery .PAINTING_ARRAY .joint ())
322+ .prefetch (Gallery .PAINTING_ARRAY .dot (Painting .TO_ARTIST ).joint ())
323+ .select (context );
324+
325+ assertNotNull (gallery .getPaintingArray ().get (0 ).getToArtist ());
326+ }
327+
328+ @ Test
329+ public void testPrefetchAcross2RelationshipsKeepingBoth_JointAndDisjoint () {
330+ Gallery gallery = createArtistWithPaintingInGallery ();
331+
332+ assertNotNull (gallery .getPaintingArray ().get (0 ).getToArtist ());
333+
334+ ObjectContext objectContext = runtime .newContext ();
335+
336+ // Prefetch the artist through the two relationships
337+ Gallery gallery2 = ObjectSelect .query (Gallery .class )
338+ .prefetch (Gallery .PAINTING_ARRAY .joint ())
339+ .prefetch (Gallery .PAINTING_ARRAY .dot (Painting .TO_ARTIST ).disjoint ())
340+ .selectOne (objectContext );
341+
342+ assertNotNull (gallery2 .getPaintingArray ().get (0 ).getToArtist ());
343+ assertNotNull (gallery .getPaintingArray ().get (0 ).getToArtist ());
344+ }
345+
346+ @ Test
347+ public void testPrefetchAcross2RelationshipsKeepingBoth_JointAndDisjointById () {
348+ Gallery gallery = createArtistWithPaintingInGallery ();
349+
350+ assertNotNull (gallery .getPaintingArray ().get (0 ).getToArtist ());
351+
352+ // Prefetch the artist through the two relationships
353+ ObjectSelect .query (Gallery .class )
354+ .prefetch (Gallery .PAINTING_ARRAY .joint ())
355+ .prefetch (Gallery .PAINTING_ARRAY .dot (Painting .TO_ARTIST ).disjointById ())
356+ .select (context );
357+
358+ assertNotNull (gallery .getPaintingArray ().get (0 ).getToArtist ());
359+ }
360+
361+ @ Test
362+ public void testPrefetchAcross2RelationshipsKeepingBoth_DisjointAndJoint () {
363+ Gallery gallery = createArtistWithPaintingInGallery ();
364+
365+ assertNotNull (gallery .getPaintingArray ().get (0 ).getToArtist ());
366+
367+ // Prefetch the artist through the two relationships
368+ ObjectSelect .query (Gallery .class )
369+ .prefetch (Gallery .PAINTING_ARRAY .disjoint ())
370+ .prefetch (Gallery .PAINTING_ARRAY .dot (Painting .TO_ARTIST ).joint ())
371+ .select (context );
372+
373+ assertNotNull (gallery .getPaintingArray ().get (0 ).getToArtist ());
374+ }
375+
376+ @ Test
377+ public void testPrefetchAcross2RelationshipsKeepingBoth_DisjointAndDisjoint () {
378+ Gallery gallery = createArtistWithPaintingInGallery ();
379+
380+ assertNotNull (gallery .getPaintingArray ().get (0 ).getToArtist ());
381+
382+ // Prefetch the artist through the two relationships
383+ ObjectSelect .query (Gallery .class )
384+ .prefetch (Gallery .PAINTING_ARRAY .disjoint ())
385+ .prefetch (Gallery .PAINTING_ARRAY .dot (Painting .TO_ARTIST ).disjoint ())
386+ .select (context );
387+
388+ assertNotNull (gallery .getPaintingArray ().get (0 ).getToArtist ());
389+ }
390+
391+ @ Test
392+ public void testPrefetchAcross2RelationshipsKeepingBoth_DisjointAndDisjointById () {
393+ Gallery gallery = createArtistWithPaintingInGallery ();
394+
395+ assertNotNull (gallery .getPaintingArray ().get (0 ).getToArtist ());
396+
397+ // Prefetch the artist through the two relationships
398+ ObjectSelect .query (Gallery .class )
399+ .prefetch (Gallery .PAINTING_ARRAY .disjoint ())
400+ .prefetch (Gallery .PAINTING_ARRAY .dot (Painting .TO_ARTIST ).disjointById ())
401+ .select (context );
402+
403+ assertNotNull (gallery .getPaintingArray ().get (0 ).getToArtist ());
404+ }
405+
406+ @ Test
407+ public void testPrefetchAcross2RelationshipsKeepingBoth_DisjointByIdAndJoint () {
408+ Gallery gallery = createArtistWithPaintingInGallery ();
409+
410+ assertNotNull (gallery .getPaintingArray ().get (0 ).getToArtist ());
411+
412+ // Prefetch the artist through the two relationships
413+ ObjectSelect .query (Gallery .class )
414+ .prefetch (Gallery .PAINTING_ARRAY .disjointById ())
415+ .prefetch (Gallery .PAINTING_ARRAY .dot (Painting .TO_ARTIST ).joint ())
416+ .select (context );
417+
418+ assertNotNull (gallery .getPaintingArray ().get (0 ).getToArtist ());
419+ }
420+
421+ @ Test
422+ public void testPrefetchAcross2RelationshipsKeepingBoth_DisjointByIdAndDisjoint () {
423+ Gallery gallery = createArtistWithPaintingInGallery ();
424+
425+ assertNotNull (gallery .getPaintingArray ().get (0 ).getToArtist ());
426+
427+ // Prefetch the artist through the two relationships
428+ ObjectSelect .query (Gallery .class )
429+ .prefetch (Gallery .PAINTING_ARRAY .disjointById ())
430+ .prefetch (Gallery .PAINTING_ARRAY .dot (Painting .TO_ARTIST ).disjoint ())
431+ .select (context );
432+
433+ assertNotNull (gallery .getPaintingArray ().get (0 ).getToArtist ());
434+ }
435+
436+ @ Test
437+ public void testPrefetchAcross2RelationshipsKeepingBoth_DisjointByIdAndDisjointById () {
438+ Gallery gallery = createArtistWithPaintingInGallery ();
439+
440+ assertNotNull (gallery .getPaintingArray ().get (0 ).getToArtist ());
441+
442+ // Prefetch the artist through the two relationships
443+ ObjectSelect .query (Gallery .class )
444+ .prefetch (Gallery .PAINTING_ARRAY .disjointById ())
445+ .prefetch (Gallery .PAINTING_ARRAY .dot (Painting .TO_ARTIST ).disjointById ())
446+ .select (context );
447+
448+ assertNotNull (gallery .getPaintingArray ().get (0 ).getToArtist ());
449+ }
289450}
0 commit comments