Search in sources :

Example 76 with ObjectContext

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

the class NestedDataContextRollbackIT method testRollbackChangesLocally.

@Test
public void testRollbackChangesLocally() {
    ObjectContext child1 = runtime.newContext(context);
    assertFalse(context.hasChanges());
    assertFalse(child1.hasChanges());
    context.newObject(Artist.class);
    child1.newObject(Artist.class);
    assertTrue(context.hasChanges());
    assertTrue(child1.hasChanges());
    child1.rollbackChangesLocally();
    assertTrue(context.hasChanges());
    assertFalse(child1.hasChanges());
}
Also used : ObjectContext(org.apache.cayenne.ObjectContext) Test(org.junit.Test)

Example 77 with ObjectContext

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

the class NestedDataContextWriteIT method testCommitChangesToParent_MergeProperties.

@Test
public void testCommitChangesToParent_MergeProperties() throws Exception {
    createMixedDataSet();
    final DataContext context = createDataContext();
    final ObjectContext childContext = runtime.newContext(context);
    // make sure we fetch in predictable order
    List<Painting> objects = ObjectSelect.query(Painting.class).orderBy(Painting.PAINTING_TITLE.asc()).select(childContext);
    assertEquals(6, objects.size());
    final Painting childModifiedSimple = objects.get(0);
    childModifiedSimple.setPaintingTitle("C_PT");
    final Painting childModifiedToOne = objects.get(1);
    childModifiedToOne.setToArtist(childModifiedSimple.getToArtist());
    final Artist childModifiedToMany = objects.get(2).getToArtist();
    // ensure painting array is fully resolved...
    childModifiedToMany.getPaintingArray().size();
    childModifiedToMany.addToPaintingArray(objects.get(3));
    queryInterceptor.runWithQueriesBlocked(() -> {
        Painting parentModifiedSimple;
        Artist parentModifiedToMany;
        childContext.commitChangesToParent();
        assertEquals(PersistenceState.COMMITTED, childModifiedSimple.getPersistenceState());
        assertEquals(PersistenceState.COMMITTED, childModifiedToOne.getPersistenceState());
        assertEquals(PersistenceState.COMMITTED, childModifiedToMany.getPersistenceState());
        parentModifiedSimple = (Painting) context.getGraphManager().getNode(childModifiedSimple.getObjectId());
        Painting parentModifiedToOne = (Painting) context.getGraphManager().getNode(childModifiedToOne.getObjectId());
        parentModifiedToMany = (Artist) context.getGraphManager().getNode(childModifiedToMany.getObjectId());
        assertNotNull(parentModifiedSimple);
        assertEquals(PersistenceState.MODIFIED, parentModifiedSimple.getPersistenceState());
        assertEquals("C_PT", parentModifiedSimple.getPaintingTitle());
        assertNotNull(context.getObjectStore().getChangesByObjectId().get(parentModifiedSimple.getObjectId()));
        assertNotNull(parentModifiedToOne);
        assertEquals(PersistenceState.MODIFIED, parentModifiedToOne.getPersistenceState());
        assertNotNull(parentModifiedToOne.getToArtist());
        assertEquals(33001, Cayenne.intPKForObject(parentModifiedToOne.getToArtist()));
        assertNotNull(context.getObjectStore().getChangesByObjectId().get(parentModifiedToOne.getObjectId()));
        // indirectly modified....
        assertNotNull(parentModifiedToMany);
        assertEquals(PersistenceState.MODIFIED, parentModifiedToMany.getPersistenceState());
        // here query is expected, as the parent was hollow and its to-many
        // relationship
        // is unresolved
        List<?> paintings = parentModifiedToMany.getPaintingArray();
        assertEquals(2, paintings.size());
    });
}
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 78 with ObjectContext

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

the class NestedDataContextWriteIT method testCommitChangesToParentFlattenedMultipleFlush.

