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