Search in sources :

Example 11 with SelectQuery

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

the class NestedCayenneContextIT method testPrefetchingToMany.

@Test
public void testPrefetchingToMany() throws Exception {
    ClientMtTable1 mt11 = clientContext.newObject(ClientMtTable1.class);
    mt11.setGlobalAttribute1("1");
    ClientMtTable1 mt12 = clientContext.newObject(ClientMtTable1.class);
    mt12.setGlobalAttribute1("2");
    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<ClientMtTable1> q = new SelectQuery<ClientMtTable1>(ClientMtTable1.class);
    q.addOrdering("globalAttribute1", SortOrder.ASCENDING);
    q.addPrefetch(ClientMtTable1.TABLE2ARRAY_PROPERTY);
    final List<ClientMtTable1> results = child.select(q);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            ClientMtTable1 o1 = results.get(0);
            assertEquals(PersistenceState.COMMITTED, o1.getPersistenceState());
            assertSame(child, o1.getObjectContext());
            List<ClientMtTable2> children1 = o1.getTable2Array();
            assertEquals(2, children1.size());
            Iterator<ClientMtTable2> it = children1.iterator();
            while (it.hasNext()) {
                ClientMtTable2 o = it.next();
                assertEquals(PersistenceState.COMMITTED, o.getPersistenceState());
                assertSame(child, o.getObjectContext());
                assertEquals(o1, o.getTable1());
            }
            ClientMtTable1 o2 = results.get(1);
            assertEquals(PersistenceState.COMMITTED, o2.getPersistenceState());
            assertSame(child, o2.getObjectContext());
            List<?> children2 = o2.getTable2Array();
            assertEquals(0, children2.size());
        }
    });
}
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) List(java.util.List) Test(org.junit.Test)

Example 12 with SelectQuery

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

the class NestedCayenneContextTooneIT method testCAY1636.

/*
 * was added for CAY-1636
 */
@Test
public void testCAY1636() throws Exception {
    ClientTooneMaster A = clientContext.newObject(ClientTooneMaster.class);
    clientContext.commitChanges();
    ClientTooneDep B = clientContext.newObject(ClientTooneDep.class);
    A.setToDependent(B);
    clientContext.commitChanges();
    ObjectContext child = runtime.newContext(clientContext);
    SelectQuery<ClientTooneMaster> query = new SelectQuery<ClientTooneMaster>(ClientTooneMaster.class);
    List<ClientTooneMaster> objects = child.select(query);
    assertEquals(1, objects.size());
    ClientTooneMaster childDeleted = (ClientTooneMaster) objects.get(0);
    child.deleteObjects(childDeleted);
    child.commitChangesToParent();
    ClientTooneMaster parentDeleted = (ClientTooneMaster) clientContext.getGraphManager().getNode(childDeleted.getObjectId());
    assertNotNull(parentDeleted);
    assertEquals(PersistenceState.DELETED, parentDeleted.getPersistenceState());
    clientContext.commitChanges();
    SelectQuery<ClientTooneMaster> query2 = new SelectQuery<ClientTooneMaster>(ClientTooneMaster.class);
    List<ClientTooneMaster> objects2 = child.select(query2);
    assertEquals(0, objects2.size());
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) ClientTooneMaster(org.apache.cayenne.testdo.toone.ClientTooneMaster) ClientTooneDep(org.apache.cayenne.testdo.toone.ClientTooneDep) Test(org.junit.Test)

Example 13 with SelectQuery

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

the class ClientServerChannelIT method testPerformQueryObjectIDInjection.

@Test
public void testPerformQueryObjectIDInjection() throws Exception {
    tMtTable1.insert(55, "g1", "s1");
    Query query = new SelectQuery("MtTable1");
    QueryResponse response = clientServerChannel.onQuery(null, query);
    assertNotNull(response);
    List<?> results = response.firstList();
    assertNotNull(results);
    assertEquals(1, results.size());
    Object result = results.get(0);
    assertTrue(result instanceof ClientMtTable1);
    ClientMtTable1 clientObject = (ClientMtTable1) result;
    assertNotNull(clientObject.getObjectId());
    assertEquals(new ObjectId("MtTable1", MtTable1.TABLE1_ID_PK_COLUMN, 55), clientObject.getObjectId());
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) SelectQuery(org.apache.cayenne.query.SelectQuery) MockQuery(org.apache.cayenne.query.MockQuery) Query(org.apache.cayenne.query.Query) ObjectId(org.apache.cayenne.ObjectId) QueryResponse(org.apache.cayenne.QueryResponse) Test(org.junit.Test)

Example 14 with SelectQuery

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

the class ClientServerChannelIT method testPerformQueryValuePropagation.

@Test
public void testPerformQueryValuePropagation() throws Exception {
    byte[] bytes = new byte[] { 1, 2, 3 };
    tMtTable3.insert(1, bytes, "abc", 4);
    Query query = new SelectQuery("MtTable3");
    QueryResponse response = clientServerChannel.onQuery(null, query);
    assertNotNull(response);
    List<?> results = response.firstList();
    assertNotNull(results);
    assertEquals(1, results.size());
    Object result = results.get(0);
    assertTrue("Result is of wrong type: " + result, result instanceof ClientMtTable3);
    ClientMtTable3 clientObject = (ClientMtTable3) result;
    assertEquals("abc", clientObject.getCharColumn());
    assertEquals(new Integer(4), clientObject.getIntColumn());
    assertTrue(new EqualsBuilder().append(clientObject.getBinaryColumn(), bytes).isEquals());
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) ClientMtTable3(org.apache.cayenne.testdo.mt.ClientMtTable3) SelectQuery(org.apache.cayenne.query.SelectQuery) MockQuery(org.apache.cayenne.query.MockQuery) Query(org.apache.cayenne.query.Query) QueryResponse(org.apache.cayenne.QueryResponse) EqualsBuilder(org.apache.cayenne.util.EqualsBuilder) Test(org.junit.Test)

Example 15 with SelectQuery

use of org.apache.cayenne.query.SelectQuery 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)

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