Search in sources :

Example 86 with ObjectContext

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

the class CacheInvalidationCacheGroupsHandlerIT method invalidateE2.

@Test
public void invalidateE2() throws Exception {
    ObjectContext context = runtime.newContext();
    ObjectSelect<E2> g0 = ObjectSelect.query(E2.class).localCache();
    ObjectSelect<E2> g1 = ObjectSelect.query(E2.class).localCache("g1");
    ObjectSelect<E2> g2 = ObjectSelect.query(E2.class).localCache("g2");
    ObjectSelect<E2> g3 = ObjectSelect.query(E2.class).localCache("g3");
    ObjectSelect<E2> g5 = ObjectSelect.query(E2.class).localCache("g5");
    assertEquals(0, g0.selectCount(context));
    assertEquals(0, g1.selectCount(context));
    assertEquals(0, g2.selectCount(context));
    assertEquals(0, g3.selectCount(context));
    assertEquals(0, g5.selectCount(context));
    e2.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));
    assertEquals(0, g3.selectCount(context));
    assertEquals(0, g5.selectCount(context));
    context.newObject(E2.class);
    context.commitChanges();
    // Typed remove will actually call untyped version, thus 4 + 2
    assertEquals(4 + 2, removeGroupUntypedCounter.get());
    assertEquals(2, removeGroupTypedCounter.get());
    // inserted via Cayenne... "g1" and "g2" should get auto refreshed...
    assertEquals(0, g0.selectCount(context));
    assertEquals(3, g1.selectCount(context));
    assertEquals(3, g2.selectCount(context));
    assertEquals(3, g3.selectCount(context));
    assertEquals(3, g5.selectCount(context));
}
Also used : ObjectContext(org.apache.cayenne.ObjectContext) E2(org.apache.cayenne.cache.invalidation.db.E2) Test(org.junit.Test)

Example 87 with ObjectContext

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

the class CacheInvalidationCacheGroupsHandlerIT method invalidateE1.

@Test
public void invalidateE1() throws Exception {
    ObjectContext context = runtime.newContext();
    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));
    context.newObject(E1.class);
    context.commitChanges();
    assertEquals(2, removeGroupUntypedCounter.get());
    assertEquals(0, removeGroupTypedCounter.get());
    // inserted via Cayenne... "g1" and "g2" should get auto refreshed...
    assertEquals(0, g0.selectCount(context));
    assertEquals(3, g1.selectCount(context));
    assertEquals(3, g2.selectCount(context));
}
Also used : ObjectContext(org.apache.cayenne.ObjectContext) E1(org.apache.cayenne.cache.invalidation.db.E1) Test(org.junit.Test)

Example 88 with ObjectContext

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

the class AshwoodEntitySorter method findReflexiveMaster.

protected Persistent findReflexiveMaster(Persistent object, ObjRelationship toOneRel, String targetEntityName) {
    DbRelationship finalRel = toOneRel.getDbRelationships().get(0);
    ObjectContext context = object.getObjectContext();
    // how do we handle this for NEW objects correctly? For now bail from the method
    if (object.getObjectId().isTemporary()) {
        return null;
    }
    ObjectIdQuery query = new ObjectIdQuery(object.getObjectId(), true, ObjectIdQuery.CACHE);
    QueryResponse response = context.getChannel().onQuery(null, query);
    List<?> result = response.firstList();
    if (result == null || result.size() == 0) {
        return null;
    }
    DataRow snapshot = (DataRow) result.get(0);
    ObjectId id = snapshot.createTargetObjectId(targetEntityName, finalRel);
    // we only care about objects participating in transaction, so no need to create hollow objects
    return (id != null) ? (Persistent) context.getGraphManager().getNode(id) : null;
}
Also used : ObjectId(org.apache.cayenne.ObjectId) DbRelationship(org.apache.cayenne.map.DbRelationship) QueryResponse(org.apache.cayenne.QueryResponse) ObjectContext(org.apache.cayenne.ObjectContext) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) DataRow(org.apache.cayenne.DataRow)

Example 89 with ObjectContext

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

the class DataContextDataChannelEventsIT method testChangeEventOnPeerChangeSecondNestingLevel.

@Test
public void testChangeEventOnPeerChangeSecondNestingLevel() throws Exception {
    ObjectContext childPeer1 = runtime.newContext(context);
    Artist a = childPeer1.newObject(Artist.class);
    a.setArtistName("X");
    childPeer1.commitChanges();
    final MockChannelListener listener = new MockChannelListener();
    EventUtil.listenForChannelEvents((DataChannel) childPeer1, listener);
    ObjectContext childPeer2 = runtime.newContext(context);
    Artist a1 = childPeer2.localObject(a);
    a1.setArtistName("Y");
    childPeer2.commitChangesToParent();
    new ParallelTestContainer() {

        @Override
        protected void assertResult() throws Exception {
            assertFalse(listener.graphCommitted);
            assertTrue(listener.graphChanged);
            assertFalse(listener.graphRolledBack);
        }
    }.runTest(10000);
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) ObjectContext(org.apache.cayenne.ObjectContext) ParallelTestContainer(org.apache.cayenne.test.parallel.ParallelTestContainer) Test(org.junit.Test)

Example 90 with ObjectContext

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

the class Http2Client method main.

public static void main(String[] args) throws Exception {
    HttpsURLConnection.setDefaultHostnameVerifier((hostname, sslSession) -> hostname.equals("localhost"));
    System.setProperty("javax.net.ssl.trustStore", Http2Client.class.getResource("/keystore").getPath());
    // Setting Protostuff properties
    System.setProperty("protostuff.runtime.collection_schema_on_repeated_fields", "true");
    System.setProperty("protostuff.runtime.morph_collection_interfaces", "true");
    System.setProperty("protostuff.runtime.morph_map_interfaces", "true");
    System.setProperty("protostuff.runtime.pojo_schema_on_collection_fields", "true");
    System.setProperty("protostuff.runtime.pojo_schema_on_map_fields", "true");
    Map<String, String> properties = new HashMap<>();
    properties.put(ClientConstants.ROP_SERVICE_URL_PROPERTY, "https://localhost:8443/");
    properties.put(ClientConstants.ROP_SERVICE_USE_ALPN_PROPERTY, "false");
    properties.put(ClientConstants.ROP_SERVICE_USERNAME_PROPERTY, "cayenne-user");
    properties.put(ClientConstants.ROP_SERVICE_PASSWORD_PROPERTY, "secret");
    properties.put(ClientConstants.ROP_SERVICE_REALM_PROPERTY, "Cayenne Realm");
    ClientRuntime runtime = ClientRuntime.builder().properties(properties).addModule(new ClientJettyHttp2Module()).build();
    ObjectContext context = runtime.newContext();
    newObjectsTutorial(context);
    selectTutorial(context);
    deleteTutorial(context);
    runtime.shutdown();
}
Also used : ClientRuntime(org.apache.cayenne.configuration.rop.client.ClientRuntime) ClientJettyHttp2Module(org.apache.cayenne.configuration.rop.client.ClientJettyHttp2Module) HashMap(java.util.HashMap) ObjectContext(org.apache.cayenne.ObjectContext)

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