Search in sources :

Example 26 with ValueHolder

use of org.apache.cayenne.ValueHolder in project cayenne by apache.

the class DataContextPrefetchIT method testPrefetchToMany_OnJoinTableDisjoinedPrefetch.

/**
 * Test that a to-many relationship is initialized when a target entity has
 * a compound PK only partially involved in relationship.
 */
@Test
public void testPrefetchToMany_OnJoinTableDisjoinedPrefetch() throws Exception {
    createTwoArtistsWithExhibitsDataSet();
    SelectQuery q = new SelectQuery(Artist.class);
    q.addPrefetch(Artist.ARTIST_EXHIBIT_ARRAY.disjoint());
    q.addOrdering(Artist.ARTIST_NAME.asc());
    final List<Artist> artists = context.performQuery(q);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            assertEquals(2, artists.size());
            Artist a1 = artists.get(0);
            assertEquals("artist2", a1.getArtistName());
            List<?> toMany = (List<?>) a1.readPropertyDirectly(Artist.ARTIST_EXHIBIT_ARRAY.getName());
            assertNotNull(toMany);
            assertFalse(((ValueHolder) toMany).isFault());
            assertEquals(2, toMany.size());
            ArtistExhibit artistExhibit = (ArtistExhibit) toMany.get(0);
            assertEquals(PersistenceState.COMMITTED, artistExhibit.getPersistenceState());
            assertSame(a1, artistExhibit.getToArtist());
            Artist a2 = artists.get(1);
            assertEquals("artist3", a2.getArtistName());
            List<?> toMany2 = (List<?>) a2.readPropertyDirectly(Artist.ARTIST_EXHIBIT_ARRAY.getName());
            assertNotNull(toMany2);
            assertFalse(((ValueHolder) toMany2).isFault());
            assertEquals(3, toMany2.size());
            ArtistExhibit artistExhibit2 = (ArtistExhibit) toMany2.get(0);
            assertEquals(PersistenceState.COMMITTED, artistExhibit2.getPersistenceState());
            assertSame(a2, artistExhibit2.getToArtist());
        }
    });
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Artist(org.apache.cayenne.testdo.testmap.Artist) ArtistExhibit(org.apache.cayenne.testdo.testmap.ArtistExhibit) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) List(java.util.List) ValueHolder(org.apache.cayenne.ValueHolder) Test(org.junit.Test)

Example 27 with ValueHolder

use of org.apache.cayenne.ValueHolder in project cayenne by apache.

the class DataContextPrefetchMultistepIT method testToManyToOne_EmptyToMany.

@Test
public void testToManyToOne_EmptyToMany() throws Exception {
    createGalleriesAndArtists();
    SelectQuery<Gallery> q = SelectQuery.query(Gallery.class, Gallery.GALLERY_NAME.eq("gallery2"));
    q.addPrefetch(Gallery.PAINTING_ARRAY.disjoint());
    q.addPrefetch(Gallery.PAINTING_ARRAY.dot(Painting.TO_ARTIST).disjoint());
    List<Gallery> galleries = context.select(q);
    assertEquals(1, galleries.size());
    Gallery g2 = galleries.get(0);
    // this relationship should be resolved
    assertTrue(g2.readPropertyDirectly(Gallery.PAINTING_ARRAY.getName()) instanceof ValueHolder);
    List<Painting> exhibits = (List<Painting>) g2.readPropertyDirectly(Gallery.PAINTING_ARRAY.getName());
    assertFalse(((ValueHolder) exhibits).isFault());
    assertEquals(0, exhibits.size());
}
Also used : Gallery(org.apache.cayenne.testdo.testmap.Gallery) List(java.util.List) ValueHolder(org.apache.cayenne.ValueHolder) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 28 with ValueHolder

use of org.apache.cayenne.ValueHolder in project cayenne by apache.

the class DataContextPrefetchMultistepIT method testToManyToOne_EmptyToMany_NoRootQualifier.

@Test
public void testToManyToOne_EmptyToMany_NoRootQualifier() throws Exception {
    createGalleriesAndArtists();
    SelectQuery<Gallery> q = SelectQuery.query(Gallery.class);
    q.addPrefetch(Gallery.PAINTING_ARRAY.disjoint());
    q.addPrefetch(Gallery.PAINTING_ARRAY.dot(Painting.TO_ARTIST).disjoint());
    List<Gallery> galleries = context.select(q);
    assertEquals(3, galleries.size());
    Gallery g = galleries.get(0);
    // this relationship should be resolved
    assertTrue(g.readPropertyDirectly(Gallery.PAINTING_ARRAY.getName()) instanceof ValueHolder);
    List<Painting> exhibits = (List<Painting>) g.readPropertyDirectly(Gallery.PAINTING_ARRAY.getName());
    assertFalse(((ValueHolder) exhibits).isFault());
    assertEquals(0, exhibits.size());
}
Also used : Gallery(org.apache.cayenne.testdo.testmap.Gallery) List(java.util.List) ValueHolder(org.apache.cayenne.ValueHolder) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 29 with ValueHolder

