Search in sources :

Example 11 with Painting

use of org.apache.cayenne.testdo.testmap.Painting in project cayenne by apache.

the class JointPrefetchIT method testJointPrefetch_ToOne_FetchLimit.

@Test
public void testJointPrefetch_ToOne_FetchLimit() throws Exception {
    createJointPrefetchDataSet();
    SelectQuery<Painting> q = new SelectQuery<>(Painting.class);
    q.setFetchLimit(2);
    q.setFetchOffset(0);
    q.addOrdering("db:PAINTING_ID", SortOrder.ASCENDING);
    q.addPrefetch(Painting.TO_ARTIST.joint());
    final List<Painting> objects = q.select(context);
    queryInterceptor.runWithQueriesBlocked(() -> {
        assertEquals(2, objects.size());
        for (Painting p : objects) {
            Artist target = p.getToArtist();
            assertNotNull(target);
            assertEquals(PersistenceState.COMMITTED, target.getPersistenceState());
        }
    });
}
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 12 with Painting

use of org.apache.cayenne.testdo.testmap.Painting in project cayenne by apache.

the class NestedDataContextPeerEventsIT method testPeerObjectUpdatedToManyRelationship.

@Test
public void testPeerObjectUpdatedToManyRelationship() throws Exception {
    Artist a = context.newObject(Artist.class);
    a.setArtistName("X");
    Painting px = context.newObject(Painting.class);
    px.setToArtist(a);
    px.setPaintingTitle("PX");
    Painting py = context.newObject(Painting.class);
    py.setPaintingTitle("PY");
    context.commitChanges();
    // pause to let the context events propagate
    Thread.sleep(500);
    ObjectContext peer1 = runtime.newContext(context);
    Painting py1 = peer1.localObject(py);
    Artist a1 = peer1.localObject(a);
    final ObjectContext peer2 = runtime.newContext(context);
    final Painting py2 = peer2.localObject(py);
    final Artist a2 = peer2.localObject(a);
    a1.addToPaintingArray(py1);
    assertEquals(1, a2.getPaintingArray().size());
    assertFalse(a2.getPaintingArray().contains(py2));
    peer1.commitChangesToParent();
    // pause to let the context events propagate
    new ParallelTestContainer() {

        @Override
        protected void assertResult() throws Exception {
            assertEquals(2, a2.getPaintingArray().size());
            assertTrue(a2.getPaintingArray().contains(py2));
            assertFalse("Peer data context became dirty on event processing", peer2.hasChanges());
        }
    }.runTest(2000);
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) ObjectContext(org.apache.cayenne.ObjectContext) ParallelTestContainer(org.apache.cayenne.test.parallel.ParallelTestContainer) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 13 with Painting

use of org.apache.cayenne.testdo.testmap.Painting in project cayenne by apache.

the class NestedDataContextReadIT method testReadToOneRelationship.

@Test
public void testReadToOneRelationship() throws Exception {
    createRelationshipDataSet();
    final ObjectContext child = runtime.newContext(context);
    // test how different object states appear in the child on select
    Painting hollowTargetSrc = Cayenne.objectForPK(context, Painting.class, 33001);
    Artist hollowTarget = hollowTargetSrc.getToArtist();
    Painting modifiedTargetSrc = Cayenne.objectForPK(context, Painting.class, 33002);
    Artist modifiedTarget = modifiedTargetSrc.getToArtist();
    modifiedTarget.setArtistName("M1");
    final Painting deletedTargetSrc = Cayenne.objectForPK(context, Painting.class, 33003);
    Artist deletedTarget = deletedTargetSrc.getToArtist();
    deletedTargetSrc.setToArtist(null);
    context.deleteObjects(deletedTarget);
    Painting committedTargetSrc = Cayenne.objectForPK(context, Painting.class, 33004);
    Artist committedTarget = committedTargetSrc.getToArtist();
    committedTarget.getArtistName();
    final Painting newTargetSrc = Cayenne.objectForPK(context, Painting.class, 33005);
    Artist newTarget = context.newObject(Artist.class);
    newTarget.setArtistName("N1");
    newTargetSrc.setToArtist(newTarget);
    assertEquals(PersistenceState.COMMITTED, hollowTargetSrc.getPersistenceState());
    assertEquals(PersistenceState.COMMITTED, modifiedTargetSrc.getPersistenceState());
    assertEquals(PersistenceState.MODIFIED, deletedTargetSrc.getPersistenceState());
    assertEquals(PersistenceState.COMMITTED, committedTargetSrc.getPersistenceState());
    assertEquals(PersistenceState.MODIFIED, newTargetSrc.getPersistenceState());
    assertEquals(PersistenceState.HOLLOW, hollowTarget.getPersistenceState());
    assertEquals(PersistenceState.MODIFIED, modifiedTarget.getPersistenceState());
    assertEquals(PersistenceState.DELETED, deletedTarget.getPersistenceState());
    assertEquals(PersistenceState.COMMITTED, committedTarget.getPersistenceState());
    assertEquals(PersistenceState.NEW, newTarget.getPersistenceState());
    // run an ordered query, so we can address specific objects directly by index
    SelectQuery<Painting> q = new SelectQuery<>(Painting.class);
    q.addOrdering(Painting.PAINTING_TITLE.asc());
    final List<Painting> childSources = q.select(child);
    assertEquals(5, childSources.size());
    queryInterceptor.runWithQueriesBlocked(() -> {
        Painting childHollowTargetSrc = childSources.get(0);
        assertSame(child, childHollowTargetSrc.getObjectContext());
        Artist childHollowTarget = childHollowTargetSrc.getToArtist();
        assertNotNull(childHollowTarget);
        assertEquals(PersistenceState.HOLLOW, childHollowTarget.getPersistenceState());
        assertSame(child, childHollowTarget.getObjectContext());
        Artist childModifiedTarget = childSources.get(1).getToArtist();
        assertEquals(PersistenceState.COMMITTED, childModifiedTarget.getPersistenceState());
        assertSame(child, childModifiedTarget.getObjectContext());
        assertEquals("M1", childModifiedTarget.getArtistName());
        Painting childDeletedTargetSrc = childSources.get(2);
        // make sure we got the right object...
        assertEquals(deletedTargetSrc.getObjectId(), childDeletedTargetSrc.getObjectId());
        Artist childDeletedTarget = childDeletedTargetSrc.getToArtist();
        assertNull(childDeletedTarget);
        Artist childCommittedTarget = childSources.get(3).getToArtist();
        assertEquals(PersistenceState.COMMITTED, childCommittedTarget.getPersistenceState());
        assertSame(child, childCommittedTarget.getObjectContext());
        Painting childNewTargetSrc = childSources.get(4);
        // make sure we got the right object...
        assertEquals(newTargetSrc.getObjectId(), childNewTargetSrc.getObjectId());
        Artist childNewTarget = childNewTargetSrc.getToArtist();
        assertNotNull(childNewTarget);
        assertEquals(PersistenceState.COMMITTED, childNewTarget.getPersistenceState());
        assertSame(child, childNewTarget.getObjectContext());
        assertEquals("N1", childNewTarget.getArtistName());
    });
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) SelectQuery(org.apache.cayenne.query.SelectQuery) ObjectContext(org.apache.cayenne.ObjectContext) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 14 with Painting

