public void testReadToOneRelationship() throws Exception {
deleteTestData();
createTestData("testReadRelationship");
DataContext parent = createDataContext();
ObjectContext child = parent.createChildContext();
// test how different object states appear in the child on select
int hollowTargetSrcId = 33001;
int modifiedTargetSrcId = 33002;
int deletedTargetSrcId = 33003;
int committedTargetSrcId = 33004;
int newTargetSrcId = 33005;
Painting hollowTargetSrc = Cayenne.objectForPK(
parent,
Painting.class,
hollowTargetSrcId);
Artist hollowTarget = hollowTargetSrc.getToArtist();
Painting modifiedTargetSrc = Cayenne.objectForPK(
parent,
Painting.class,
modifiedTargetSrcId);
Artist modifiedTarget = modifiedTargetSrc.getToArtist();
modifiedTarget.setArtistName("M1");
Painting deletedTargetSrc = Cayenne.objectForPK(
parent,
Painting.class,
deletedTargetSrcId);
Artist deletedTarget = deletedTargetSrc.getToArtist();
deletedTargetSrc.setToArtist(null);
parent.deleteObject(deletedTarget);
Painting committedTargetSrc = Cayenne.objectForPK(
parent,
Painting.class,
committedTargetSrcId);
Artist committedTarget = committedTargetSrc.getToArtist();
committedTarget.getArtistName();
Painting newTargetSrc = Cayenne.objectForPK(
parent,
Painting.class,
newTargetSrcId);
Artist newTarget = parent.newObject(Artist.class);
newTarget.setArtistName("N1");
newTargetSrc.setToArtist(newTarget);
assertEquals(PersistenceState.COMMITTED, hollowTargetSrc.getPersistenceState());
assertEquals(PersistenceState.COMMITTED, modifiedTargetSrc.getPersistenceState());
assertEquals(PersistenceState.MODIFIED, deletedTargetSrc.getPersistenceState());
assertEquals(PersistenceState.COMMITTED, committedTargetSrc.getPersistenceState());
assertEquals(PersistenceState.MODIFIED, newTargetSrc.getPersistenceState());
assertEquals(PersistenceState.HOLLOW, hollowTarget.getPersistenceState());
assertEquals(PersistenceState.MODIFIED, modifiedTarget.getPersistenceState());
assertEquals(PersistenceState.DELETED, deletedTarget.getPersistenceState());
assertEquals(PersistenceState.COMMITTED, committedTarget.getPersistenceState());
assertEquals(PersistenceState.NEW, newTarget.getPersistenceState());
// run an ordered query, so we can address specific objects directly by index
SelectQuery q = new SelectQuery(Painting.class);
q.addOrdering(Painting.PAINTING_TITLE_PROPERTY, SortOrder.ASCENDING);
List childSources = child.performQuery(q);
assertEquals(5, childSources.size());
blockQueries();
try {
Painting childHollowTargetSrc = (Painting) childSources.get(0);