use of org.apache.cayenne.query.SelectQuery in project cayenne by apache.
the class DataContextRefreshQueryIT method testRefreshQueryResultGroupLocal.
@Test
public void testRefreshQueryResultGroupLocal() 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();
// results are served from cache and therefore are not refreshed
context.performQuery(q);
assertSame(a1, p1.getToArtist());
assertSame(a1, p2.getToArtist());
assertEquals("c", p1.getToArtist().getArtistName());
assertEquals("c", p2.getToArtist().getArtistName());
RefreshQuery refresh = new RefreshQuery("X");
// this should invalidate results for the next query run
context.performQuery(refresh);
// this should force a refresh
context.performQuery(q);
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.SelectQuery in project cayenne by apache.
the class DataContextRefreshingIT method testRefetchRootWithNullifiedToOne.
@Test
public void testRefetchRootWithNullifiedToOne() throws Exception {
createSingleArtistAndPaintingDataSet();
Painting painting = (Painting) context.performQuery(new SelectQuery(Painting.class)).get(0);
assertNotNull(painting.getToArtist());
assertEquals("artist2", painting.getToArtist().getArtistName());
assertEquals(1, tPainting.update().set("ARTIST_ID", null, Types.BIGINT).execute());
// select without prefetch
painting = (Painting) context.performQuery(new SelectQuery(Painting.class)).get(0);
assertNotNull(painting);
assertNull(painting.getToArtist());
}
use of org.apache.cayenne.query.SelectQuery in project cayenne by apache.
the class DataContextRefreshingIT method testRefetchRootWithAddedToMany.
@Test
public void testRefetchRootWithAddedToMany() throws Exception {
createSingleArtistDataSet();
Artist artist = (Artist) context.performQuery(new SelectQuery(Artist.class)).get(0);
assertEquals(artist.getPaintingArray().size(), 0);
tPainting.insert(5, "p", 5, 1000);
// select without prefetch
SelectQuery query = new SelectQuery(Artist.class);
artist = (Artist) context.performQuery(query).get(0);
assertEquals(artist.getPaintingArray().size(), 0);
// select using relationship prefetching
query.addPrefetch(Artist.PAINTING_ARRAY.disjoint());
artist = (Artist) context.performQuery(query).get(0);
assertEquals(artist.getPaintingArray().size(), 1);
}
use of org.apache.cayenne.query.SelectQuery in project cayenne by apache.
the class DataContextRefreshingIT method testInvalidateRootWithChangedToOneTarget.
@Test
public void testInvalidateRootWithChangedToOneTarget() throws Exception {
createTwoArtistsAndPaintingDataSet();
Painting painting = (Painting) context.performQuery(new SelectQuery(Painting.class)).get(0);
Artist artistBefore = painting.getToArtist();
assertNotNull(artistBefore);
assertEquals("artist2", artistBefore.getArtistName());
assertEquals(1, tPainting.update().set("ARTIST_ID", 6).execute());
context.invalidateObjects(painting);
assertNotSame(artistBefore, painting.getToArtist());
assertEquals("artist3", painting.getToArtist().getArtistName());
}
use of org.apache.cayenne.query.SelectQuery in project cayenne by apache.
the class DataContextRefreshingIT method testRefetchRootWithUpdatedAttributes.
@Test
public void testRefetchRootWithUpdatedAttributes() throws Exception {
createSingleArtistDataSet();
String nameBefore = "artist2";
String nameAfter = "not an artist";
SelectQuery queryBefore = new SelectQuery(Artist.class, ExpressionFactory.matchExp("artistName", nameBefore));
Artist artist = (Artist) context.performQuery(queryBefore).get(0);
assertEquals(nameBefore, artist.getArtistName());
assertEquals(1, tArtist.update().set("ARTIST_NAME", nameAfter).execute());
// fetch into the same context
List<Artist> artists = context.performQuery(queryBefore);
assertEquals(0, artists.size());
SelectQuery queryAfter = new SelectQuery(Artist.class, ExpressionFactory.matchExp("artistName", nameAfter));
artist = (Artist) context.performQuery(queryAfter).get(0);
assertNotNull(artist);
assertEquals(nameAfter, artist.getArtistName());
}
Aggregations