// Create an artist in the same context.
Artist a = context.newObject(Artist.class);
a.setArtistName("Test Artist");
// Create a painting in the same context.
Painting p = context.newObject(Painting.class);
p.setPaintingTitle("Test Painting");
// Set the painting's gallery.
p.setToGallery(g);
assertEquals(g, p.getToGallery());
// Unregister the painting from the context.
context.unregisterObjects(Collections.singletonList(p));
// Make sure that even though the painting has been removed from the context's
// object graph that the reference to the gallery is the same.
assertEquals(g, p.getToGallery());
// Now, set the relationship between "p" & "a." Since "p" is not registered with a
// context, but "a" is, "p" should be auto-registered with the context of "a."
p.setToArtist(a);
// Now commit the gallery, artist, & painting.
context.commitChanges();
// Check one last time that the painting's gallery is set to what we expect.
assertEquals(g, p.getToGallery());
// Now, retrieve the same painting from the DB. Note that the gallery relationship
// is null even though according to our painting, that should not be the case; a
// NULL
// value has been recorded to the DB for the painting's gallery_id field.
//
// The full object graph is not being re-registered during auto-registration
// with the context.
Painting newP = (Painting) DataObjectUtils.objectForPK(createDataContext(), p
.getObjectId());
assertNotNull(newP.getToGallery());
}