Search in sources :

Example 6 with Painting

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

the class DeleteObjectIT method testDeleteHollow.

@Test
public void testDeleteHollow() throws Exception {
    createHollowDataSet();
    List<Painting> paintings = context.performQuery(new SelectQuery(Painting.class));
    Painting p = paintings.get(0);
    Artist a = p.getToArtist();
    assertEquals(PersistenceState.HOLLOW, a.getPersistenceState());
    context.deleteObjects(a);
    assertEquals(PersistenceState.DELETED, a.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 7 with Painting

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

the class FlattenedPrefetchIT method testJointMultiPrefetch.

@Test
public void testJointMultiPrefetch() throws Exception {
    createPrefetchDataSet2();
    SelectQuery q = new SelectQuery(Painting.class);
    q.addPrefetch(Painting.TO_ARTIST.joint());
    q.addPrefetch(Painting.TO_ARTIST.dot(Artist.GROUP_ARRAY).joint());
    final List<Painting> objects = context.performQuery(q);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            assertEquals(3, objects.size());
            for (Painting p : objects) {
                Artist a = p.getToArtist();
                assertEquals(PersistenceState.COMMITTED, a.getPersistenceState());
                List<ArtGroup> list = a.getGroupArray();
                assertNotNull(list);
                assertFalse("artist's groups not resolved: " + a, ((ValueHolder) list).isFault());
                assertTrue(list.size() > 0);
                for (ArtGroup g : list) {
                    assertEquals(PersistenceState.COMMITTED, g.getPersistenceState());
                }
                // assert no duplicates
                Set<ArtGroup> s = new HashSet<ArtGroup>(list);
                assertEquals(s.size(), list.size());
            }
        }
    });
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Artist(org.apache.cayenne.testdo.testmap.Artist) Set(java.util.Set) HashSet(java.util.HashSet) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) List(java.util.List) ValueHolder(org.apache.cayenne.ValueHolder) ArtGroup(org.apache.cayenne.testdo.testmap.ArtGroup) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 8 with Painting

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

the class JointPrefetchIT method testJointPrefetchSQLTemplate.

@Test
public void testJointPrefetchSQLTemplate() throws Exception {
    createJointPrefetchDataSet();
    // correctly naming columns is the key..
    SQLTemplate q = new SQLTemplate(Artist.class, "SELECT distinct " + "#result('ESTIMATED_PRICE' 'BigDecimal' '' 'paintingArray.ESTIMATED_PRICE'), " + "#result('PAINTING_TITLE' 'String' '' 'paintingArray.PAINTING_TITLE'), " + "#result('GALLERY_ID' 'int' '' 'paintingArray.GALLERY_ID'), " + "#result('PAINTING_ID' 'int' '' 'paintingArray.PAINTING_ID'), " + "#result('ARTIST_NAME' 'String'), " + "#result('DATE_OF_BIRTH' 'java.util.Date'), " + "#result('t0.ARTIST_ID' 'int' '' 'ARTIST_ID') " + "FROM ARTIST t0, PAINTING t1 " + "WHERE t0.ARTIST_ID = t1.ARTIST_ID");
    q.addPrefetch(Artist.PAINTING_ARRAY.joint());
    q.setFetchingDataRows(false);
    @SuppressWarnings("unchecked") final List<Artist> objects = (List<Artist>) context.performQuery(q);
    queryInterceptor.runWithQueriesBlocked(() -> {
        // without OUTER join we will get fewer objects...
        assertEquals(2, objects.size());
        for (Artist a : objects) {
            List<Painting> list = a.getPaintingArray();
            assertNotNull(list);
            assertFalse(((ValueHolder) list).isFault());
            assertTrue(list.size() > 0);
            for (Painting p : list) {
                assertEquals(PersistenceState.COMMITTED, p.getPersistenceState());
                // make sure properties are not null..
                assertNotNull(p.getPaintingTitle());
            }
        }
    });
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) Artist(org.apache.cayenne.testdo.testmap.Artist) List(java.util.List) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 9 with Painting

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

the class JointPrefetchIT method testJointPrefetchSQLSelectToMany.

@Test
public void testJointPrefetchSQLSelectToMany() throws Exception {
    createJointPrefetchDataSet();
    @SuppressWarnings("unchecked") final List<Artist> objects = SQLSelect.query(Artist.class, "SELECT " + "#result('PAINTING_ID' 'int' '' 'paintingArray.PAINTING_ID'), " + "#result('ARTIST_NAME' 'String'), " + "#result('DATE_OF_BIRTH' 'java.util.Date'), " + "#result('t0.ARTIST_ID' 'int' '' 'ARTIST_ID') " + "FROM ARTIST t0, PAINTING t1 " + "WHERE t0.ARTIST_ID = t1.ARTIST_ID").addPrefetch(Artist.PAINTING_ARRAY.joint()).select(context);
    queryInterceptor.runWithQueriesBlocked(() -> {
        assertNotNull(objects);
        assertEquals(2, objects.size());
        for (Artist artist : objects) {
            List<Painting> paintings = artist.getPaintingArray();
            assertTrue(paintings.size() > 0);
            for (Painting painting : paintings) {
                assertEquals(PersistenceState.COMMITTED, painting.getPersistenceState());
            }
        }
    });
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 10 with Painting

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

the class JointPrefetchIT method testJointPrefetchToOne.

@Test
public void testJointPrefetchToOne() throws Exception {
    createJointPrefetchDataSet();
    // query with to-many joint prefetches
    SelectQuery<Painting> q = new SelectQuery<>(Painting.class);
    q.addOrdering("db:PAINTING_ID", SortOrder.ASCENDING);
    q.addPrefetch(Painting.TO_ARTIST.joint());
    final List<Painting> objects = q.select(context);
    queryInterceptor.runWithQueriesBlocked(() -> {
        assertEquals(3, 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)

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