Search in sources :

Example 26 with ObjectContext

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

the class NestedDataContextWriteIT method testCAY1194.

@Test
public void testCAY1194() throws Exception {
    DataContext context = createDataContext();
    Artist artist = context.newObject(Artist.class);
    artist.setArtistName("111");
    ObjectContext child = runtime.newContext(context);
    Painting painting = child.newObject(Painting.class);
    painting.setPaintingTitle("222");
    Artist localParentMt = child.localObject(artist);
    assertEquals(0, artist.getPaintingArray().size());
    assertEquals(0, localParentMt.getPaintingArray().size());
    painting.setToArtist(localParentMt);
    assertEquals(0, artist.getPaintingArray().size());
    assertEquals(1, localParentMt.getPaintingArray().size());
    assertEquals(localParentMt.getPaintingArray().get(0).getObjectContext(), child);
    child.commitChangesToParent();
    assertEquals(1, artist.getPaintingArray().size());
    assertEquals(artist.getPaintingArray().get(0).getObjectContext(), context);
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) ObjectContext(org.apache.cayenne.ObjectContext) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 27 with ObjectContext

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

the class NestedDataContextWriteIT method testDeleteNew.

@Test
public void testDeleteNew() throws Exception {
    createSingleArtistDataSet();
    DataContext context = createDataContext();
    ObjectContext childContext = runtime.newContext(context);
    Artist a = Cayenne.objectForPK(childContext, Artist.class, 33001);
    Painting p = childContext.newObject(Painting.class);
    p.setPaintingTitle("X");
    a.addToPaintingArray(p);
    childContext.commitChangesToParent();
    childContext.deleteObjects(p);
    a.removeFromPaintingArray(p);
    childContext.commitChangesToParent();
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) ObjectContext(org.apache.cayenne.ObjectContext) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 28 with ObjectContext

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

the class NestedDataContextWriteIT method testCommitChanges.

@Test
public void testCommitChanges() throws Exception {
    createArtistsDataSet();
    DataContext context = createDataContext();
    ObjectContext childContext = runtime.newContext(context);
    // make sure we fetch in predictable order
    SelectQuery query = new SelectQuery(Artist.class);
    query.addOrdering(Artist.ARTIST_NAME.asc());
    List<?> objects = childContext.performQuery(query);
    assertEquals(4, objects.size());
    Artist childNew = childContext.newObject(Artist.class);
    childNew.setArtistName("NNN");
    Artist childModified = (Artist) objects.get(0);
    childModified.setArtistName("MMM");
    Artist childCommitted = (Artist) objects.get(1);
    // delete AND modify
    Artist childDeleted = (Artist) objects.get(2);
    childContext.deleteObjects(childDeleted);
    childDeleted.setArtistName("DDD");
    Artist childHollow = (Artist) objects.get(3);
    childContext.invalidateObjects(childHollow);
    childContext.commitChanges();
    assertEquals(PersistenceState.COMMITTED, childNew.getPersistenceState());
    assertEquals(PersistenceState.COMMITTED, childModified.getPersistenceState());
    assertEquals(PersistenceState.COMMITTED, childCommitted.getPersistenceState());
    assertEquals(PersistenceState.TRANSIENT, childDeleted.getPersistenceState());
    assertEquals(PersistenceState.HOLLOW, childHollow.getPersistenceState());
    Artist parentNew = (Artist) context.getGraphManager().getNode(childNew.getObjectId());
    Artist parentModified = (Artist) context.getGraphManager().getNode(childModified.getObjectId());
    Artist parentCommitted = (Artist) context.getGraphManager().getNode(childCommitted.getObjectId());
    Artist parentDeleted = (Artist) context.getGraphManager().getNode(childDeleted.getObjectId());
    Artist parentHollow = (Artist) context.getGraphManager().getNode(childHollow.getObjectId());
    assertNotNull(parentNew);
    assertEquals(PersistenceState.COMMITTED, parentNew.getPersistenceState());
    assertEquals("NNN", parentNew.getArtistName());
    assertNotNull(parentModified);
    assertEquals(PersistenceState.COMMITTED, parentModified.getPersistenceState());
    assertEquals("MMM", parentModified.getArtistName());
    assertNull(context.getObjectStore().getChangesByObjectId().get(parentModified.getObjectId()));
    assertNull("Deleted object should not be registered.", parentDeleted);
    assertNotNull(parentCommitted);
    assertEquals(PersistenceState.COMMITTED, parentCommitted.getPersistenceState());
    assertNotNull(parentHollow);
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Artist(org.apache.cayenne.testdo.testmap.Artist) ObjectContext(org.apache.cayenne.ObjectContext) Test(org.junit.Test)

Example 29 with ObjectContext

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

the class NestedDataContextWriteIT method testCommitChangesToParentPropagatedKey.

@Test
public void testCommitChangesToParentPropagatedKey() throws Exception {
    final DataContext context = createDataContext();
    final ObjectContext childContext = runtime.newContext(context);
    final Painting childMaster = childContext.newObject(Painting.class);
    childMaster.setPaintingTitle("Master");
    final PaintingInfo childDetail1 = childContext.newObject(PaintingInfo.class);
    childDetail1.setTextReview("Detail1");
    childDetail1.setPainting(childMaster);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            childContext.commitChangesToParent();
            assertEquals(PersistenceState.COMMITTED, childMaster.getPersistenceState());
            assertEquals(PersistenceState.COMMITTED, childDetail1.getPersistenceState());
            Painting parentMaster = (Painting) context.getGraphManager().getNode(childMaster.getObjectId());
            assertNotNull(parentMaster);
            assertEquals(PersistenceState.NEW, parentMaster.getPersistenceState());
            PaintingInfo parentDetail1 = (PaintingInfo) context.getGraphManager().getNode(childDetail1.getObjectId());
            assertNotNull(parentDetail1);
            assertEquals(PersistenceState.NEW, parentDetail1.getPersistenceState());
            assertSame(parentMaster, parentDetail1.getPainting());
            assertSame(parentDetail1, parentMaster.getToPaintingInfo());
        }
    });
}
Also used : UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) ObjectContext(org.apache.cayenne.ObjectContext) PaintingInfo(org.apache.cayenne.testdo.testmap.PaintingInfo) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 30 with ObjectContext

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

