Search in sources :

Example 46 with ObjectContext

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

the class VerticalInheritanceMultipleAttributesIT method testUpdateTwoObjects.

/**
 * @link https://issues.apache.org/jira/browse/CAY-2282
 */
@Test
public void testUpdateTwoObjects() throws SQLException {
    // Insert records we want to update
    ivOtherTable.insert(1, "other1");
    ivOtherTable.insert(2, "other2");
    ivBaseTable.insert(1, "Impl 1", "I");
    ivBaseTable.insert(2, "Impl 2", "I");
    ivImplTable.insert(1, "attr1", "attr2", 1, 2);
    ivImplTable.insert(2, "attr1", "attr2", 1, 2);
    // Fetch and update the records
    IvOther other1 = ObjectSelect.query(IvOther.class).where(IvOther.NAME.eq("other1")).selectOne(context);
    IvOther other2 = ObjectSelect.query(IvOther.class).where(IvOther.NAME.eq("other2")).selectOne(context);
    List<IvImpl> implResult = ObjectSelect.query(IvImpl.class).select(context);
    assertEquals(2, implResult.size());
    for (IvImpl record : implResult) {
        record.setName(record.getName() + "-Change");
        record.setAttr1(record.getAttr1() + "-Change");
        record.setAttr2(record.getAttr2() + "-Change");
        record.setOther1(other2);
        record.setOther2(other1);
    }
    context.commitChanges();
    // Check result via clean context
    ObjectContext cleanContext = runtime.newContext();
    implResult = ObjectSelect.query(IvImpl.class).select(cleanContext);
    assertEquals(2, implResult.size());
    for (IvImpl record : implResult) {
        assertTrue(record.getName().endsWith("-Change"));
        assertTrue(record.getAttr1().endsWith("-Change"));
        assertTrue(record.getAttr2().endsWith("-Change"));
        assertEquals(other2.getObjectId(), record.getOther1().getObjectId());
        assertEquals(other1.getObjectId(), record.getOther2().getObjectId());
    }
}
Also used : IvOther(org.apache.cayenne.testdo.inheritance_vertical.IvOther) ObjectContext(org.apache.cayenne.ObjectContext) IvImpl(org.apache.cayenne.testdo.inheritance_vertical.IvImpl) Test(org.junit.Test)

Example 47 with ObjectContext

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

the class PersistentObjectHolderTest method testInvalidate.

@Test
public void testInvalidate() {
    ObjectContext context = mock(ObjectContext.class);
    ClientMtTable2 o = new ClientMtTable2();
    o.setPersistenceState(PersistenceState.COMMITTED);
    o.setObjectContext(context);
    PersistentObjectHolder holder = new PersistentObjectHolder(o, ClientMtTable2.TABLE1.getName());
    assertTrue(holder.isFault());
    ClientMtTable1 o1 = new ClientMtTable1();
    o1.setObjectContext(context);
    holder.setValueDirectly(o1);
    holder.invalidate();
    assertTrue(holder.isFault());
    assertNull(holder.value);
}
Also used : ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ClientMtTable2(org.apache.cayenne.testdo.mt.ClientMtTable2) ObjectContext(org.apache.cayenne.ObjectContext) Test(org.junit.Test)

Example 48 with ObjectContext

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

the class ShallowMergeOperationIT method testMerge_NoOverride.

