public void testReplace() throws Exception {
String altGalleryName = "alt gallery";
Painting p1 = newPainting();
Gallery g1 = newGallery();
g1.setGalleryName(altGalleryName);
p1.setToGallery(g1);
// do save
ctxt.commitChanges();
ctxt = createDataContext();
// test database data
Painting p2 = fetchPainting();
Gallery g21 = p2.getToGallery();
assertNotNull(g21);
assertEquals(altGalleryName, g21.getGalleryName());
assertEquals(1, g21.getPaintingArray().size());
assertSame(p2, g21.getPaintingArray().get(0));
Gallery g22 = newGallery();
// *** TESTING THIS ***
p2.setToGallery(g22);
// test before save
assertEquals(0, g21.getPaintingArray().size());
assertEquals(1, g22.getPaintingArray().size());
assertSame(p2, g22.getPaintingArray().get(0));
// do save II
ctxt.commitChanges();
ctxt = createDataContext();
Painting p3 = fetchPainting();
Gallery g3 = p3.getToGallery();
assertNotNull(g3);
assertEquals(galleryName, g3.getGalleryName());
assertEquals(1, g3.getPaintingArray().size());
assertSame(p3, g3.getPaintingArray().get(0));
}