Search in sources :

Example 76 with Painting

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

the class DataContextPrefetchIT method testPrefetch_ToOne.

@Test
public void testPrefetch_ToOne() throws Exception {
    createTwoArtistsAndTwoPaintingsDataSet();
    SelectQuery q = new SelectQuery(Painting.class);
    q.addPrefetch(Painting.TO_ARTIST.disjoint());
    final List<Painting> result = context.performQuery(q);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            assertFalse(result.isEmpty());
            Painting p1 = result.get(0);
            Object toOnePrefetch = p1.readNestedProperty("toArtist");
            assertNotNull(toOnePrefetch);
            assertTrue("Expected Artist, got: " + toOnePrefetch.getClass().getName(), toOnePrefetch instanceof Artist);
            Artist a1 = (Artist) toOnePrefetch;
            assertEquals(PersistenceState.COMMITTED, a1.getPersistenceState());
        }
    });
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Artist(org.apache.cayenne.testdo.testmap.Artist) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 77 with Painting

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

the class DataContextPrefetchIT method testPrefetch_ToOne_ObjPath.

@Test
public void testPrefetch_ToOne_ObjPath() throws Exception {
    createTwoArtistsAndTwoPaintingsDataSet();
    SelectQuery q = new SelectQuery(Painting.class);
    q.addPrefetch(Painting.TO_ARTIST.disjoint());
    q.andQualifier(ExpressionFactory.matchExp("toArtist.artistName", "artist2"));
    List<Painting> results = context.performQuery(q);
    assertEquals(1, results.size());
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 78 with Painting

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

the class DataContextPrefetchIT method testPrefetchWithLocalCache.

/**
 * This test and next one is the result of CAY-2349 fix
 */
@Test
public void testPrefetchWithLocalCache() throws Exception {
    tArtist.deleteAll();
    tGallery.deleteAll();
    tPainting.deleteAll();
    tArtist.insert(1, "artist1");
    tGallery.insert(1, "gallery1");
    tPainting.insert(1, "painting1", 1, 100, 1);
    List<Painting> paintings = ObjectSelect.query(Painting.class).localCache("g1").select(context);
    assertEquals(1, paintings.size());
    assertTrue(paintings.get(0).readPropertyDirectly(Painting.TO_ARTIST.getName()) instanceof Fault);
    paintings = ObjectSelect.query(Painting.class).prefetch(Painting.TO_ARTIST.joint()).localCache("g1").select(context);
    assertEquals(1, paintings.size());
    assertTrue(paintings.get(0).readPropertyDirectly(Painting.TO_ARTIST.getName()) instanceof Artist);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            List<Painting> paintings = ObjectSelect.query(Painting.class).prefetch(Painting.TO_ARTIST.joint()).localCache("g1").select(context);
            assertEquals(1, paintings.size());
            assertTrue(paintings.get(0).readPropertyDirectly(Painting.TO_ARTIST.getName()) instanceof Artist);
        }
    });
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) Fault(org.apache.cayenne.Fault) List(java.util.List) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 79 with Painting

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

the class DataContextPrefetchIT method testPrefetchJointAndDisjointByIdTogether.

@Test
public void testPrefetchJointAndDisjointByIdTogether() throws Exception {
    createArtistWithTwoPaintingsAndTwoInfosDataSet();
    SelectQuery<Painting> query = new SelectQuery<Painting>(Painting.class);
    query.andQualifier(Painting.PAINTING_TITLE.eq("p_artist2"));
    query.addPrefetch(Painting.TO_ARTIST.joint());
    query.addPrefetch(Painting.TO_PAINTING_INFO.disjointById());
    final List<Painting> results = context.select(query);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            assertEquals(1, results.size());
            Painting p0 = results.get(0);
            Artist a0 = (Artist) p0.readPropertyDirectly(Painting.TO_ARTIST.getName());
            assertNotNull(a0);
            PaintingInfo info = (PaintingInfo) p0.readPropertyDirectly(Painting.TO_PAINTING_INFO.getName());
            assertNotNull(info);
        }
    });
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Artist(org.apache.cayenne.testdo.testmap.Artist) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) PaintingInfo(org.apache.cayenne.testdo.testmap.PaintingInfo) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 80 with Painting

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

the class DataContextPrefetchIT method testPrefetch_OneToOneWithQualifier.

@Test
public void testPrefetch_OneToOneWithQualifier() throws Exception {
    createArtistWithTwoPaintingsAndTwoInfosDataSet();
    Expression e = ExpressionFactory.likeExp("toArtist.artistName", "a%");
    SelectQuery q = new SelectQuery(Painting.class, e);
    q.addPrefetch(Painting.TO_PAINTING_INFO.disjoint());
    q.addOrdering(Painting.PAINTING_TITLE.asc());
    final List<Painting> results = context.performQuery(q);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            assertEquals(2, results.size());
            // testing non-null to-one target
            Painting p0 = results.get(0);
            Object o2 = p0.readPropertyDirectly(Painting.TO_PAINTING_INFO.getName());
            assertTrue(o2 instanceof PaintingInfo);
            PaintingInfo pi2 = (PaintingInfo) o2;
            assertEquals(PersistenceState.COMMITTED, pi2.getPersistenceState());
            assertEquals(Cayenne.intPKForObject(p0), Cayenne.intPKForObject(pi2));
            // testing null to-one target
            Painting p1 = results.get(1);
            assertNull(p1.readPropertyDirectly(Painting.TO_PAINTING_INFO.getName()));
            // there was a bug marking an object as dirty when clearing the
            // relationships
            assertEquals(PersistenceState.COMMITTED, p1.getPersistenceState());
        }
    });
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Expression(org.apache.cayenne.exp.Expression) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) PaintingInfo(org.apache.cayenne.testdo.testmap.PaintingInfo) 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