@Test
public void testCommitChangesToParentFlattenedMultipleFlush() {
    final DataContext context = createDataContext();
    final ObjectContext childContext = runtime.newContext(context);
    final Artist childO1 = childContext.newObject(Artist.class);
    childO1.setArtistName("o1");
    final ArtGroup childO2 = childContext.newObject(ArtGroup.class);
    childO2.setName("o2");
    childO2.addToArtistArray(childO1);
    childContext.commitChangesToParent();
    final ArtGroup childO3 = childContext.newObject(ArtGroup.class);
    childO3.setName("o3");
    childO1.addToGroupArray(childO3);
    assertEquals(2, childO1.getGroupArray().size());
    assertEquals(1, childO2.getArtistArray().size());
    assertEquals(1, childO3.getArtistArray().size());
    queryInterceptor.runWithQueriesBlocked(() -> {
        childContext.commitChangesToParent();
        assertEquals(PersistenceState.COMMITTED, childO1.getPersistenceState());
        assertEquals(PersistenceState.COMMITTED, childO2.getPersistenceState());
        assertEquals(PersistenceState.COMMITTED, childO3.getPersistenceState());
        Artist parentO1 = (Artist) context.getGraphManager().getNode(childO1.getObjectId());
        assertNotNull(parentO1);
        assertEquals(PersistenceState.NEW, parentO1.getPersistenceState());
        ArtGroup parentO2 = (ArtGroup) context.getGraphManager().getNode(childO2.getObjectId());
        assertNotNull(parentO2);
        assertEquals(PersistenceState.NEW, parentO2.getPersistenceState());
        ArtGroup parentO3 = (ArtGroup) context.getGraphManager().getNode(childO3.getObjectId());
        assertNotNull(parentO3);
        assertEquals(PersistenceState.NEW, parentO3.getPersistenceState());
        assertEquals(2, parentO1.getGroupArray().size());
        assertEquals(1, parentO2.getArtistArray().size());
        assertEquals(1, parentO3.getArtistArray().size());
        assertTrue(parentO2.getArtistArray().contains(parentO1));
        assertTrue(parentO3.getArtistArray().contains(parentO1));
        assertTrue(parentO1.getGroupArray().contains(parentO2));
        assertTrue(parentO1.getGroupArray().contains(parentO3));
    });
    childO1.removeFromGroupArray(childO2);
    queryInterceptor.runWithQueriesBlocked(() -> {
        childContext.commitChangesToParent();
        assertEquals(PersistenceState.COMMITTED, childO1.getPersistenceState());
        assertEquals(PersistenceState.COMMITTED, childO2.getPersistenceState());
        assertEquals(PersistenceState.COMMITTED, childO3.getPersistenceState());
        Artist parentO1 = (Artist) context.getGraphManager().getNode(childO1.getObjectId());
        assertNotNull(parentO1);
        assertEquals(PersistenceState.NEW, parentO1.getPersistenceState());
        ArtGroup parentO2 = (ArtGroup) context.getGraphManager().getNode(childO2.getObjectId());
        assertNotNull(parentO2);
        assertEquals(PersistenceState.NEW, parentO2.getPersistenceState());
        ArtGroup parentO3 = (ArtGroup) context.getGraphManager().getNode(childO3.getObjectId());
        assertNotNull(parentO3);
        assertEquals(PersistenceState.NEW, parentO3.getPersistenceState());
        assertEquals(1, parentO1.getGroupArray().size());
        assertEquals(0, parentO2.getArtistArray().size());
        assertEquals(1, parentO3.getArtistArray().size());
        assertTrue(parentO3.getArtistArray().contains(parentO1));
        assertTrue(parentO1.getGroupArray().contains(parentO3));
    });
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) ObjectContext(org.apache.cayenne.ObjectContext) ArtGroup(org.apache.cayenne.testdo.testmap.ArtGroup) Test(org.junit.Test)

Example 79 with ObjectContext

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

the class NestedDataContextWriteIT method testTwoStageCommit.

@Test
@Ignore("Waiting for a fix")
public void testTwoStageCommit() {
    DataContext parent = createDataContext();
    ObjectContext child = runtime.newContext(parent);
    Painting painting = child.newObject(Painting.class);
    painting.setPaintingTitle("222");
    child.commitChangesToParent();
    parent.commitChanges();
    assertTrue(!painting.getObjectId().isTemporary());
}
Also used : ObjectContext(org.apache.cayenne.ObjectContext) Painting(org.apache.cayenne.testdo.testmap.Painting) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 80 with ObjectContext

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

the class VerticalInheritanceMultipleAttributesIT method testPartialCreateObjectsWithUpdate.

@Test
public void testPartialCreateObjectsWithUpdate() throws SQLException {
    ivOtherTable.insert(1, "other1");
    ivOtherTable.insert(2, "other2");
    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);
    IvImpl impl1 = context.newObject(IvImpl.class);
    impl1.setName("name");
    impl1.setAttr1("attr1");
    IvImpl impl2 = context.newObject(IvImpl.class);
    impl2.setName("name");
    impl2.setAttr1("attr1");
    context.commitChanges();
    ObjectContext cleanContext = runtime.newContext();
    List<IvImpl> implResult = ObjectSelect.query(IvImpl.class).select(cleanContext);
    assertEquals(2, implResult.size());
    for (IvImpl record : implResult) {
        assertEquals("name", record.getName());
        assertEquals("attr1", record.getAttr1());
        assertNull(record.getAttr2());
        assertNull(record.getOther1());
        assertNull(record.getOther2());
    }
    impl1.setAttr1("attr1");
    impl1.setAttr2("attr2");
    impl1.setOther1(other1);
    impl1.setOther2(other2);
    impl2.setAttr1("attr1");
    impl2.setAttr2("attr2");
    impl2.setOther1(other1);
    impl2.setOther2(other2);
    context.commitChanges();
    cleanContext = runtime.newContext();
    implResult = ObjectSelect.query(IvImpl.class).select(cleanContext);
    assertEquals(2, implResult.size());
    for (IvImpl record : implResult) {
        assertEquals("name", record.getName());
        assertEquals("attr1", record.getAttr1());
        assertEquals("attr2", record.getAttr2());
        assertEquals(other1.getObjectId(), record.getOther1().getObjectId());
        assertEquals(other2.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)

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