@Test
public void testMerge_NoOverride() throws Exception {
    createArtistsDataSet();
    ObjectContext childContext = runtime.newContext(context);
    final ShallowMergeOperation op = new ShallowMergeOperation(childContext);
    int modifiedId = 33003;
    final Artist modified = (Artist) Cayenne.objectForQuery(context, new ObjectIdQuery(ObjectId.of("Artist", Artist.ARTIST_ID_PK_COLUMN, modifiedId)));
    final Artist peerModified = (Artist) Cayenne.objectForQuery(childContext, new ObjectIdQuery(ObjectId.of("Artist", Artist.ARTIST_ID_PK_COLUMN, modifiedId)));
    modified.setArtistName("M1");
    peerModified.setArtistName("M2");
    assertEquals(PersistenceState.MODIFIED, modified.getPersistenceState());
    assertEquals(PersistenceState.MODIFIED, peerModified.getPersistenceState());
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            Persistent peerModified2 = op.merge(modified);
            assertSame(peerModified, peerModified2);
            assertEquals(PersistenceState.MODIFIED, peerModified2.getPersistenceState());
            assertEquals("M2", peerModified.getArtistName());
            assertEquals("M1", modified.getArtistName());
        }
    });
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) Persistent(org.apache.cayenne.Persistent) ObjectContext(org.apache.cayenne.ObjectContext) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Test(org.junit.Test)

Example 49 with ObjectContext

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

the class SessionContextRequestHandler method requestStart.

public void requestStart(ServletRequest request, ServletResponse response) {
    CayenneRuntime.bindThreadInjector(injector);
    if (request instanceof HttpServletRequest) {
        // this forces session creation if it does not exist yet
        HttpSession session = ((HttpServletRequest) request).getSession();
        ObjectContext context;
        synchronized (session) {
            context = (ObjectContext) session.getAttribute(SESSION_CONTEXT_KEY);
            if (context == null) {
                context = injector.getInstance(ObjectContextFactory.class).createContext();
                session.setAttribute(SESSION_CONTEXT_KEY, context);
            }
        }
        BaseContext.bindThreadObjectContext(context);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpSession(javax.servlet.http.HttpSession) ObjectContext(org.apache.cayenne.ObjectContext)

Example 50 with ObjectContext

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

the class ClientChannelServerDiffsIT method testReturnDiffInPrePersist.

@Test
public void testReturnDiffInPrePersist() {
    final List<GenericDiff> diffs = new ArrayList<>();
    final NoopGraphChangeHandler diffReader = new NoopGraphChangeHandler() {

        @Override
        public void nodePropertyChanged(Object nodeId, String property, Object oldValue, Object newValue) {
            super.nodePropertyChanged(nodeId, property, oldValue, newValue);
            diffs.add(new GenericDiff((ObjectId) nodeId, property, oldValue, newValue));
        }
    };
    LifecycleCallbackRegistry callbackRegistry = clientServerChannel.getEntityResolver().getCallbackRegistry();
    try {
        callbackRegistry.addListener(LifecycleEvent.POST_ADD, MtTable1.class, new ClientChannelServerDiffsListener1(), "prePersist");
        ClientChannel channel = new ClientChannel(connection, false, new MockEventManager(), false) {

            @Override
            public GraphDiff onSync(ObjectContext originatingContext, GraphDiff changes, int syncType) {
                GraphDiff serverDiff = super.onSync(originatingContext, changes, syncType);
                assertNotNull(serverDiff);
                serverDiff.apply(diffReader);
                return serverDiff;
            }
        };
        CayenneContext context = new CayenneContext(channel);
        ClientMtTable1 o = context.newObject(ClientMtTable1.class);
        ObjectId tempId = o.getObjectId();
        o.setServerAttribute1("YY");
        context.commitChanges();
        assertEquals(2, diffReader.size);
        assertEquals(1, diffs.size());
        assertEquals(tempId, diffs.get(0).sourceId);
        assertEquals(ClientMtTable1.GLOBAL_ATTRIBUTE1.getName(), diffs.get(0).property);
        assertNull(diffs.get(0).oldValue);
        assertEquals("XXX", diffs.get(0).newValue);
    } finally {
        callbackRegistry.clear();
    }
}
Also used : ObjectId(org.apache.cayenne.ObjectId) GraphDiff(org.apache.cayenne.graph.GraphDiff) ArrayList(java.util.ArrayList) CayenneContext(org.apache.cayenne.CayenneContext) LifecycleCallbackRegistry(org.apache.cayenne.reflect.LifecycleCallbackRegistry) ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ObjectContext(org.apache.cayenne.ObjectContext) MockEventManager(org.apache.cayenne.event.MockEventManager) 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