Search in sources :

Example 6 with SelectQuery

use of org.apache.cayenne.query.SelectQuery in project cayenne by apache.

the class CayenneContextWithDataContextIT method testPrefetchingToMany.

@Test
public void testPrefetchingToMany() throws Exception {
    createTwoMtTable1sAnd2sDataSet();
    SelectQuery q = new SelectQuery(ClientMtTable1.class);
    q.addOrdering(ClientMtTable1.GLOBAL_ATTRIBUTE1_PROPERTY, SortOrder.ASCENDING);
    q.addPrefetch(ClientMtTable1.TABLE2ARRAY_PROPERTY);
    final List<ClientMtTable1> results = clientContext.performQuery(q);
    clientServerInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            ClientMtTable1 o1 = results.get(0);
            assertEquals(PersistenceState.COMMITTED, o1.getPersistenceState());
            assertSame(clientContext, o1.getObjectContext());
            List<?> children1 = o1.getTable2Array();
            assertEquals(2, children1.size());
            Iterator<?> it = children1.iterator();
            while (it.hasNext()) {
                ClientMtTable2 o = (ClientMtTable2) it.next();
                assertEquals(PersistenceState.COMMITTED, o.getPersistenceState());
                assertSame(clientContext, o.getObjectContext());
            // TODO: fixme... reverse relationship is not connected and will
            // cause a fetch
            // assertEquals(o1, o.getTable1());
            }
        }
    });
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ClientMtTable2(org.apache.cayenne.testdo.mt.ClientMtTable2) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) Iterator(java.util.Iterator) RemoteIncrementalFaultList(org.apache.cayenne.remote.RemoteIncrementalFaultList) List(java.util.List) Test(org.junit.Test)

Example 7 with SelectQuery

use of org.apache.cayenne.query.SelectQuery in project cayenne by apache.

the class CayenneContextWithDataContextIT method testPrefetchingToOneNull.

@Test
public void testPrefetchingToOneNull() throws Exception {
    tMtTable2.insert(15, null, "g3");
    SelectQuery q = new SelectQuery(ClientMtTable2.class);
    q.addPrefetch(ClientMtTable2.TABLE1_PROPERTY);
    final List<ClientMtTable2> results = clientContext.performQuery(q);
    clientServerInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            assertEquals(1, results.size());
            ClientMtTable2 o = results.get(0);
            assertEquals(PersistenceState.COMMITTED, o.getPersistenceState());
            assertSame(clientContext, o.getObjectContext());
            assertNull(o.getTable1());
        }
    });
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) ClientMtTable2(org.apache.cayenne.testdo.mt.ClientMtTable2) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) Test(org.junit.Test)

Example 8 with SelectQuery

use of org.apache.cayenne.query.SelectQuery in project cayenne by apache.

the class CayenneContextWithDataContextIT method testPrefetchingToOne.

@Test
public void testPrefetchingToOne() throws Exception {
    createTwoMtTable1sAnd2sDataSet();
    final ObjectId prefetchedId = new ObjectId("MtTable1", MtTable1.TABLE1_ID_PK_COLUMN, 1);
    SelectQuery q = new SelectQuery(ClientMtTable2.class);
    q.addOrdering(ClientMtTable2.GLOBAL_ATTRIBUTE_PROPERTY, SortOrder.ASCENDING);
    q.addPrefetch(ClientMtTable2.TABLE1_PROPERTY);
    final List<ClientMtTable2> results = clientContext.performQuery(q);
    clientServerInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            assertEquals(2, results.size());
            for (ClientMtTable2 o : results) {
                assertEquals(PersistenceState.COMMITTED, o.getPersistenceState());
                assertSame(clientContext, o.getObjectContext());
                ClientMtTable1 o1 = o.getTable1();
                assertNotNull(o1);
                assertEquals(PersistenceState.COMMITTED, o1.getPersistenceState());
                assertSame(clientContext, o1.getObjectContext());
                assertEquals(prefetchedId, o1.getObjectId());
            }
        }
    });
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ClientMtTable2(org.apache.cayenne.testdo.mt.ClientMtTable2) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) Test(org.junit.Test)

Example 9 with SelectQuery

use of org.apache.cayenne.query.SelectQuery in project cayenne by apache.

the class NestedCayenneContextIT method testPrefetchingToOne.

@Test
public void testPrefetchingToOne() throws Exception {
    final ClientMtTable1 mt11 = clientContext.newObject(ClientMtTable1.class);
    clientContext.newObject(ClientMtTable1.class);
    ClientMtTable2 mt21 = clientContext.newObject(ClientMtTable2.class);
    ClientMtTable2 mt22 = clientContext.newObject(ClientMtTable2.class);
    mt21.setTable1(mt11);
    mt22.setTable1(mt11);
    clientContext.commitChanges();
    final ObjectContext child = runtime.newContext(clientContext);
    SelectQuery<ClientMtTable2> q = new SelectQuery<ClientMtTable2>(ClientMtTable2.class);
    q.addPrefetch(ClientMtTable2.TABLE1_PROPERTY);
    final List<ClientMtTable2> results = child.select(q);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            assertEquals(2, results.size());
            Iterator<?> it = results.iterator();
            while (it.hasNext()) {
                ClientMtTable2 o = (ClientMtTable2) it.next();
                assertEquals(PersistenceState.COMMITTED, o.getPersistenceState());
                assertSame(child, o.getObjectContext());
                ClientMtTable1 o1 = o.getTable1();
                assertNotNull(o1);
                assertEquals(PersistenceState.COMMITTED, o1.getPersistenceState());
                assertSame(child, o1.getObjectContext());
                assertEquals(mt11.getObjectId(), o1.getObjectId());
            }
        }
    });
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ClientMtTable2(org.apache.cayenne.testdo.mt.ClientMtTable2) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) Iterator(java.util.Iterator) Test(org.junit.Test)

