use of org.apache.cayenne.query.RefreshQuery in project cayenne by apache.
the class CayenneContextMapRelationshipIT method testAddToMany.
@Test
public void testAddToMany() throws Exception {
createTwoMapToManysWithTargetsDataSet();
ObjectId id = new ObjectId("IdMapToMany", IdMapToMany.ID_PK_COLUMN, 1);
ClientIdMapToMany o1 = (ClientIdMapToMany) Cayenne.objectForQuery(context, new ObjectIdQuery(id));
Map<Object, ClientIdMapToManyTarget> targets = o1.getTargets();
assertNotNull(targets);
assertEquals(3, targets.size());
ClientIdMapToManyTarget newTarget = o1.getObjectContext().newObject(ClientIdMapToManyTarget.class);
o1.addToTargets(newTarget);
assertEquals(4, targets.size());
assertSame(o1, newTarget.getMapToMany());
o1.getObjectContext().commitChanges();
o1.getObjectContext().performGenericQuery(new RefreshQuery());
assertEquals(4, o1.getTargets().size());
int newId = Cayenne.intPKForObject(newTarget);
assertSame(newTarget, o1.getTargets().get(newId));
assertEquals(PersistenceState.COMMITTED, o1.getPersistenceState());
assertEquals(PersistenceState.COMMITTED, newTarget.getPersistenceState());
}
use of org.apache.cayenne.query.RefreshQuery in project cayenne by apache.
the class CayenneContextRefreshQueryIT method testRefreshToMany.
@Test
public void testRefreshToMany() throws Exception {
createM1AndTwoM2sDataSet();
ClientMtTable1 a = Cayenne.objectForPK(context, ClientMtTable1.class, 1);
assertEquals(2, a.getTable2Array().size());
delete1M2DataSet();
RefreshQuery refresh = new RefreshQuery(a);
context.performGenericQuery(refresh);
assertEquals(PersistenceState.HOLLOW, a.getPersistenceState());
assertEquals(1, a.getTable2Array().size());
}
use of org.apache.cayenne.query.RefreshQuery in project cayenne by apache.
the class DataContextRefreshQueryIT method testRefreshQueryResultsSharedCache.
@Test
public void testRefreshQueryResultsSharedCache() throws Exception {
createRefreshCollectionDataSet();
Expression qual = Painting.PAINTING_TITLE.eq("P2");
SelectQuery<Painting> q = new SelectQuery<>(Painting.class, qual);
q.addOrdering("db:PAINTING_ID", SortOrder.ASCENDING);
q.setCacheStrategy(QueryCacheStrategy.SHARED_CACHE);
q.setCacheGroup("X");
List<?> paints = context.performQuery(q);
// fetch P1 separately from cached query
Painting p1 = Cayenne.objectForPK(context, Painting.class, 33001);
Painting p2 = (Painting) paints.get(0);
Artist a1 = p2.getToArtist();
assertSame(a1, p1.getToArtist());
assertNotNull(context.getParentDataDomain().getSharedSnapshotCache().getCachedSnapshot(p1.getObjectId()));
assertNotNull(context.getParentDataDomain().getSharedSnapshotCache().getCachedSnapshot(p2.getObjectId()));
createRefreshCollectionToOneUpdateDataSet();
RefreshQuery refresh = new RefreshQuery(q);
context.performQuery(refresh);
assertNotNull(context.getParentDataDomain().getSharedSnapshotCache().getCachedSnapshot(p1.getObjectId()));
// probably refreshed eagerly
assertNotNull(context.getParentDataDomain().getSharedSnapshotCache().getCachedSnapshot(p2.getObjectId()));
assertEquals(PersistenceState.COMMITTED, p1.getPersistenceState());
assertEquals(PersistenceState.COMMITTED, p2.getPersistenceState());
assertSame(a1, p1.getToArtist());
assertNotSame(a1, p2.getToArtist());
assertEquals("c", p1.getToArtist().getArtistName());
assertEquals("b", p2.getToArtist().getArtistName());
}
use of org.apache.cayenne.query.RefreshQuery in project cayenne by apache.
the class DataContextRefreshQueryIT method testRefreshObjectToMany.
@Test
public void testRefreshObjectToMany() throws Exception {
createRefreshObjectToManyDataSet();
Artist a = Cayenne.objectForPK(context, Artist.class, 33001L);
assertEquals(2, a.getPaintingArray().size());
createRefreshObjectToManyUpdateDataSet();
RefreshQuery refresh = new RefreshQuery(a);
context.performQuery(refresh);
assertEquals(PersistenceState.HOLLOW, a.getPersistenceState());
assertEquals(1, a.getPaintingArray().size());
}
use of org.apache.cayenne.query.RefreshQuery in project cayenne by apache.
the class DataContextRefreshQueryIT method testRefreshCollectionToOne.
@Test
public void testRefreshCollectionToOne() throws Exception {
createRefreshCollectionDataSet();
SelectQuery<Painting> q = new SelectQuery<>(Painting.class);
q.addOrdering("db:PAINTING_ID", SortOrder.ASCENDING);
List<?> paints = context.performQuery(q);
Painting p1 = (Painting) paints.get(0);
Painting p2 = (Painting) paints.get(1);
Artist a1 = p1.getToArtist();
assertSame(a1, p2.getToArtist());
assertNotNull(context.getParentDataDomain().getSharedSnapshotCache().getCachedSnapshot(p1.getObjectId()));
assertNotNull(context.getParentDataDomain().getSharedSnapshotCache().getCachedSnapshot(p2.getObjectId()));
createRefreshCollectionToOneUpdateDataSet();
RefreshQuery refresh = new RefreshQuery(paints);
context.performQuery(refresh);
assertNull(context.getParentDataDomain().getSharedSnapshotCache().getCachedSnapshot(p1.getObjectId()));
assertNull(context.getParentDataDomain().getSharedSnapshotCache().getCachedSnapshot(p2.getObjectId()));
assertEquals(PersistenceState.HOLLOW, p1.getPersistenceState());
assertEquals(PersistenceState.HOLLOW, p2.getPersistenceState());
assertNotSame(a1, p1.getToArtist());
assertNotSame(a1, p2.getToArtist());
assertEquals("b", p1.getToArtist().getArtistName());
}
Aggregations