Search in sources :

Example 16 with ClientMtTable2

use of org.apache.cayenne.testdo.mt.ClientMtTable2 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 17 with ClientMtTable2

use of org.apache.cayenne.testdo.mt.ClientMtTable2 in project cayenne by apache.

the class PersistentObjectInContextIT method testResolveToOneReverseResolved.

@Test
public void testResolveToOneReverseResolved() throws Exception {
    createTwoMtTable1sAnd2sDataSet();
    ObjectId gid = new ObjectId("MtTable2", MtTable2.TABLE2_ID_PK_COLUMN, new Integer(1));
    ClientMtTable2 mtTable21 = (ClientMtTable2) Cayenne.objectForQuery(context, new ObjectIdQuery(gid));
    assertNotNull(mtTable21);
    ClientMtTable1 mtTable1 = mtTable21.getTable1();
    assertNotNull("To one relationship incorrectly resolved to null", mtTable1);
    List<ClientMtTable2> list = mtTable1.getTable2Array();
    assertNotNull(list);
    assertTrue(list instanceof ValueHolder);
    assertTrue(((ValueHolder) list).isFault());
    // resolve it here...
    assertEquals(2, list.size());
    for (ClientMtTable2 t2 : list) {
        PersistentObjectHolder holder = (PersistentObjectHolder) t2.getTable1Direct();
        assertFalse(holder.isFault());
        assertSame(mtTable1, holder.getValue());
    }
    assertEquals("g1", mtTable1.getGlobalAttribute1());
}
Also used : PersistentObjectHolder(org.apache.cayenne.util.PersistentObjectHolder) ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ClientMtTable2(org.apache.cayenne.testdo.mt.ClientMtTable2) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Test(org.junit.Test)

Example 18 with ClientMtTable2

use of org.apache.cayenne.testdo.mt.ClientMtTable2 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 19 with ClientMtTable2

use of org.apache.cayenne.testdo.mt.ClientMtTable2 in project cayenne by apache.

the class ClientExpressionIT method testExpressionFactoryMatch.

@Test
public void testExpressionFactoryMatch() throws Exception {
    createDataSet();
    SelectQuery<ClientMtTable1> table1Query = new SelectQuery<ClientMtTable1>(ClientMtTable1.class);
    table1Query.addOrdering(new Ordering("db:TABLE1_ID", SortOrder.ASCENDING));
    List<ClientMtTable1> table1List = context.select(table1Query);
    assertNotNull(table1List);
    ClientMtTable1 element_1 = table1List.get(0);
    ClientMtTable1 element_2 = table1List.get(1);
    Expression exp = ExpressionFactory.matchExp(ClientMtTable2.TABLE1_PROPERTY, element_1);
    SelectQuery<ClientMtTable2> table2Query = new SelectQuery<ClientMtTable2>(ClientMtTable2.class, exp);
    List<ClientMtTable2> table2List = context.select(table2Query);
    assertNotNull(table2List);
    assertEquals(2, table2List.size());
    exp = ExpressionFactory.matchExp(element_2);
    table2Query = new SelectQuery<ClientMtTable2>(ClientMtTable2.class, exp);
    table2List = context.select(table2Query);
    assertNotNull(table2List);
    assertEquals(2, table2List.size());
}
Also used : ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ClientMtTable2(org.apache.cayenne.testdo.mt.ClientMtTable2) Expression(org.apache.cayenne.exp.Expression) Test(org.junit.Test)

Example 20 with ClientMtTable2

use of org.apache.cayenne.testdo.mt.ClientMtTable2 in project cayenne by apache.

the class ClientExpressionIT method testParams.

@Test
public void testParams() throws Exception {
    createDataSet();
    SelectQuery<ClientMtTable1> table1Query = new SelectQuery<ClientMtTable1>(ClientMtTable1.class);
    table1Query.addOrdering(new Ordering("db:TABLE1_ID", SortOrder.ASCENDING));
    List<ClientMtTable1> table1List = context.select(table1Query);
    ClientMtTable1 element_1 = table1List.get(0);
    ClientMtTable1 element_5 = table1List.get(4);
    Expression exp = ExpressionFactory.exp("table1 = $attr");
    exp = exp.params(Collections.singletonMap("attr", element_1));
    SelectQuery<ClientMtTable2> table2Query = new SelectQuery<ClientMtTable2>(ClientMtTable2.class, exp);
    List<ClientMtTable2> table2List = context.select(table2Query);
    assertEquals(2, table2List.size());
    exp = exp.andExp(ExpressionFactory.exp("table1 = $attr")).params(Collections.singletonMap("attr", element_5));
    table2Query = new SelectQuery<ClientMtTable2>(ClientMtTable2.class, exp);
    table2List = context.select(table2Query);
    assertEquals(0, table2List.size());
}
Also used : ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ClientMtTable2(org.apache.cayenne.testdo.mt.ClientMtTable2) Expression(org.apache.cayenne.exp.Expression) Test(org.junit.Test)

Aggregations

ClientMtTable2 (org.apache.cayenne.testdo.mt.ClientMtTable2)48 Test (org.junit.Test)48 ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)46 UnitTestClosure (org.apache.cayenne.unit.di.UnitTestClosure)9 Expression (org.apache.cayenne.exp.Expression)7 SelectQuery (org.apache.cayenne.query.SelectQuery)7 ObjectContext (org.apache.cayenne.ObjectContext)6 ObjectIdQuery (org.apache.cayenne.query.ObjectIdQuery)6 Iterator (java.util.Iterator)3 List (java.util.List)3 GraphDiff (org.apache.cayenne.graph.GraphDiff)3 PersistentObjectHolder (org.apache.cayenne.util.PersistentObjectHolder)3 GraphChangeHandler (org.apache.cayenne.graph.GraphChangeHandler)2 LifecycleCallbackRegistry (org.apache.cayenne.reflect.LifecycleCallbackRegistry)2 ClientChannelServerDiffsListener1 (org.apache.cayenne.remote.ClientChannelServerDiffsListener1)2 ROPSerializationService (org.apache.cayenne.rop.ROPSerializationService)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 CayenneContext (org.apache.cayenne.CayenneContext)1 ValueHolder (org.apache.cayenne.ValueHolder)1