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