use of org.apache.cayenne.query.RefreshQuery in project cayenne by apache.
the class CDOCollectionRelationshipIT method testAddToManyViaReverse.
@Test
public void testAddToManyViaReverse() throws Exception {
CollectionToMany o1 = Cayenne.objectForPK(context, CollectionToMany.class, 1);
Collection<?> targets = o1.getTargets();
assertNotNull(targets);
assertEquals(3, targets.size());
CollectionToManyTarget newTarget = o1.getObjectContext().newObject(CollectionToManyTarget.class);
newTarget.setCollectionToMany(o1);
assertEquals(4, targets.size());
assertTrue(o1.getTargets().contains(newTarget));
assertSame(o1, newTarget.getCollectionToMany());
o1.getObjectContext().commitChanges();
o1.getObjectContext().performGenericQuery(new RefreshQuery());
assertEquals(4, o1.getTargets().size());
}
use of org.apache.cayenne.query.RefreshQuery in project cayenne by apache.
the class CDOCollectionRelationshipIT method testRemoveToMany.
@Test
public void testRemoveToMany() throws Exception {
CollectionToMany o1 = Cayenne.objectForPK(context, CollectionToMany.class, 1);
Collection<?> targets = o1.getTargets();
assertEquals(3, targets.size());
CollectionToManyTarget target = Cayenne.objectForPK(o1.getObjectContext(), CollectionToManyTarget.class, 2);
o1.removeFromTargets(target);
assertEquals(2, targets.size());
assertFalse(o1.getTargets().contains(target));
assertNull(target.getCollectionToMany());
o1.getObjectContext().commitChanges();
o1.getObjectContext().performGenericQuery(new RefreshQuery());
assertEquals(2, o1.getTargets().size());
assertFalse(o1.getTargets().contains(target));
}
use of org.apache.cayenne.query.RefreshQuery in project cayenne by apache.
the class DataContextQueryAction method interceptRefreshQuery.
@Override
protected boolean interceptRefreshQuery() {
if (query instanceof RefreshQuery) {
RefreshQuery refreshQuery = (RefreshQuery) query;
DataContext context = (DataContext) actingContext;
// 1. refresh all
if (refreshQuery.isRefreshAll()) {
synchronized (context.getObjectStore()) {
invalidateLocally(context.getObjectStore(), context.getObjectStore().getObjectIterator());
context.getQueryCache().clear();
}
// cascade
return !DONE;
}
// 2. invalidate object collection
Collection objects = refreshQuery.getObjects();
if (objects != null && !objects.isEmpty()) {
synchronized (context.getObjectStore()) {
invalidateLocally(context.getObjectStore(), objects.iterator());
}
// cascade
return !DONE;
}
// 3. refresh query - have to do it eagerly to refresh the objects involved
Query cachedQuery = refreshQuery.getQuery();
if (cachedQuery != null) {
String cacheKey = cachedQuery.getMetaData(context.getEntityResolver()).getCacheKey();
context.getQueryCache().remove(cacheKey);
this.response = context.performGenericQuery(cachedQuery);
// do not cascade to avoid running query twice
return DONE;
}
// 4. refresh groups...
String[] groups = refreshQuery.getGroupKeys();
if (groups != null && groups.length > 0) {
for (String group : groups) {
context.getQueryCache().removeGroup(group);
}
// cascade group invalidation
return !DONE;
}
// shouldn't ever happen
return DONE;
}
return !DONE;
}
use of org.apache.cayenne.query.RefreshQuery in project cayenne by apache.
the class ROPPrefetchToManyMapIT method test.
@Test
public void test() throws Exception {
ObjectContext context = createROPContext();
ClientIdMapToMany map = context.newObject(ClientIdMapToMany.class);
ClientIdMapToManyTarget target = context.newObject(ClientIdMapToManyTarget.class);
target.setMapToMany(map);
context.commitChanges();
context.performQuery(new RefreshQuery());
SelectQuery<ClientIdMapToMany> query = new SelectQuery<ClientIdMapToMany>(ClientIdMapToMany.class);
query.addPrefetch("targets");
final ClientIdMapToMany mapToMany = (ClientIdMapToMany) Cayenne.objectForQuery(context, query);
queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {
public void execute() {
assertEquals(mapToMany.getTargets().size(), 1);
}
});
}
Aggregations