Search in sources :

Example 21 with RefreshQuery

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());
}
Also used : CollectionToMany(org.apache.cayenne.testdo.relationships_collection_to_many.CollectionToMany) RefreshQuery(org.apache.cayenne.query.RefreshQuery) CollectionToManyTarget(org.apache.cayenne.testdo.relationships_collection_to_many.CollectionToManyTarget) Test(org.junit.Test)

Example 22 with RefreshQuery

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));
}
Also used : CollectionToMany(org.apache.cayenne.testdo.relationships_collection_to_many.CollectionToMany) RefreshQuery(org.apache.cayenne.query.RefreshQuery) CollectionToManyTarget(org.apache.cayenne.testdo.relationships_collection_to_many.CollectionToManyTarget) Test(org.junit.Test)

Example 23 with RefreshQuery

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;
}
Also used : ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Query(org.apache.cayenne.query.Query) RefreshQuery(org.apache.cayenne.query.RefreshQuery) RefreshQuery(org.apache.cayenne.query.RefreshQuery) Collection(java.util.Collection)

Example 24 with RefreshQuery

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);
        }
    });
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) ClientIdMapToManyTarget(org.apache.cayenne.testdo.map_to_many.ClientIdMapToManyTarget) RefreshQuery(org.apache.cayenne.query.RefreshQuery) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) ClientIdMapToMany(org.apache.cayenne.testdo.map_to_many.ClientIdMapToMany) ObjectContext(org.apache.cayenne.ObjectContext) Test(org.junit.Test)

Aggregations

RefreshQuery (org.apache.cayenne.query.RefreshQuery)24 Test (org.junit.Test)21 SelectQuery (org.apache.cayenne.query.SelectQuery)9 Artist (org.apache.cayenne.testdo.testmap.Artist)9 Painting (org.apache.cayenne.testdo.testmap.Painting)5 Map (java.util.Map)3 Set (java.util.Set)3 Expression (org.apache.cayenne.exp.Expression)3 ObjectIdQuery (org.apache.cayenne.query.ObjectIdQuery)3 Query (org.apache.cayenne.query.Query)3 IdMapToMany (org.apache.cayenne.testdo.map_to_many.IdMapToMany)3 MapToMany (org.apache.cayenne.testdo.map_to_many.MapToMany)3 MapToManyTarget (org.apache.cayenne.testdo.map_to_many.MapToManyTarget)3 CollectionToMany (org.apache.cayenne.testdo.relationships_collection_to_many.CollectionToMany)3 CollectionToManyTarget (org.apache.cayenne.testdo.relationships_collection_to_many.CollectionToManyTarget)3 SetToMany (org.apache.cayenne.testdo.relationships_set_to_many.SetToMany)3 SetToManyTarget (org.apache.cayenne.testdo.relationships_set_to_many.SetToManyTarget)3 Collection (java.util.Collection)2 ClientIdMapToMany (org.apache.cayenne.testdo.map_to_many.ClientIdMapToMany)2 ClientIdMapToManyTarget (org.apache.cayenne.testdo.map_to_many.ClientIdMapToManyTarget)2