use of org.apache.cayenne.query.SelectQuery in project cayenne by apache.
the class DataContextProcedureQueryIT method testUpdateNoParam.
@Test
public void testUpdateNoParam() throws Exception {
if (!accessStackAdapter.supportsStoredProcedures()) {
return;
}
// create an artist with painting in the database
createArtist(1000.0);
ProcedureQuery q = new ProcedureQuery(UPDATE_STORED_PROCEDURE_NOPARAM);
// since stored procedure commits its stuff, we must use an explicit
// non-committing transaction
BaseTransaction t = new ExternalTransaction(jdbcEventLogger);
BaseTransaction.bindThreadTransaction(t);
try {
context.performGenericQuery(q);
} finally {
BaseTransaction.bindThreadTransaction(null);
t.commit();
}
// check that price have doubled
SelectQuery select = new SelectQuery(Artist.class);
select.addPrefetch("paintingArray");
List<?> artists = context.performQuery(select);
assertEquals(1, artists.size());
Artist a = (Artist) artists.get(0);
Painting p = a.getPaintingArray().get(0);
assertEquals(2000, p.getEstimatedPrice().intValue());
}
use of org.apache.cayenne.query.SelectQuery in project cayenne by apache.
the class DataContextQueryCachingIT method testLocalCacheDataObjectsRefresh.
@Test
public void testLocalCacheDataObjectsRefresh() throws Exception {
SelectQuery<Artist> select = new SelectQuery<>(Artist.class);
select.setCacheStrategy(QueryCacheStrategy.LOCAL_CACHE);
MockDataNode engine = MockDataNode.interceptNode(domain, getNode());
try {
// first run, no cache yet
List<?> rows1 = mockupDataRows(2);
engine.reset();
engine.addExpectedResult(select, rows1);
List<?> resultRows = context.performQuery(select);
assertEquals(1, engine.getRunCount());
assertEquals(2, resultRows.size());
assertTrue(resultRows.get(0) instanceof DataObject);
QueryMetadata cacheKey = select.getMetaData(context.getEntityResolver());
assertNull(context.getParentDataDomain().getQueryCache().get(cacheKey));
assertEquals(resultRows, context.getQueryCache().get(cacheKey));
// second run, must refresh the cache
List<?> rows2 = mockupDataRows(4);
engine.reset();
engine.addExpectedResult(select, rows2);
select.setCacheStrategy(QueryCacheStrategy.LOCAL_CACHE_REFRESH);
List<?> freshResultRows = context.performQuery(select);
assertEquals(1, engine.getRunCount());
assertEquals(4, freshResultRows.size());
assertTrue(resultRows.get(0) instanceof DataObject);
assertNull(context.getParentDataDomain().getQueryCache().get(cacheKey));
assertEquals(freshResultRows, context.getQueryCache().get(cacheKey));
} finally {
engine.stopInterceptNode();
}
}
use of org.apache.cayenne.query.SelectQuery in project cayenne by apache.
the class DataContextQueryCachingIT method testLocalCacheRefreshObjectsRefresh.
@Test
public void testLocalCacheRefreshObjectsRefresh() throws Exception {
createInsertDataSet();
SelectQuery<Artist> select = new SelectQuery<>(Artist.class);
select.setCacheStrategy(QueryCacheStrategy.LOCAL_CACHE_REFRESH);
// no cache yet...
List<Artist> objects1 = context.performQuery(select);
assertEquals(1, objects1.size());
Artist a1 = objects1.get(0);
assertEquals("aaa", a1.getArtistName());
// cache, but force refresh
createUpdateDataSet1();
List<?> objects2 = context.performQuery(select);
assertEquals(1, objects2.size());
Artist a2 = (Artist) objects2.get(0);
assertSame(a1, a2);
assertEquals("bbb", a2.getArtistName());
}
use of org.apache.cayenne.query.SelectQuery in project cayenne by apache.
the class DataContextRefreshQueryIT method testRefreshCollection.
@Test
public void testRefreshCollection() throws Exception {
createRefreshCollectionDataSet();
SelectQuery<Artist> q = new SelectQuery<>(Artist.class);
q.addOrdering("db:ARTIST_ID", SortOrder.ASCENDING);
List<?> artists = context.performQuery(q);
Artist a1 = (Artist) artists.get(0);
Artist a2 = (Artist) artists.get(1);
assertEquals(2, a1.getPaintingArray().size());
assertEquals(0, a2.getPaintingArray().size());
assertNotNull(context.getParentDataDomain().getSharedSnapshotCache().getCachedSnapshot(a1.getObjectId()));
assertNotNull(context.getParentDataDomain().getSharedSnapshotCache().getCachedSnapshot(a2.getObjectId()));
RefreshQuery refresh = new RefreshQuery(artists);
context.performQuery(refresh);
assertNull(context.getParentDataDomain().getSharedSnapshotCache().getCachedSnapshot(a1.getObjectId()));
assertNull(context.getParentDataDomain().getSharedSnapshotCache().getCachedSnapshot(a2.getObjectId()));
assertEquals(PersistenceState.HOLLOW, a1.getPersistenceState());
assertEquals(PersistenceState.HOLLOW, a2.getPersistenceState());
assertTrue(((ValueHolder) a1.readProperty(Artist.PAINTING_ARRAY.getName())).isFault());
assertTrue(((ValueHolder) a2.readProperty(Artist.PAINTING_ARRAY.getName())).isFault());
}
use of org.apache.cayenne.query.SelectQuery in project cayenne by apache.
the class DataContextRefreshQueryIT method testRefreshQueryResultsLocalCache.
@Test
public void testRefreshQueryResultsLocalCache() 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.LOCAL_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());
}
Aggregations