use of org.apache.cayenne.access.DataContext in project cayenne by apache.
the class ExpressionIT method testMatch.
@Test
public void testMatch() {
assertTrue(context instanceof DataContext);
DataContext context2 = (DataContext) runtime.newContext();
Artist a1 = context.newObject(Artist.class);
a1.setArtistName("Equals");
Painting p1 = context.newObject(Painting.class);
p1.setToArtist(a1);
p1.setPaintingTitle("painting1");
context.commitChanges();
SelectQuery<Painting> query = new SelectQuery<Painting>(Painting.class);
Expression e = Painting.TO_ARTIST.eq(a1);
query.setQualifier(e);
assertNotSame(context2, context);
List<Painting> objects = context2.select(query);
assertEquals(1, objects.size());
// 2 same objects in different contexts
assertTrue(e.match(objects.get(0)));
// we change one object - so the objects are different now
// (PersistenceState different)
a1.setArtistName("newName");
SelectQuery<Painting> q2 = new SelectQuery<Painting>(Painting.class);
Expression ex2 = Painting.TO_ARTIST.eq(a1);
q2.setQualifier(ex2);
assertTrue(ex2.match(objects.get(0)));
Artist a2 = context.newObject(Artist.class);
a2.setArtistName("Equals");
context.commitChanges();
SelectQuery<Painting> q = new SelectQuery<Painting>(Painting.class);
Expression ex = Painting.TO_ARTIST.eq(a2);
q.setQualifier(ex);
// 2 different objects in different contexts
assertFalse(ex.match(objects.get(0)));
}
use of org.apache.cayenne.access.DataContext in project cayenne by apache.
the class DataContextFactory method createFromDataContext.
protected ObjectContext createFromDataContext(DataContext parent) {
// child ObjectStore should not have direct access to snapshot cache, so do not
// pass it in constructor.
ObjectStore objectStore = objectStoreFactory.createObjectStore(null);
DataContext context = newInstance(parent, objectStore);
context.setValidatingObjectsOnCommit(parent.isValidatingObjectsOnCommit());
context.setUsingSharedSnapshotCache(parent.isUsingSharedSnapshotCache());
context.setQueryCache(new NestedQueryCache(queryCache));
context.setTransactionFactory(transactionFactory);
return context;
}
use of org.apache.cayenne.access.DataContext in project cayenne by apache.
the class CDOOne2OneDepIT method testTakeObjectSnapshotDependentFault.
@Test
public void testTakeObjectSnapshotDependentFault() throws Exception {
// prepare data
Artist a1 = newArtist();
PaintingInfo pi1 = newPaintingInfo();
Painting p1 = newPainting();
p1.setToArtist(a1);
p1.setToPaintingInfo(pi1);
context.commitChanges();
context = context1;
Painting painting = fetchPainting();
assertTrue(painting.readPropertyDirectly("toPaintingInfo") instanceof Fault);
// test that taking a snapshot does not trigger a fault, and generally works well
DataRow snapshot = ((DataContext) context).currentSnapshot(painting);
assertEquals(paintingName, snapshot.get("PAINTING_TITLE"));
assertTrue(painting.readPropertyDirectly("toPaintingInfo") instanceof Fault);
}
use of org.apache.cayenne.access.DataContext in project cayenne by apache.
the class DataContextFactoryTest method testCreateDataContextValidation.
@Test
public void testCreateDataContextValidation() throws Exception {
final EventManager eventManager = new MockEventManager();
final DataDomain domain = new DataDomain("d1");
domain.setValidatingObjectsOnCommit(true);
Module testModule = binder -> {
binder.bind(JdbcEventLogger.class).to(Slf4jJdbcEventLogger.class);
binder.bind(DataDomain.class).toInstance(domain);
binder.bind(EventManager.class).toInstance(eventManager);
binder.bind(QueryCache.class).toInstance(new MapQueryCache(5));
binder.bind(RuntimeProperties.class).toInstance(new DefaultRuntimeProperties(Collections.<String, String>emptyMap()));
binder.bind(ObjectMapRetainStrategy.class).to(DefaultObjectMapRetainStrategy.class);
binder.bind(ObjectStoreFactory.class).to(DefaultObjectStoreFactory.class);
binder.bind(TransactionFactory.class).to(DefaultTransactionFactory.class);
binder.bind(TransactionManager.class).to(DefaultTransactionManager.class);
binder.bind(EventBridge.class).toProvider(NoopEventBridgeProvider.class);
binder.bind(DataRowStoreFactory.class).to(DefaultDataRowStoreFactory.class);
};
Injector injector = DIBootstrap.createInjector(testModule);
domain.setDataRowStoreFactory(injector.getInstance(DataRowStoreFactory.class));
DataContextFactory factory = new DataContextFactory();
injector.injectMembers(factory);
DataContext c1 = (DataContext) factory.createContext();
assertTrue(c1.isValidatingObjectsOnCommit());
domain.setValidatingObjectsOnCommit(false);
DataContext c2 = (DataContext) factory.createContext();
assertFalse(c2.isValidatingObjectsOnCommit());
}
use of org.apache.cayenne.access.DataContext in project cayenne by apache.
the class DataContextFactory method createFromGenericChannel.
protected ObjectContext createFromGenericChannel(DataChannel parent) {
// for new dataRowStores use the same name for all stores
// it makes it easier to track the event subject
DataRowStore snapshotCache = (dataDomain.isSharedCacheEnabled()) ? dataDomain.getSharedSnapshotCache() : dataRowStoreFactory.createDataRowStore(dataDomain.getName());
DataContext context = newInstance(parent, objectStoreFactory.createObjectStore(snapshotCache));
context.setValidatingObjectsOnCommit(dataDomain.isValidatingObjectsOnCommit());
context.setQueryCache(new NestedQueryCache(queryCache));
return context;
}
Aggregations