Search in sources :

Example 1 with ValueHolder

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

the class ClientServerChannelIT method testOnQueryPrefetchingToManyEmpty.

@Test
public void testOnQueryPrefetchingToManyEmpty() throws Exception {
    createTwoMtTable1sAnd2sDataSet();
    SelectQuery q = new SelectQuery(ClientMtTable1.class);
    q.addOrdering(ClientMtTable1.GLOBAL_ATTRIBUTE1_PROPERTY, SortOrder.ASCENDING);
    q.addPrefetch(ClientMtTable1.TABLE2ARRAY_PROPERTY);
    final List<?> results = clientServerChannel.onQuery(null, q).firstList();
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            ClientMtTable1 o2 = (ClientMtTable1) results.get(1);
            assertNull(o2.getObjectContext());
            List<?> children2 = o2.getTable2Array();
            assertNotNull(children2);
            assertFalse(((ValueHolder) children2).isFault());
            assertEquals(0, children2.size());
        }
    });
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) List(java.util.List) ValueHolder(org.apache.cayenne.ValueHolder) Test(org.junit.Test)

Example 2 with ValueHolder

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

the class ClientServerChannelQueryIT method testSelectQueryPrefetchToOne.

@Test
public void testSelectQueryPrefetchToOne() throws Exception {
    createTwoMtTable1sAnd2sDataSet();
    SelectQuery q = new SelectQuery(ClientMtTable2.class, ExpressionFactory.exp("globalAttribute = 'g1'"));
    q.addPrefetch(ClientMtTable2.TABLE1_PROPERTY);
    List<?> results = context.performQuery(q);
    assertEquals(1, results.size());
    ClientMtTable2 result = (ClientMtTable2) results.get(0);
    ValueHolder holder = result.getTable1Direct();
    assertNotNull(holder);
    assertTrue(holder instanceof PersistentObjectHolder);
    PersistentObjectHolder objectHolder = (PersistentObjectHolder) holder;
    assertFalse(objectHolder.isFault());
    ClientMtTable1 target = (ClientMtTable1) objectHolder.getValue();
    assertNotNull(target);
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) PersistentObjectHolder(org.apache.cayenne.util.PersistentObjectHolder) ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ClientMtTable2(org.apache.cayenne.testdo.mt.ClientMtTable2) ValueHolder(org.apache.cayenne.ValueHolder) Test(org.junit.Test)

Example 3 with ValueHolder

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

the class FlattenedPrefetchIT method testJointManyToMany.

@Test
public void testJointManyToMany() throws Exception {
    createPrefetchDataSet1();
    SelectQuery q = new SelectQuery(Artist.class);
    q.addPrefetch(Artist.GROUP_ARRAY.joint());
    final List<Artist> objects = context.performQuery(q);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            assertEquals(3, objects.size());
            for (Artist a : objects) {
                List<ArtGroup> list = a.getGroupArray();
                assertNotNull(list);
                assertFalse("artist's groups not resolved: " + a, ((ValueHolder) list).isFault());
                assertTrue(list.size() > 0);
                for (ArtGroup g : list) {
                    assertEquals(PersistenceState.COMMITTED, g.getPersistenceState());
                }
                // assert no duplicates
                Set<ArtGroup> s = new HashSet<ArtGroup>(list);
                assertEquals(s.size(), list.size());
            }
        }
    });
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Artist(org.apache.cayenne.testdo.testmap.Artist) Set(java.util.Set) HashSet(java.util.HashSet) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) List(java.util.List) ValueHolder(org.apache.cayenne.ValueHolder) ArtGroup(org.apache.cayenne.testdo.testmap.ArtGroup) Test(org.junit.Test)

Example 4 with ValueHolder

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

the class FlattenedPrefetchIT method testJointMultiPrefetch.

@Test
public void testJointMultiPrefetch() throws Exception {
    createPrefetchDataSet2();
    SelectQuery q = new SelectQuery(Painting.class);
    q.addPrefetch(Painting.TO_ARTIST.joint());
    q.addPrefetch(Painting.TO_ARTIST.dot(Artist.GROUP_ARRAY).joint());
    final List<Painting> objects = context.performQuery(q);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            assertEquals(3, objects.size());
            for (Painting p : objects) {
                Artist a = p.getToArtist();
                assertEquals(PersistenceState.COMMITTED, a.getPersistenceState());
                List<ArtGroup> list = a.getGroupArray();
                assertNotNull(list);
                assertFalse("artist's groups not resolved: " + a, ((ValueHolder) list).isFault());
                assertTrue(list.size() > 0);
                for (ArtGroup g : list) {
                    assertEquals(PersistenceState.COMMITTED, g.getPersistenceState());
                }
                // assert no duplicates
                Set<ArtGroup> s = new HashSet<ArtGroup>(list);
                assertEquals(s.size(), list.size());
            }
        }
    });
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Artist(org.apache.cayenne.testdo.testmap.Artist) Set(java.util.Set) HashSet(java.util.HashSet) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) List(java.util.List) ValueHolder(org.apache.cayenne.ValueHolder) ArtGroup(org.apache.cayenne.testdo.testmap.ArtGroup) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 5 with ValueHolder

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

the class JointPrefetchIT method testJointPrefetchMultiStep.

@Test
public void testJointPrefetchMultiStep() throws Exception {
    createJointPrefetchDataSet();
    // query with to-many joint prefetches
    SelectQuery<Artist> q = new SelectQuery<>(Artist.class);
    q.addPrefetch(Artist.PAINTING_ARRAY.dot(Painting.TO_GALLERY).joint());
    final DataContext context = this.context;
    // make sure phantomly prefetched objects are not deallocated
    context.getObjectStore().objectMap = new HashMap<>();
    // sanity check...
    DataObject g1 = (DataObject) context.getGraphManager().getNode(new ObjectId("Gallery", Gallery.GALLERY_ID_PK_COLUMN, 33001));
    assertNull(g1);
    final List<Artist> objects = q.select(context);
    queryInterceptor.runWithQueriesBlocked(() -> {
        assertEquals(3, objects.size());
        for (Artist a : objects) {
            ValueHolder list = (ValueHolder) a.getPaintingArray();
            assertNotNull(list);
            // intermediate relationship is not fetched...
            assertTrue(list.isFault());
        }
        // however both galleries must be in memory...
        DataObject g11 = (DataObject) context.getGraphManager().getNode(new ObjectId("Gallery", Gallery.GALLERY_ID_PK_COLUMN, 33001));
        assertNotNull(g11);
        assertEquals(PersistenceState.COMMITTED, g11.getPersistenceState());
        DataObject g2 = (DataObject) context.getGraphManager().getNode(new ObjectId("Gallery", Gallery.GALLERY_ID_PK_COLUMN, 33002));
        assertNotNull(g2);
        assertEquals(PersistenceState.COMMITTED, g2.getPersistenceState());
    });
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) SelectQuery(org.apache.cayenne.query.SelectQuery) DataObject(org.apache.cayenne.DataObject) ObjectId(org.apache.cayenne.ObjectId) 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