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());
}
});
}
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());
}
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());
}
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());
}
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());
}
});
}
Aggregations