Search in sources :

Example 86 with SelectQuery

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

Example 87 with SelectQuery

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

Example 88 with SelectQuery

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

Example 89 with SelectQuery

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

Example 90 with SelectQuery

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());
}
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)

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