use of org.apache.cayenne.testdo.testmap.Painting in project cayenne by apache.

the class NestedDataContextWriteIT method testCAY1194.

@Test
public void testCAY1194() throws Exception {
    DataContext context = createDataContext();
    Artist artist = context.newObject(Artist.class);
    artist.setArtistName("111");
    ObjectContext child = runtime.newContext(context);
    Painting painting = child.newObject(Painting.class);
    painting.setPaintingTitle("222");
    Artist localParentMt = child.localObject(artist);
    assertEquals(0, artist.getPaintingArray().size());
    assertEquals(0, localParentMt.getPaintingArray().size());
    painting.setToArtist(localParentMt);
    assertEquals(0, artist.getPaintingArray().size());
    assertEquals(1, localParentMt.getPaintingArray().size());
    assertEquals(localParentMt.getPaintingArray().get(0).getObjectContext(), child);
    child.commitChangesToParent();
    assertEquals(1, artist.getPaintingArray().size());
    assertEquals(artist.getPaintingArray().get(0).getObjectContext(), context);
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) ObjectContext(org.apache.cayenne.ObjectContext) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 15 with Painting

use of org.apache.cayenne.testdo.testmap.Painting in project cayenne by apache.

the class NestedDataContextWriteIT method testDeleteNew.

@Test
public void testDeleteNew() throws Exception {
    createSingleArtistDataSet();
    DataContext context = createDataContext();
    ObjectContext childContext = runtime.newContext(context);
    Artist a = Cayenne.objectForPK(childContext, Artist.class, 33001);
    Painting p = childContext.newObject(Painting.class);
    p.setPaintingTitle("X");
    a.addToPaintingArray(p);
    childContext.commitChangesToParent();
    childContext.deleteObjects(p);
    a.removeFromPaintingArray(p);
    childContext.commitChangesToParent();
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) ObjectContext(org.apache.cayenne.ObjectContext) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Aggregations

Painting (org.apache.cayenne.testdo.testmap.Painting)222 Test (org.junit.Test)218 Artist (org.apache.cayenne.testdo.testmap.Artist)144 SelectQuery (org.apache.cayenne.query.SelectQuery)75 UnitTestClosure (org.apache.cayenne.unit.di.UnitTestClosure)35 List (java.util.List)30 Expression (org.apache.cayenne.exp.Expression)29 ArrayList (java.util.ArrayList)21 BigDecimal (java.math.BigDecimal)19 PaintingInfo (org.apache.cayenne.testdo.testmap.PaintingInfo)14 ObjectContext (org.apache.cayenne.ObjectContext)13 EJBQLQuery (org.apache.cayenne.query.EJBQLQuery)13 Gallery (org.apache.cayenne.testdo.testmap.Gallery)13 ValueHolder (org.apache.cayenne.ValueHolder)12 ROPainting (org.apache.cayenne.testdo.testmap.ROPainting)12 SQLTemplate (org.apache.cayenne.query.SQLTemplate)11 DataRow (org.apache.cayenne.DataRow)7 ProcedureQuery (org.apache.cayenne.query.ProcedureQuery)6 LifecycleCallbackRegistry (org.apache.cayenne.reflect.LifecycleCallbackRegistry)6 RefreshQuery (org.apache.cayenne.query.RefreshQuery)5