the class NestedDataContextWriteIT method testAddRemove.

@Test
public void testAddRemove() {
    DataContext context = createDataContext();
    ObjectContext child = runtime.newContext(context);
    Artist a = child.newObject(Artist.class);
    a.setArtistName("X");
    child.commitChanges();
    Painting p1 = child.newObject(Painting.class);
    p1.setPaintingTitle("P1");
    a.addToPaintingArray(p1);
    Painting p2 = child.newObject(Painting.class);
    p2.setPaintingTitle("P2");
    a.addToPaintingArray(p2);
    a.removeFromPaintingArray(p2);
    // this causes an error on commit
    child.deleteObjects(p2);
    child.commitChangesToParent();
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) ObjectContext(org.apache.cayenne.ObjectContext) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Aggregations

ObjectContext (org.apache.cayenne.ObjectContext)108 Test (org.junit.Test)99 Artist (org.apache.cayenne.testdo.testmap.Artist)32 UnitTestClosure (org.apache.cayenne.unit.di.UnitTestClosure)14 SelectQuery (org.apache.cayenne.query.SelectQuery)13 Painting (org.apache.cayenne.testdo.testmap.Painting)13 ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)12 ParallelTestContainer (org.apache.cayenne.test.parallel.ParallelTestContainer)10 GraphDiff (org.apache.cayenne.graph.GraphDiff)9 ObjectId (org.apache.cayenne.ObjectId)8 Table2 (org.apache.cayenne.crypto.db.Table2)7 HashMap (java.util.HashMap)6 Persistent (org.apache.cayenne.Persistent)6 QueryResponse (org.apache.cayenne.QueryResponse)6 ClientMtTable2 (org.apache.cayenne.testdo.mt.ClientMtTable2)6 Query (org.apache.cayenne.query.Query)5 CayenneContext (org.apache.cayenne.CayenneContext)4 DataChannel (org.apache.cayenne.DataChannel)4 ObjectContextFactory (org.apache.cayenne.configuration.ObjectContextFactory)4 EntityResolver (org.apache.cayenne.map.EntityResolver)4