use of org.apache.cayenne.query.SelectQuery in project cayenne by apache.
the class MiscTypesIT method testCharArray.
@Test
public void testCharArray() {
ArraysEntity test = context.newObject(ArraysEntity.class);
char[] a = new char[] { 'x', 'y', 'z' };
test.setCharArray(a);
context.commitChanges();
SelectQuery q = new SelectQuery(ArraysEntity.class);
ArraysEntity testRead = (ArraysEntity) context.performQuery(q).get(0);
assertNotNull(testRead.getCharArray());
assertArraysEqual(a, testRead.getCharArray());
test.setCharArray(null);
context.commitChanges();
}
use of org.apache.cayenne.query.SelectQuery in project cayenne by apache.
the class NestedDataContextReadIT method testReadToOneRelationship.
@Test
public void testReadToOneRelationship() throws Exception {
createRelationshipDataSet();
final ObjectContext child = runtime.newContext(context);
// test how different object states appear in the child on select
Painting hollowTargetSrc = Cayenne.objectForPK(context, Painting.class, 33001);
Artist hollowTarget = hollowTargetSrc.getToArtist();
Painting modifiedTargetSrc = Cayenne.objectForPK(context, Painting.class, 33002);
Artist modifiedTarget = modifiedTargetSrc.getToArtist();
modifiedTarget.setArtistName("M1");
final Painting deletedTargetSrc = Cayenne.objectForPK(context, Painting.class, 33003);
Artist deletedTarget = deletedTargetSrc.getToArtist();
deletedTargetSrc.setToArtist(null);
context.deleteObjects(deletedTarget);
Painting committedTargetSrc = Cayenne.objectForPK(context, Painting.class, 33004);
Artist committedTarget = committedTargetSrc.getToArtist();
committedTarget.getArtistName();
final Painting newTargetSrc = Cayenne.objectForPK(context, Painting.class, 33005);
Artist newTarget = context.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<Painting> q = new SelectQuery<>(Painting.class);
q.addOrdering(Painting.PAINTING_TITLE.asc());
final List<Painting> childSources = q.select(child);
assertEquals(5, childSources.size());
queryInterceptor.runWithQueriesBlocked(() -> {
Painting childHollowTargetSrc = childSources.get(0);
assertSame(child, childHollowTargetSrc.getObjectContext());
Artist childHollowTarget = childHollowTargetSrc.getToArtist();
assertNotNull(childHollowTarget);
assertEquals(PersistenceState.HOLLOW, childHollowTarget.getPersistenceState());
assertSame(child, childHollowTarget.getObjectContext());
Artist childModifiedTarget = childSources.get(1).getToArtist();
assertEquals(PersistenceState.COMMITTED, childModifiedTarget.getPersistenceState());
assertSame(child, childModifiedTarget.getObjectContext());
assertEquals("M1", childModifiedTarget.getArtistName());
Painting childDeletedTargetSrc = childSources.get(2);
// make sure we got the right object...
assertEquals(deletedTargetSrc.getObjectId(), childDeletedTargetSrc.getObjectId());
Artist childDeletedTarget = childDeletedTargetSrc.getToArtist();
assertNull(childDeletedTarget);
Artist childCommittedTarget = childSources.get(3).getToArtist();
assertEquals(PersistenceState.COMMITTED, childCommittedTarget.getPersistenceState());
assertSame(child, childCommittedTarget.getObjectContext());
Painting childNewTargetSrc = childSources.get(4);
// make sure we got the right object...
assertEquals(newTargetSrc.getObjectId(), childNewTargetSrc.getObjectId());
Artist childNewTarget = childNewTargetSrc.getToArtist();
assertNotNull(childNewTarget);
assertEquals(PersistenceState.COMMITTED, childNewTarget.getPersistenceState());
assertSame(child, childNewTarget.getObjectContext());
assertEquals("N1", childNewTarget.getArtistName());
});
}
use of org.apache.cayenne.query.SelectQuery in project cayenne by apache.
the class NestedDataContextWriteIT method testCommitChangesToParentDeleted.
@Test
public void testCommitChangesToParentDeleted() throws Exception {
createArtistsDataSet();
DataContext context = createDataContext();
ObjectContext childContext = runtime.newContext(context);
// make sure we fetch in predictable order
SelectQuery query = new SelectQuery(Artist.class);
query.addOrdering(Artist.ARTIST_NAME.asc());
List<?> objects = childContext.performQuery(query);
assertEquals(4, objects.size());
// delete AND modify
Artist childDeleted = (Artist) objects.get(2);
childContext.deleteObjects(childDeleted);
childDeleted.setArtistName("DDD");
// don't block queries - on delete Cayenne may need to resolve delete rules via
// fetch
childContext.commitChangesToParent();
// * all modified child objects must be in committed state now
// * all modifications should be propagated to the parent
// * no actual commit should occur.
assertEquals(PersistenceState.TRANSIENT, childDeleted.getPersistenceState());
Artist parentDeleted = (Artist) context.getGraphManager().getNode(childDeleted.getObjectId());
assertNotNull(parentDeleted);
assertEquals(PersistenceState.DELETED, parentDeleted.getPersistenceState());
assertEquals("DDD", parentDeleted.getArtistName());
}
use of org.apache.cayenne.query.SelectQuery in project cayenne by apache.
the class NestedDataContextWriteIT method testCommitChangesToParent.
@Test
public void testCommitChangesToParent() throws Exception {
createArtistsDataSet();
final DataContext context = createDataContext();
final ObjectContext childContext = runtime.newContext(context);
// make sure we fetch in predictable order
SelectQuery query = new SelectQuery(Artist.class);
query.addOrdering(Artist.ARTIST_NAME.asc());
List<?> objects = childContext.performQuery(query);
assertEquals(4, objects.size());
final Artist childNew = childContext.newObject(Artist.class);
childNew.setArtistName("NNN");
final Artist childModified = (Artist) objects.get(0);
childModified.setArtistName("MMM");
final Artist childCommitted = (Artist) objects.get(1);
final Artist childHollow = (Artist) objects.get(3);
childContext.invalidateObjects(childHollow);
queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {
public void execute() {
childContext.commitChangesToParent();
// * all modified child objects must be in committed state now
// * all modifications should be propagated to the parent
// * no actual commit should occur.
assertEquals(PersistenceState.COMMITTED, childNew.getPersistenceState());
assertEquals(PersistenceState.COMMITTED, childModified.getPersistenceState());
assertEquals(PersistenceState.COMMITTED, childCommitted.getPersistenceState());
assertEquals(PersistenceState.HOLLOW, childHollow.getPersistenceState());
Artist parentNew = (Artist) context.getGraphManager().getNode(childNew.getObjectId());
Artist parentModified = (Artist) context.getGraphManager().getNode(childModified.getObjectId());
Artist parentCommitted = (Artist) context.getGraphManager().getNode(childCommitted.getObjectId());
Artist parentHollow = (Artist) context.getGraphManager().getNode(childHollow.getObjectId());
assertNotNull(parentNew);
assertEquals(PersistenceState.NEW, parentNew.getPersistenceState());
assertEquals("NNN", parentNew.getArtistName());
assertNotNull(parentModified);
assertEquals(PersistenceState.MODIFIED, parentModified.getPersistenceState());
assertEquals("MMM", parentModified.getArtistName());
assertNotNull(context.getObjectStore().getChangesByObjectId().get(parentModified.getObjectId()));
assertNotNull(parentCommitted);
assertEquals(PersistenceState.COMMITTED, parentCommitted.getPersistenceState());
assertNotNull(parentHollow);
// TODO: we can assert that when we figure out how nested "invalidate"
// should
// work
// assertEquals(PersistenceState.HOLLOW,
// parentHollow.getPersistenceState());
}
});
}
use of org.apache.cayenne.query.SelectQuery in project cayenne by apache.
the class NestedDataContextWriteIT method testCommitChanges.
@Test
public void testCommitChanges() throws Exception {
createArtistsDataSet();
DataContext context = createDataContext();
ObjectContext childContext = runtime.newContext(context);
// make sure we fetch in predictable order
SelectQuery query = new SelectQuery(Artist.class);
query.addOrdering(Artist.ARTIST_NAME.asc());
List<?> objects = childContext.performQuery(query);
assertEquals(4, objects.size());
Artist childNew = childContext.newObject(Artist.class);
childNew.setArtistName("NNN");
Artist childModified = (Artist) objects.get(0);
childModified.setArtistName("MMM");
Artist childCommitted = (Artist) objects.get(1);
// delete AND modify
Artist childDeleted = (Artist) objects.get(2);
childContext.deleteObjects(childDeleted);
childDeleted.setArtistName("DDD");
Artist childHollow = (Artist) objects.get(3);
childContext.invalidateObjects(childHollow);
childContext.commitChanges();
assertEquals(PersistenceState.COMMITTED, childNew.getPersistenceState());
assertEquals(PersistenceState.COMMITTED, childModified.getPersistenceState());
assertEquals(PersistenceState.COMMITTED, childCommitted.getPersistenceState());
assertEquals(PersistenceState.TRANSIENT, childDeleted.getPersistenceState());
assertEquals(PersistenceState.HOLLOW, childHollow.getPersistenceState());
Artist parentNew = (Artist) context.getGraphManager().getNode(childNew.getObjectId());
Artist parentModified = (Artist) context.getGraphManager().getNode(childModified.getObjectId());
Artist parentCommitted = (Artist) context.getGraphManager().getNode(childCommitted.getObjectId());
Artist parentDeleted = (Artist) context.getGraphManager().getNode(childDeleted.getObjectId());
Artist parentHollow = (Artist) context.getGraphManager().getNode(childHollow.getObjectId());
assertNotNull(parentNew);
assertEquals(PersistenceState.COMMITTED, parentNew.getPersistenceState());
assertEquals("NNN", parentNew.getArtistName());
assertNotNull(parentModified);
assertEquals(PersistenceState.COMMITTED, parentModified.getPersistenceState());
assertEquals("MMM", parentModified.getArtistName());
assertNull(context.getObjectStore().getChangesByObjectId().get(parentModified.getObjectId()));
assertNull("Deleted object should not be registered.", parentDeleted);
assertNotNull(parentCommitted);
assertEquals(PersistenceState.COMMITTED, parentCommitted.getPersistenceState());
assertNotNull(parentHollow);
}
Aggregations