### Question:
ObjectIdRelationshipHandler { public void relate(DataObject from, DataObject to) { if (from.getObjectContext() == null) { throw new IllegalArgumentException("'from' has null ObjectContext"); } String property = objectIdPropertyName(from); String relationship = objectIdRelationshipName(property); if (to != null) { if (to.getObjectContext() == null) { throw new IllegalArgumentException("'to' has null ObjectContext"); } if (from.getObjectContext() != to.getObjectContext()) { throw new IllegalArgumentException( "'from' and 'to' objects are registered in different ObjectContexts"); } from.writePropertyDirectly(relationship, to); if (to.getObjectId().isTemporary() && !to.getObjectId().isReplacementIdAttached()) { from.writeProperty(property, new ObjectIdPropagatedValueFactory( referenceableHandler, to)); } else { String uuid = referenceableHandler.getStringId(to); from.writeProperty(property, uuid); } } else { from.writeProperty(property, null); from.writePropertyDirectly(relationship, null); } } ObjectIdRelationshipHandler(IdCoder referenceableHandler); String objectIdRelationshipName(String uuidPropertyName); String objectIdPropertyName(DataObject object); void relate(DataObject from, DataObject to);  }### Answer:
@Test public void testRelate_Existing() throws Exception { e1Table.insert(1); ObjectContext context = runtime.newContext(); E1 e1 = (E1) Cayenne.objectForQuery(context, ObjectSelect.query(E1.class)); UuidRoot1 r1 = context.newObject(UuidRoot1.class); IdCoder refHandler = new IdCoder(context.getEntityResolver()); ObjectIdRelationshipHandler handler = new ObjectIdRelationshipHandler(refHandler); handler.relate(r1, e1); assertEquals("E1:1", r1.getUuid()); assertSame(e1, r1.readPropertyDirectly("cay:related:uuid")); context.commitChanges(); Object[] r1x = rootTable.select(); assertEquals("E1:1", r1x[1]); }
@Test public void testRelate_New() throws Exception { ObjectContext context = runtime.newContext(); E1 e1 = context.newObject(E1.class); UuidRoot1 r1 = context.newObject(UuidRoot1.class); IdCoder refHandler = new IdCoder(context .getEntityResolver()); ObjectIdRelationshipHandler handler = new ObjectIdRelationshipHandler(refHandler); handler.relate(r1, e1); assertSame(e1, r1.readPropertyDirectly("cay:related:uuid")); context.commitChanges(); int id = Cayenne.intPKForObject(e1); Object[] r1x = rootTable.select(); assertEquals("E1:" + id, r1x[1]); assertEquals("E1:" + id, r1.getUuid()); }
@Test public void testRelate_Change() throws Exception { e1Table.insert(1); rootTable.insert(1, "E1:1"); ObjectContext context = runtime.newContext(); UuidRoot1 r1 = Cayenne.objectForPK(context, UuidRoot1.class, 1); assertEquals("E1:1", r1.getUuid()); E1 e1 = context.newObject(E1.class); IdCoder refHandler = new IdCoder(context .getEntityResolver()); ObjectIdRelationshipHandler handler = new ObjectIdRelationshipHandler(refHandler); handler.relate(r1, e1); assertSame(e1, r1.readPropertyDirectly("cay:related:uuid")); context.commitChanges(); int id = Cayenne.intPKForObject(e1); assertNotEquals(1, id); Object[] r1x = rootTable.select(); assertEquals("E1:" + id, r1x[1]); assertEquals("E1:" + id, r1.getUuid()); assertSame(e1, r1.readProperty("cay:related:uuid")); }