Search in sources :

Example 6 with DataContext

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)));
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) SelectQuery(org.apache.cayenne.query.SelectQuery) DataContext(org.apache.cayenne.access.DataContext) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 7 with DataContext

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;
}
Also used : ObjectStore(org.apache.cayenne.access.ObjectStore) DataContext(org.apache.cayenne.access.DataContext) NestedQueryCache(org.apache.cayenne.cache.NestedQueryCache)

Example 8 with DataContext

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);
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) DataContext(org.apache.cayenne.access.DataContext) PaintingInfo(org.apache.cayenne.testdo.testmap.PaintingInfo) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 9 with DataContext

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());
}
Also used : RuntimeProperties(org.apache.cayenne.configuration.RuntimeProperties) Injector(org.apache.cayenne.di.Injector) DataContext(org.apache.cayenne.access.DataContext) DataRowStoreFactory(org.apache.cayenne.access.DataRowStoreFactory) MapQueryCache(org.apache.cayenne.cache.MapQueryCache) TransactionManager(org.apache.cayenne.tx.TransactionManager) QueryCache(org.apache.cayenne.cache.QueryCache) Module(org.apache.cayenne.di.Module) ObjectMapRetainStrategy(org.apache.cayenne.access.ObjectMapRetainStrategy) ObjectStoreFactory(org.apache.cayenne.configuration.ObjectStoreFactory) DefaultRuntimeProperties(org.apache.cayenne.configuration.DefaultRuntimeProperties) DataDomain(org.apache.cayenne.access.DataDomain) Slf4jJdbcEventLogger(org.apache.cayenne.log.Slf4jJdbcEventLogger) EventBridge(org.apache.cayenne.event.EventBridge) MockEventManager(org.apache.cayenne.event.MockEventManager) JdbcEventLogger(org.apache.cayenne.log.JdbcEventLogger) TransactionFactory(org.apache.cayenne.tx.TransactionFactory) DefaultDataRowStoreFactory(org.apache.cayenne.access.DefaultDataRowStoreFactory) DIBootstrap(org.apache.cayenne.di.DIBootstrap) DefaultTransactionManager(org.apache.cayenne.tx.DefaultTransactionManager) DefaultTransactionFactory(org.apache.cayenne.tx.DefaultTransactionFactory) Test(org.junit.Test) DefaultObjectMapRetainStrategy(org.apache.cayenne.access.DefaultObjectMapRetainStrategy) EventManager(org.apache.cayenne.event.EventManager) NoopEventBridgeProvider(org.apache.cayenne.event.NoopEventBridgeProvider) Assert(org.junit.Assert) Collections(java.util.Collections) DefaultObjectStoreFactory(org.apache.cayenne.configuration.DefaultObjectStoreFactory) DataDomain(org.apache.cayenne.access.DataDomain) MockEventManager(org.apache.cayenne.event.MockEventManager) EventManager(org.apache.cayenne.event.EventManager) DefaultObjectStoreFactory(org.apache.cayenne.configuration.DefaultObjectStoreFactory) MapQueryCache(org.apache.cayenne.cache.MapQueryCache) DefaultRuntimeProperties(org.apache.cayenne.configuration.DefaultRuntimeProperties) DefaultDataRowStoreFactory(org.apache.cayenne.access.DefaultDataRowStoreFactory) DefaultTransactionManager(org.apache.cayenne.tx.DefaultTransactionManager) DataContext(org.apache.cayenne.access.DataContext) DefaultObjectMapRetainStrategy(org.apache.cayenne.access.DefaultObjectMapRetainStrategy) DefaultTransactionFactory(org.apache.cayenne.tx.DefaultTransactionFactory) Slf4jJdbcEventLogger(org.apache.cayenne.log.Slf4jJdbcEventLogger) Injector(org.apache.cayenne.di.Injector) NoopEventBridgeProvider(org.apache.cayenne.event.NoopEventBridgeProvider) Module(org.apache.cayenne.di.Module) MockEventManager(org.apache.cayenne.event.MockEventManager) DataRowStoreFactory(org.apache.cayenne.access.DataRowStoreFactory) DefaultDataRowStoreFactory(org.apache.cayenne.access.DefaultDataRowStoreFactory) Test(org.junit.Test)

Example 10 with DataContext

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;
}
Also used : DataContext(org.apache.cayenne.access.DataContext) NestedQueryCache(org.apache.cayenne.cache.NestedQueryCache) DataRowStore(org.apache.cayenne.access.DataRowStore)

Aggregations

DataContext (org.apache.cayenne.access.DataContext)12 Test (org.junit.Test)8 Module (org.apache.cayenne.di.Module)4 Assert (org.junit.Assert)4 Collections (java.util.Collections)3 NestedQueryCache (org.apache.cayenne.cache.NestedQueryCache)3 ObjectContextFactory (org.apache.cayenne.configuration.ObjectContextFactory)3 DIBootstrap (org.apache.cayenne.di.DIBootstrap)3 EventManager (org.apache.cayenne.event.EventManager)3 TransactionFactory (org.apache.cayenne.tx.TransactionFactory)3 Collection (java.util.Collection)2 DataChannel (org.apache.cayenne.DataChannel)2 ObjectContext (org.apache.cayenne.ObjectContext)2 ClientServerChannel (org.apache.cayenne.access.ClientServerChannel)2 DataDomain (org.apache.cayenne.access.DataDomain)2 DataRowStore (org.apache.cayenne.access.DataRowStore)2 DataRowStoreFactory (org.apache.cayenne.access.DataRowStoreFactory)2 DefaultDataRowStoreFactory (org.apache.cayenne.access.DefaultDataRowStoreFactory)2 DefaultObjectMapRetainStrategy (org.apache.cayenne.access.DefaultObjectMapRetainStrategy)2 ObjectMapRetainStrategy (org.apache.cayenne.access.ObjectMapRetainStrategy)2