}
public void testAddToFlattenedRelationship() throws Exception {
createTestData("testAddToFlattenedRelationship");
DataContext context = createDataContext();
Artist a1 = Cayenne.objectForPK(context, Artist.class, 33001);
assertEquals(0, a1.getGroupArray().size());
SelectQuery q = new SelectQuery(ArtGroup.class, ExpressionFactory.matchExp(
"name",
"g1"));
List results = context.performQuery(q);
assertEquals(1, results.size());
assertFalse(context.hasChanges());
ArtGroup group = (ArtGroup) results.get(0);
a1.addToGroupArray(group);
assertTrue(context.hasChanges());
List groupList = a1.getGroupArray();
assertEquals(1, groupList.size());
assertEquals("g1", ((ArtGroup) groupList.get(0)).getName());
// Ensure that the commit doesn't fail
a1.getObjectContext().commitChanges();
// and check again
assertFalse(context.hasChanges());
// refetch artist with a different context
context = createDataContext();
a1 = Cayenne.objectForPK(context, Artist.class, 33001);
groupList = a1.getGroupArray();