Example 10 with SelectQuery

use of org.apache.cayenne.query.SelectQuery in project cayenne by apache.

the class NestedCayenneContextIT method testCommitChangesToParent.

@Test
public void testCommitChangesToParent() throws Exception {
    clientContext.newObject(ClientMtTable1.class);
    clientContext.newObject(ClientMtTable1.class);
    clientContext.newObject(ClientMtTable1.class);
    clientContext.newObject(ClientMtTable1.class);
    clientContext.commitChanges();
    final ObjectContext child = runtime.newContext(clientContext);
    SelectQuery<ClientMtTable1> query = new SelectQuery<ClientMtTable1>(ClientMtTable1.class);
    List<ClientMtTable1> objects = child.select(query);
    assertEquals(4, objects.size());
    final ClientMtTable1 childNew = child.newObject(ClientMtTable1.class);
    childNew.setGlobalAttribute1("NNN");
    final ClientMtTable1 childModified = objects.get(0);
    childModified.setGlobalAttribute1("MMM");
    final ClientMtTable1 childCommitted = objects.get(1);
    final ClientMtTable1 childHollow = objects.get(3);
    child.invalidateObjects(childHollow);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            child.commitChangesToParent();
            // * all modified child objects must be in committed state now
            // * all modifications should be propagated to the parent
            // * no actual commit should occur.
            assertEquals(PersistenceState.COMMITTED, childNew.getPersistenceState());
            assertEquals(PersistenceState.COMMITTED, childModified.getPersistenceState());
            assertEquals(PersistenceState.COMMITTED, childCommitted.getPersistenceState());
            assertEquals(PersistenceState.HOLLOW, childHollow.getPersistenceState());
            ClientMtTable1 parentNew = (ClientMtTable1) clientContext.getGraphManager().getNode(childNew.getObjectId());
            final ClientMtTable1 parentModified = (ClientMtTable1) clientContext.getGraphManager().getNode(childModified.getObjectId());
            ClientMtTable1 parentCommitted = (ClientMtTable1) clientContext.getGraphManager().getNode(childCommitted.getObjectId());
            ClientMtTable1 parentHollow = (ClientMtTable1) clientContext.getGraphManager().getNode(childHollow.getObjectId());
            assertNotNull(parentNew);
            assertEquals(PersistenceState.NEW, parentNew.getPersistenceState());
            assertEquals("NNN", parentNew.getGlobalAttribute1());
            assertNotNull(parentModified);
            assertEquals(PersistenceState.MODIFIED, parentModified.getPersistenceState());
            assertEquals("MMM", parentModified.getGlobalAttribute1());
            assertNotNull(parentCommitted);
            assertEquals(PersistenceState.COMMITTED, parentCommitted.getPersistenceState());
            assertNotNull(parentHollow);
            // check that arc changes got recorded in the parent context
            GraphDiff diffs = clientContext.internalGraphManager().getDiffs();
            final int[] modifiedProperties = new int[1];
            diffs.apply(new GraphChangeHandler() {

                @Override
                public void arcCreated(Object nodeId, Object targetNodeId, Object arcId) {
                }

                @Override
                public void arcDeleted(Object nodeId, Object targetNodeId, Object arcId) {
                }

                @Override
                public void nodeCreated(Object nodeId) {
                }

                @Override
                public void nodeIdChanged(Object nodeId, Object newId) {
                }

                @Override
                public void nodePropertyChanged(Object nodeId, String property, Object oldValue, Object newValue) {
                    if (nodeId.equals(parentModified.getObjectId())) {
                        modifiedProperties[0]++;
                    }
                }

                @Override
                public void nodeRemoved(Object nodeId) {
                }
            });
            assertEquals(1, modifiedProperties[0]);
        }
    });
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) GraphDiff(org.apache.cayenne.graph.GraphDiff) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) GraphChangeHandler(org.apache.cayenne.graph.GraphChangeHandler) Test(org.junit.Test)

Aggregations

SelectQuery (org.apache.cayenne.query.SelectQuery)360 Test (org.junit.Test)348 Artist (org.apache.cayenne.testdo.testmap.Artist)128 Painting (org.apache.cayenne.testdo.testmap.Painting)75 UnitTestClosure (org.apache.cayenne.unit.di.UnitTestClosure)64 Expression (org.apache.cayenne.exp.Expression)47 List (java.util.List)43 ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)26 ValueHolder (org.apache.cayenne.ValueHolder)21 ReturnTypesMap1 (org.apache.cayenne.testdo.return_types.ReturnTypesMap1)18 ArrayList (java.util.ArrayList)16 Date (java.util.Date)13 DataRow (org.apache.cayenne.DataRow)12 ObjectContext (org.apache.cayenne.ObjectContext)12 DbEntity (org.apache.cayenne.map.DbEntity)12 SQLTemplate (org.apache.cayenne.query.SQLTemplate)11 ROArtist (org.apache.cayenne.testdo.testmap.ROArtist)11 HashMap (java.util.HashMap)10 ObjectId (org.apache.cayenne.ObjectId)10 Query (org.apache.cayenne.query.Query)10