Search in sources :

Example 56 with ObjectContext

use of org.apache.cayenne.ObjectContext in project cayenne by apache.

the class CacheInvalidationCustomHandlerIT method testInvalidate.

@Test
public void testInvalidate() throws Exception {
    ObjectContext context = runtime.newContext();
    // no explicit cache group must still work - it lands inside default cache called 'cayenne.default.cache'
    ObjectSelect<E1> g0 = ObjectSelect.query(E1.class).localCache();
    ObjectSelect<E1> g1 = ObjectSelect.query(E1.class).localCache("g1");
    ObjectSelect<E1> g2 = ObjectSelect.query(E1.class).localCache("g2");
    assertEquals(0, g0.selectCount(context));
    assertEquals(0, g1.selectCount(context));
    assertEquals(0, g2.selectCount(context));
    e1.insert(1).insert(2);
    // inserted via SQL... query results are still cached...
    assertEquals(0, g0.selectCount(context));
    assertEquals(0, g1.selectCount(context));
    assertEquals(0, g2.selectCount(context));
    E1 e1 = context.newObject(E1.class);
    context.commitChanges();
    // inserted via Cayenne... "g1" should get auto refreshed...
    assertEquals(0, g0.selectCount(context));
    assertEquals(3, g1.selectCount(context));
    assertEquals(0, g2.selectCount(context));
    context.deleteObject(e1);
    context.commitChanges();
    // deleted via Cayenne... "g1" should get auto refreshed
    assertEquals(0, g0.selectCount(context));
    assertEquals(2, g1.selectCount(context));
    assertEquals(0, g2.selectCount(context));
}
Also used : ObjectContext(org.apache.cayenne.ObjectContext) E1(org.apache.cayenne.cache.invalidation.db.E1) Test(org.junit.Test)

Example 57 with ObjectContext

use of org.apache.cayenne.ObjectContext in project cayenne by apache.

the class ObjectStore method processSnapshotEvent.

/**
 * @since 1.2
 */
synchronized void processSnapshotEvent(SnapshotEvent event) {
    Map<ObjectId, DataRow> modifiedDiffs = event.getModifiedDiffs();
    if (modifiedDiffs != null && !modifiedDiffs.isEmpty()) {
        for (Map.Entry<ObjectId, DataRow> entry : modifiedDiffs.entrySet()) {
            processUpdatedSnapshot(entry.getKey(), entry.getValue());
        }
    }
    Collection<ObjectId> deletedIDs = event.getDeletedIds();
    if (deletedIDs != null && !deletedIDs.isEmpty()) {
        for (ObjectId deletedID : deletedIDs) {
            processDeletedID(deletedID);
        }
    }
    processInvalidatedIDs(event.getInvalidatedIds());
    processIndirectlyModifiedIDs(event.getIndirectlyModifiedIds());
    // TODO: andrus, 3/28/2006 - 'SnapshotEventDecorator' serves as a bridge (or
    // rather a noop wrapper) between old snapshot events and new GraphEvents. Once
    // SnapshotEvents are replaced with GraphEvents (in 2.0) we won't need it
    GraphDiff diff = new SnapshotEventDecorator(event);
    ObjectContext originatingContext = (event.getPostedBy() instanceof ObjectContext) ? (ObjectContext) event.getPostedBy() : null;
    context.fireDataChannelChanged(originatingContext, diff);
}
Also used : ObjectId(org.apache.cayenne.ObjectId) GraphDiff(org.apache.cayenne.graph.GraphDiff) ObjectContext(org.apache.cayenne.ObjectContext) DataRow(org.apache.cayenne.DataRow) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 58 with ObjectContext

use of org.apache.cayenne.ObjectContext in project cayenne by apache.

the class DataContextLocalObjectIT method testLocalObject_TempId_NestedContext.