use of org.apache.cayenne.ValueHolder in project cayenne by apache.

the class DataContextPrefetchMultistepIT method testToManyToManyFirstStepResolved.

@Test
public void testToManyToManyFirstStepResolved() throws Exception {
    createTwoArtistsWithExhibitsDataSet();
    Expression e = ExpressionFactory.exp("galleryName = $name");
    SelectQuery<Gallery> q = SelectQuery.query(Gallery.class, e.params(Collections.singletonMap("name", "gallery2")));
    q.addPrefetch("exhibitArray");
    q.addPrefetch("exhibitArray.artistExhibitArray");
    List<Gallery> galleries = context.select(q);
    assertEquals(1, galleries.size());
    Gallery g2 = galleries.get(0);
    // this relationship should be resolved
    assertTrue(g2.readPropertyDirectly("exhibitArray") instanceof ValueHolder);
    List<Exhibit> exhibits = (List<Exhibit>) g2.readPropertyDirectly("exhibitArray");
    assertFalse(((ValueHolder) exhibits).isFault());
    assertEquals(1, exhibits.size());
    Exhibit e1 = exhibits.get(0);
    assertEquals(PersistenceState.COMMITTED, e1.getPersistenceState());
    // this to-many must also be resolved
    assertTrue(e1.readPropertyDirectly("artistExhibitArray") instanceof ValueHolder);
    List<ArtistExhibit> aexhibits = (List<ArtistExhibit>) e1.readPropertyDirectly("artistExhibitArray");
    assertFalse(((ValueHolder) aexhibits).isFault());
    assertEquals(1, exhibits.size());
    ArtistExhibit ae1 = aexhibits.get(0);
    assertEquals(PersistenceState.COMMITTED, ae1.getPersistenceState());
}
Also used : Expression(org.apache.cayenne.exp.Expression) Gallery(org.apache.cayenne.testdo.testmap.Gallery) Exhibit(org.apache.cayenne.testdo.testmap.Exhibit) ArtistExhibit(org.apache.cayenne.testdo.testmap.ArtistExhibit) ArtistExhibit(org.apache.cayenne.testdo.testmap.ArtistExhibit) List(java.util.List) ValueHolder(org.apache.cayenne.ValueHolder) Test(org.junit.Test)

Example 30 with ValueHolder

use of org.apache.cayenne.ValueHolder in project cayenne by apache.

the class DataContextDisjointByIdPrefetch_ExtrasIT method testFlattenedRelationship.

@Test
public void testFlattenedRelationship() throws Exception {
    createBagWithTwoBoxesAndPlentyOfBallsDataSet();
    SelectQuery query = new SelectQuery(Bag.class);
    query.addPrefetch(Bag.BALLS.disjointById());
    final List<Bag> result = context.performQuery(query);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            assertFalse(result.isEmpty());
            Bag b1 = result.get(0);
            List<Ball> balls = (List<Ball>) b1.readPropertyDirectly(Bag.BALLS.getName());
            assertNotNull(balls);
            assertFalse(((ValueHolder) balls).isFault());
            assertEquals(6, balls.size());
            List<Integer> volumes = new ArrayList<Integer>();
            for (Ball b : balls) {
                assertEquals(PersistenceState.COMMITTED, b.getPersistenceState());
                volumes.add(b.getThingVolume());
            }
            assertTrue(volumes.containsAll(Arrays.asList(10, 20, 30, 40, 20, 40)));
        }
    });
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Ball(org.apache.cayenne.testdo.things.Ball) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) Bag(org.apache.cayenne.testdo.things.Bag) ArrayList(java.util.ArrayList) List(java.util.List) ValueHolder(org.apache.cayenne.ValueHolder) Test(org.junit.Test)

Aggregations

ValueHolder (org.apache.cayenne.ValueHolder)35 Test (org.junit.Test)32 List (java.util.List)28 UnitTestClosure (org.apache.cayenne.unit.di.UnitTestClosure)23 SelectQuery (org.apache.cayenne.query.SelectQuery)21 Artist (org.apache.cayenne.testdo.testmap.Artist)18 Painting (org.apache.cayenne.testdo.testmap.Painting)12 ArrayList (java.util.ArrayList)10 ArtistExhibit (org.apache.cayenne.testdo.testmap.ArtistExhibit)7 Gallery (org.apache.cayenne.testdo.testmap.Gallery)7 HashSet (java.util.HashSet)4 Iterator (java.util.Iterator)4 Set (java.util.Set)4 Expression (org.apache.cayenne.exp.Expression)4 EJBQLQuery (org.apache.cayenne.query.EJBQLQuery)4 ArtGroup (org.apache.cayenne.testdo.testmap.ArtGroup)4 Exhibit (org.apache.cayenne.testdo.testmap.Exhibit)3 Bag (org.apache.cayenne.testdo.things.Bag)3 Ball (org.apache.cayenne.testdo.things.Ball)3 Box (org.apache.cayenne.testdo.things.Box)3