Search in sources :

Example 91 with SelectQuery

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());
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Artist(org.apache.cayenne.testdo.testmap.Artist) Expression(org.apache.cayenne.exp.Expression) RefreshQuery(org.apache.cayenne.query.RefreshQuery) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 92 with SelectQuery

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());
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 93 with SelectQuery

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);
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) SelectQuery(org.apache.cayenne.query.SelectQuery) Test(org.junit.Test)

Example 94 with SelectQuery

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());
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Artist(org.apache.cayenne.testdo.testmap.Artist) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 95 with SelectQuery

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());
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Artist(org.apache.cayenne.testdo.testmap.Artist) Test(org.junit.Test)

Aggregations

SelectQuery (org.apache.cayenne.query.SelectQuery)360 Test (org.junit.Test)348 Artist (org.apache.cayenne.testdo.testmap.Artist)128 Painting (org.apache.cayenne.testdo.testmap.Painting)75 UnitTestClosure (org.apache.cayenne.unit.di.UnitTestClosure)64 Expression (org.apache.cayenne.exp.Expression)47 List (java.util.List)43 ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)26 ValueHolder (org.apache.cayenne.ValueHolder)21 ReturnTypesMap1 (org.apache.cayenne.testdo.return_types.ReturnTypesMap1)18 ArrayList (java.util.ArrayList)16 Date (java.util.Date)13 DataRow (org.apache.cayenne.DataRow)12 ObjectContext (org.apache.cayenne.ObjectContext)12 DbEntity (org.apache.cayenne.map.DbEntity)12 SQLTemplate (org.apache.cayenne.query.SQLTemplate)11 ROArtist (org.apache.cayenne.testdo.testmap.ROArtist)11 HashMap (java.util.HashMap)10 ObjectId (org.apache.cayenne.ObjectId)10 Query (org.apache.cayenne.query.Query)10