@Test
public void testLocalObject_TempId_NestedContext() throws Exception {
    final Artist a1 = context1.newObject(Artist.class);
    final ObjectContext nestedContext = runtime.newContext(context1);
    interceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            Artist a3 = nestedContext.localObject(a1);
            assertNotSame(a3, a1);
            assertEquals(a3.getObjectId(), a1.getObjectId());
            assertSame(nestedContext, a3.getObjectContext());
        }
    });
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) ObjectContext(org.apache.cayenne.ObjectContext) Test(org.junit.Test)

Example 59 with ObjectContext

use of org.apache.cayenne.ObjectContext in project cayenne by apache.

the class ToOneFault method resolveFault.

/**
 * Resolves this fault to a DataObject.
 */
@Override
public Object resolveFault(Persistent sourceObject, String relationshipName) {
    if (sourceObject.getObjectContext() == null) {
        throw new IllegalStateException("Null ObjectContext. Can't read a to-one relationship '" + relationshipName + "' for an object with ID: " + sourceObject.getObjectId());
    }
    int state = sourceObject.getPersistenceState();
    if (state == PersistenceState.NEW) {
        return null;
    }
    Object target = doResolveFault(sourceObject, relationshipName);
    // must update the diff for the object
    ObjectContext context = sourceObject.getObjectContext();
    if ((state == PersistenceState.MODIFIED || state == PersistenceState.DELETED) && context instanceof DataContext) {
        ObjectDiff diff = ((DataContext) context).getObjectStore().getChangesByObjectId().get(sourceObject.getObjectId());
        if (diff != null) {
            diff.updateArcSnapshot(relationshipName, (Persistent) target);
        }
    }
    return target;
}
Also used : ObjectContext(org.apache.cayenne.ObjectContext)

Example 60 with ObjectContext

use of org.apache.cayenne.ObjectContext in project cayenne by apache.

the class EJBQLQueryIT method testSelectFromNestedContext.

@Test
public void testSelectFromNestedContext() throws Exception {
    tArtist.insert(1, "a1");
    tArtist.insert(2, "a2");
    tPainting.insert(1, 2, "title1");
    tPainting.insert(2, 1, "title2");
    tPainting.insert(3, 1, "title3");
    ObjectContext nested = runtime.newContext(context);
    EJBQLQuery query = new EJBQLQuery("SELECT a, COUNT(a.paintingArray) FROM Artist a GROUP BY a");
    List<Object[]> result = nested.performQuery(query);
    assertEquals(2, result.size());
    for (Object[] next : result) {
        assertTrue(next[0] instanceof Artist);
        assertTrue(next[1] instanceof Number);
    }
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) ObjectContext(org.apache.cayenne.ObjectContext) Test(org.junit.Test)

Aggregations

ObjectContext (org.apache.cayenne.ObjectContext)127 Test (org.junit.Test)116 Artist (org.apache.cayenne.testdo.testmap.Artist)35 Painting (org.apache.cayenne.testdo.testmap.Painting)14 ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)12 Table1 (org.apache.cayenne.crypto.db.Table1)10 ParallelTestContainer (org.apache.cayenne.test.parallel.ParallelTestContainer)10 ObjectId (org.apache.cayenne.ObjectId)8 GraphDiff (org.apache.cayenne.graph.GraphDiff)8 Table2 (org.apache.cayenne.crypto.db.Table2)7 UnitTestClosure (org.apache.cayenne.unit.di.UnitTestClosure)7 HashMap (java.util.HashMap)6 Persistent (org.apache.cayenne.Persistent)6 ClientMtTable2 (org.apache.cayenne.testdo.mt.ClientMtTable2)6 IvImpl (org.apache.cayenne.testdo.inheritance_vertical.IvImpl)5 CayenneContext (org.apache.cayenne.CayenneContext)4 DataChannel (org.apache.cayenne.DataChannel)4 QueryResponse (org.apache.cayenne.QueryResponse)4 ObjectContextFactory (org.apache.cayenne.configuration.ObjectContextFactory)4 EntityResolver (org.apache.cayenne.map.EntityResolver)4