Search in sources :

Example 16 with RefreshQuery

use of org.apache.cayenne.query.RefreshQuery in project cayenne by apache.

the class DataContextRefreshQueryIT method testRefreshSingleObject.

@Test
public void testRefreshSingleObject() 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);
    assertEquals(2, a1.getPaintingArray().size());
    assertNotNull(context.getParentDataDomain().getSharedSnapshotCache().getCachedSnapshot(a1.getObjectId()));
    RefreshQuery refresh = new RefreshQuery(a1);
    context.performQuery(refresh);
    assertNull(context.getParentDataDomain().getSharedSnapshotCache().getCachedSnapshot(a1.getObjectId()));
    assertEquals(PersistenceState.HOLLOW, a1.getPersistenceState());
    assertTrue(((ValueHolder) a1.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 17 with RefreshQuery

use of org.apache.cayenne.query.RefreshQuery in project cayenne by apache.

the class DataContextRefreshQueryIT method testRefreshAll.

@Test
public void testRefreshAll() 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);
    Painting p1 = a1.getPaintingArray().get(0);
    Painting p2 = a1.getPaintingArray().get(0);
    assertNotNull(context.getParentDataDomain().getSharedSnapshotCache().getCachedSnapshot(a1.getObjectId()));
    assertNotNull(context.getParentDataDomain().getSharedSnapshotCache().getCachedSnapshot(a2.getObjectId()));
    assertNotNull(context.getParentDataDomain().getSharedSnapshotCache().getCachedSnapshot(p1.getObjectId()));
    assertNotNull(context.getParentDataDomain().getSharedSnapshotCache().getCachedSnapshot(p2.getObjectId()));
    RefreshQuery refresh = new RefreshQuery();
    context.performQuery(refresh);
    assertNull(context.getParentDataDomain().getSharedSnapshotCache().getCachedSnapshot(a1.getObjectId()));
    assertNull(context.getParentDataDomain().getSharedSnapshotCache().getCachedSnapshot(a2.getObjectId()));
    assertNull(context.getParentDataDomain().getSharedSnapshotCache().getCachedSnapshot(p1.getObjectId()));
    assertNull(context.getParentDataDomain().getSharedSnapshotCache().getCachedSnapshot(p2.getObjectId()));
    assertEquals(PersistenceState.HOLLOW, a1.getPersistenceState());
    assertEquals(PersistenceState.HOLLOW, a2.getPersistenceState());
    assertEquals(PersistenceState.HOLLOW, p1.getPersistenceState());
    assertEquals(PersistenceState.HOLLOW, p2.getPersistenceState());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) SelectQuery(org.apache.cayenne.query.SelectQuery) RefreshQuery(org.apache.cayenne.query.RefreshQuery) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 18 with RefreshQuery

use of org.apache.cayenne.query.RefreshQuery in project cayenne by apache.

the class DataDomainCallbacksIT method testPostLoad.

@Test
public void testPostLoad() throws Exception {
    LifecycleCallbackRegistry registry = resolver.getCallbackRegistry();
    registry.addCallback(LifecycleEvent.POST_LOAD, Artist.class, "postLoadCallback");
    MockCallingBackListener listener = new MockCallingBackListener();
    registry.addListener(LifecycleEvent.POST_LOAD, Artist.class, listener, "publicCallback");
    Artist a1 = context.newObject(Artist.class);
    a1.setArtistName("XX");
    context.commitChanges();
    assertEquals(0, a1.getPostLoaded());
    assertNull(listener.getPublicCalledbackEntity());
    SelectQuery q = new SelectQuery(Artist.class);
    context.performQuery(q);
    assertEquals(1, a1.getPostLoaded());
    assertSame(a1, listener.getPublicCalledbackEntity());
    a1.resetCallbackFlags();
    listener.reset();
    // post load must be called on rollback...
    a1.resetCallbackFlags();
    listener.reset();
    context.rollbackChanges();
    assertEquals(0, a1.getPostLoaded());
    assertNull(listener.getPublicCalledbackEntity());
    // now change and rollback the artist - postLoad must be called
    a1.setArtistName("YY");
    context.rollbackChanges();
    assertEquals(1, a1.getPostLoaded());
    assertSame(a1, listener.getPublicCalledbackEntity());
    // test invalidated
    a1.resetCallbackFlags();
    listener.reset();
    assertEquals(0, a1.getPostLoaded());
    assertNull(listener.getPublicCalledbackEntity());
    context.performQuery(new RefreshQuery(a1));
    assertEquals(0, a1.getPostLoaded());
    assertNull(listener.getPublicCalledbackEntity());
    a1.getArtistName();
    assertEquals(1, a1.getPostLoaded());
    assertSame(a1, listener.getPublicCalledbackEntity());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) SelectQuery(org.apache.cayenne.query.SelectQuery) RefreshQuery(org.apache.cayenne.query.RefreshQuery) LifecycleCallbackRegistry(org.apache.cayenne.reflect.LifecycleCallbackRegistry) Test(org.junit.Test)

Example 19 with RefreshQuery

use of org.apache.cayenne.query.RefreshQuery in project cayenne by apache.

the class CDOSetRelationshipIT method testRemoveToMany.

@Test
public void testRemoveToMany() throws Exception {
    createTestDataSet();
    SetToMany o1 = Cayenne.objectForPK(context, SetToMany.class, 1);
    Set targets = o1.getTargets();
    assertEquals(3, targets.size());
    SetToManyTarget target = Cayenne.objectForPK(o1.getObjectContext(), SetToManyTarget.class, 2);
    o1.removeFromTargets(target);
    assertEquals(2, targets.size());
    assertFalse(o1.getTargets().contains(target));
    assertNull(target.getSetToMany());
    o1.getObjectContext().commitChanges();
    o1.getObjectContext().performGenericQuery(new RefreshQuery());
    assertEquals(2, o1.getTargets().size());
    assertFalse(o1.getTargets().contains(target));
}
Also used : Set(java.util.Set) RefreshQuery(org.apache.cayenne.query.RefreshQuery) SetToMany(org.apache.cayenne.testdo.relationships_set_to_many.SetToMany) SetToManyTarget(org.apache.cayenne.testdo.relationships_set_to_many.SetToManyTarget) Test(org.junit.Test)

Example 20 with RefreshQuery

use of org.apache.cayenne.query.RefreshQuery in project cayenne by apache.

the class CDOSetRelationshipIT method testAddToManyViaReverse.

@Test
public void testAddToManyViaReverse() throws Exception {
    createTestDataSet();
    SetToMany o1 = Cayenne.objectForPK(context, SetToMany.class, 1);
    Set targets = o1.getTargets();
    assertNotNull(targets);
    assertEquals(3, targets.size());
    SetToManyTarget newTarget = o1.getObjectContext().newObject(SetToManyTarget.class);
    newTarget.setSetToMany(o1);
    assertEquals(4, targets.size());
    assertTrue(o1.getTargets().contains(newTarget));
    assertSame(o1, newTarget.getSetToMany());
    o1.getObjectContext().commitChanges();
    o1.getObjectContext().performGenericQuery(new RefreshQuery());
    assertEquals(4, o1.getTargets().size());
}
Also used : Set(java.util.Set) RefreshQuery(org.apache.cayenne.query.RefreshQuery) SetToMany(org.apache.cayenne.testdo.relationships_set_to_many.SetToMany) SetToManyTarget(org.apache.cayenne.testdo.relationships_set_to_many.SetToManyTarget) 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