Search in sources :

Example 61 with SelectQuery

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

the class JointPrefetchIT method testJointPrefetchMultiStep.

@Test
public void testJointPrefetchMultiStep() throws Exception {
    createJointPrefetchDataSet();
    // query with to-many joint prefetches
    SelectQuery<Artist> q = new SelectQuery<>(Artist.class);
    q.addPrefetch(Artist.PAINTING_ARRAY.dot(Painting.TO_GALLERY).joint());
    final DataContext context = this.context;
    // make sure phantomly prefetched objects are not deallocated
    context.getObjectStore().objectMap = new HashMap<>();
    // sanity check...
    DataObject g1 = (DataObject) context.getGraphManager().getNode(new ObjectId("Gallery", Gallery.GALLERY_ID_PK_COLUMN, 33001));
    assertNull(g1);
    final List<Artist> objects = q.select(context);
    queryInterceptor.runWithQueriesBlocked(() -> {
        assertEquals(3, objects.size());
        for (Artist a : objects) {
            ValueHolder list = (ValueHolder) a.getPaintingArray();
            assertNotNull(list);
            // intermediate relationship is not fetched...
            assertTrue(list.isFault());
        }
        // however both galleries must be in memory...
        DataObject g11 = (DataObject) context.getGraphManager().getNode(new ObjectId("Gallery", Gallery.GALLERY_ID_PK_COLUMN, 33001));
        assertNotNull(g11);
        assertEquals(PersistenceState.COMMITTED, g11.getPersistenceState());
        DataObject g2 = (DataObject) context.getGraphManager().getNode(new ObjectId("Gallery", Gallery.GALLERY_ID_PK_COLUMN, 33002));
        assertNotNull(g2);
        assertEquals(PersistenceState.COMMITTED, g2.getPersistenceState());
    });
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) SelectQuery(org.apache.cayenne.query.SelectQuery) DataObject(org.apache.cayenne.DataObject) ObjectId(org.apache.cayenne.ObjectId) ValueHolder(org.apache.cayenne.ValueHolder) Test(org.junit.Test)

Example 62 with SelectQuery

use of org.apache.cayenne.query.SelectQuery 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)

Example 63 with SelectQuery

use of org.apache.cayenne.query.SelectQuery 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 64 with SelectQuery

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

the class MiscTypesIT method testByteArray.

@Test
public void testByteArray() {
    ArraysEntity test = context.newObject(ArraysEntity.class);
    byte[] a = new byte[] { 1, 2, 3 };
    test.setByteArray(a);
    context.commitChanges();
    SelectQuery q = new SelectQuery(ArraysEntity.class);
    ArraysEntity testRead = (ArraysEntity) context.performQuery(q).get(0);
    assertNotNull(testRead.getByteArray());
    assertArraysEqual(a, testRead.getByteArray());
    test.setByteArray(null);
    context.commitChanges();
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) ArraysEntity(org.apache.cayenne.testdo.misc_types.ArraysEntity) Test(org.junit.Test)

Example 65 with SelectQuery

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

the class MiscTypesIT method testByteWrapperArray.

@Test
public void testByteWrapperArray() {
    ArraysEntity test = context.newObject(ArraysEntity.class);
    Byte[] a = new Byte[] { new Byte((byte) 1), new Byte((byte) 2), new Byte((byte) 3) };
    test.setByteWrapperArray(a);
    context.commitChanges();
    SelectQuery q = new SelectQuery(ArraysEntity.class);
    ArraysEntity testRead = (ArraysEntity) context.performQuery(q).get(0);
    assertNotNull(testRead.getByteWrapperArray());
    assertArraysEqual(a, testRead.getByteWrapperArray());
    test.setByteWrapperArray(null);
    context.commitChanges();
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) ArraysEntity(org.apache.cayenne.testdo.misc_types.ArraysEntity) Test(org.junit.Test)

Aggregations

SelectQuery (org.apache.cayenne.query.SelectQuery)360 Test (org.junit.Test)348 Artist (org.apache.cayenne.testdo.testmap.Artist)128 Painting (org.apache.cayenne.testdo.testmap.Painting)75 UnitTestClosure (org.apache.cayenne.unit.di.UnitTestClosure)64 Expression (org.apache.cayenne.exp.Expression)47 List (java.util.List)43 ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)26 ValueHolder (org.apache.cayenne.ValueHolder)21 ReturnTypesMap1 (org.apache.cayenne.testdo.return_types.ReturnTypesMap1)18 ArrayList (java.util.ArrayList)16 Date (java.util.Date)13 DataRow (org.apache.cayenne.DataRow)12 ObjectContext (org.apache.cayenne.ObjectContext)12 DbEntity (org.apache.cayenne.map.DbEntity)12 SQLTemplate (org.apache.cayenne.query.SQLTemplate)11 ROArtist (org.apache.cayenne.testdo.testmap.ROArtist)11 HashMap (java.util.HashMap)10 ObjectId (org.apache.cayenne.ObjectId)10 Query (org.apache.cayenne.query.Query)10