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);
}
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();
}
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);
}
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());
}
});
}
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();
}
Aggregations