Search in sources :

Example 21 with ValueHolder

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

the class DataContextPrefetchIT method testPrefetchToMany_WithQualfier.

@Test
public void testPrefetchToMany_WithQualfier() throws Exception {
    createTwoArtistsAndTwoPaintingsDataSet();
    Map<String, Object> params = new HashMap<>();
    params.put("name1", "artist2");
    params.put("name2", "artist3");
    Expression e = ExpressionFactory.exp("artistName = $name1 or artistName = $name2");
    SelectQuery<Artist> q = new SelectQuery<>("Artist", e.params(params));
    q.addPrefetch(Artist.PAINTING_ARRAY.disjoint());
    final List<Artist> artists = context.select(q);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            assertEquals(2, artists.size());
            Artist a1 = artists.get(0);
            List<?> toMany = (List<?>) a1.readPropertyDirectly(Artist.PAINTING_ARRAY.getName());
            assertNotNull(toMany);
            assertFalse(((ValueHolder) toMany).isFault());
            assertEquals(1, toMany.size());
            Painting p1 = (Painting) toMany.get(0);
            assertEquals("p_" + a1.getArtistName(), p1.getPaintingTitle());
            Artist a2 = artists.get(1);
            List<?> toMany2 = (List<?>) a2.readPropertyDirectly(Artist.PAINTING_ARRAY.getName());
            assertNotNull(toMany2);
            assertFalse(((ValueHolder) toMany2).isFault());
            assertEquals(1, toMany2.size());
            Painting p2 = (Painting) toMany2.get(0);
            assertEquals("p_" + a2.getArtistName(), p2.getPaintingTitle());
        }
    });
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) HashMap(java.util.HashMap) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) ValueHolder(org.apache.cayenne.ValueHolder) Painting(org.apache.cayenne.testdo.testmap.Painting) SelectQuery(org.apache.cayenne.query.SelectQuery) Expression(org.apache.cayenne.exp.Expression) List(java.util.List) Test(org.junit.Test)

Example 22 with ValueHolder

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

the class DataContextPrefetchIT method testPrefetchToMany_ViaProperty.

@Test
public void testPrefetchToMany_ViaProperty() throws Exception {
    createTwoArtistsAndTwoPaintingsDataSet();
    SelectQuery<Artist> q = new SelectQuery<Artist>(Artist.class);
    q.addPrefetch(Artist.PAINTING_ARRAY.disjoint());
    final List<Artist> artists = context.select(q);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            assertEquals(2, artists.size());
            for (int i = 0; i < 2; i++) {
                Artist a = artists.get(i);
                List<?> toMany = (List<?>) a.readPropertyDirectly("paintingArray");
                assertNotNull(toMany);
                assertFalse(((ValueHolder) toMany).isFault());
                assertEquals(1, toMany.size());
                Painting p = (Painting) toMany.get(0);
                assertEquals("Invalid prefetched painting:" + p, "p_" + a.getArtistName(), p.getPaintingTitle());
            }
        }
    });
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) SelectQuery(org.apache.cayenne.query.SelectQuery) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) List(java.util.List) ValueHolder(org.apache.cayenne.ValueHolder) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 23 with ValueHolder

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

the class DataContextPrefetchIT method testPrefetchToManyOnJoinTableJoinedPrefetch.

/**
 * Test that a to-many relationship is initialized when a target entity has
 * a compound PK only partially involved in relationship.
 */
@Test
public void testPrefetchToManyOnJoinTableJoinedPrefetch() throws Exception {
    createTwoArtistsWithExhibitsDataSet();
    SelectQuery q = new SelectQuery(Artist.class);
    q.addPrefetch(Artist.ARTIST_EXHIBIT_ARRAY.joint());
    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("artistExhibitArray");
            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 24 with ValueHolder

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

the class DataContextPrefetchIT method testPrefetch_ToManyNoReverse.

/**
 * Test that a to-many relationship is initialized when there is no inverse
 * relationship
 */
@Test
public void testPrefetch_ToManyNoReverse() throws Exception {
    createTwoArtistsAndTwoPaintingsDataSet();
    ObjEntity paintingEntity = context.getEntityResolver().getObjEntity(Painting.class);
    ObjRelationship relationship = paintingEntity.getRelationship("toArtist");
    paintingEntity.removeRelationship("toArtist");
    try {
        SelectQuery<Artist> q = new SelectQuery<>(Artist.class);
        q.addPrefetch(Artist.PAINTING_ARRAY.disjoint());
        final List<Artist> result = context.performQuery(q);
        queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

            public void execute() {
                assertFalse(result.isEmpty());
                Artist a1 = result.get(0);
                List<?> toMany = (List<?>) a1.readPropertyDirectly("paintingArray");
                assertNotNull(toMany);
                assertFalse(((ValueHolder) toMany).isFault());
            }
        });
    } finally {
        paintingEntity.addRelationship(relationship);
    }
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) SelectQuery(org.apache.cayenne.query.SelectQuery) ObjEntity(org.apache.cayenne.map.ObjEntity) ObjRelationship(org.apache.cayenne.map.ObjRelationship) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) List(java.util.List) ValueHolder(org.apache.cayenne.ValueHolder) Test(org.junit.Test)

Example 25 with ValueHolder

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

the class DataContextPrefetchIT method testPrefetchToManyNoQualifier.

@Test
public void testPrefetchToManyNoQualifier() throws Exception {
    createTwoArtistsAndTwoPaintingsDataSet();
    SelectQuery q = new SelectQuery(Artist.class);
    q.addPrefetch(Artist.PAINTING_ARRAY.disjoint());
    final List<Artist> artists = context.performQuery(q);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            assertEquals(2, artists.size());
            for (int i = 0; i < 2; i++) {
                Artist a = artists.get(i);
                List<?> toMany = (List<?>) a.readPropertyDirectly("paintingArray");
                assertNotNull(toMany);
                assertFalse(((ValueHolder) toMany).isFault());
                assertEquals(1, toMany.size());
                Painting p = (Painting) toMany.get(0);
                assertEquals("Invalid prefetched painting:" + p, "p_" + a.getArtistName(), p.getPaintingTitle());
            }
        }
    });
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Artist(org.apache.cayenne.testdo.testmap.Artist) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) List(java.util.List) ValueHolder(org.apache.cayenne.ValueHolder) Painting(org.apache.cayenne.testdo.testmap.Painting) 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