Search in sources :

Example 11 with RefreshQuery

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());
}
Also used : ClientIdMapToManyTarget(org.apache.cayenne.testdo.map_to_many.ClientIdMapToManyTarget) RefreshQuery(org.apache.cayenne.query.RefreshQuery) ClientIdMapToMany(org.apache.cayenne.testdo.map_to_many.ClientIdMapToMany) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Test(org.junit.Test)

Example 12 with RefreshQuery

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

Example 13 with RefreshQuery

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

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

Example 15 with RefreshQuery

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

Aggregations

RefreshQuery (org.apache.cayenne.query.RefreshQuery)24 Test (org.junit.Test)21 SelectQuery (org.apache.cayenne.query.SelectQuery)9 Artist (org.apache.cayenne.testdo.testmap.Artist)9 Painting (org.apache.cayenne.testdo.testmap.Painting)5 Map (java.util.Map)3 Set (java.util.Set)3 Expression (org.apache.cayenne.exp.Expression)3 ObjectIdQuery (org.apache.cayenne.query.ObjectIdQuery)3 Query (org.apache.cayenne.query.Query)3 IdMapToMany (org.apache.cayenne.testdo.map_to_many.IdMapToMany)3 MapToMany (org.apache.cayenne.testdo.map_to_many.MapToMany)3 MapToManyTarget (org.apache.cayenne.testdo.map_to_many.MapToManyTarget)3 CollectionToMany (org.apache.cayenne.testdo.relationships_collection_to_many.CollectionToMany)3 CollectionToManyTarget (org.apache.cayenne.testdo.relationships_collection_to_many.CollectionToManyTarget)3 SetToMany (org.apache.cayenne.testdo.relationships_set_to_many.SetToMany)3 SetToManyTarget (org.apache.cayenne.testdo.relationships_set_to_many.SetToManyTarget)3 Collection (java.util.Collection)2 ClientIdMapToMany (org.apache.cayenne.testdo.map_to_many.ClientIdMapToMany)2 ClientIdMapToManyTarget (org.apache.cayenne.testdo.map_to_many.ClientIdMapToManyTarget)2