Search in sources :

Example 56 with EJBQLQuery

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

the class DataContextEJBQLFetchJoinIT method testSeveralEntitiesFetchJoins.

@Test
public void testSeveralEntitiesFetchJoins() throws Exception {
    createMultipleFetchJoinsDataSet();
    String ejbql = "SELECT DISTINCT a , g " + "FROM Artist a JOIN FETCH a.paintingArray , Gallery g JOIN FETCH g.exhibitArray " + "WHERE a.artistName='A1' AND g.galleryName='gallery1'";
    EJBQLQuery query = new EJBQLQuery(ejbql);
    final List<?> objects = context.performQuery(query);
    queryBlocker.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            assertNotNull(objects);
            assertFalse(objects.isEmpty());
            assertEquals(1, objects.size());
        }
    });
}
Also used : EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) Test(org.junit.Test)

Example 57 with EJBQLQuery

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

the class DataContextEJBQLFetchJoinIT method testSeveralEntitiesAndScalarFetchInnerJoins.

@Test
public void testSeveralEntitiesAndScalarFetchInnerJoins() throws Exception {
    createMultipleFetchJoinsDataSet();
    String ejbql = "SELECT DISTINCT a, a.artistName , g " + "FROM Artist a JOIN FETCH a.paintingArray, Gallery g JOIN FETCH g.exhibitArray " + "ORDER BY a.artistName";
    EJBQLQuery query = new EJBQLQuery(ejbql);
    final List<?> objects = context.performQuery(query);
    queryBlocker.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            assertEquals(2, objects.size());
            Object[] firstRow = (Object[]) objects.get(0);
            Artist a = (Artist) firstRow[0];
            assertEquals("A1", a.getArtistName());
            List<Painting> paintings = a.getPaintingArray();
            assertNotNull(paintings);
            assertFalse(((ValueHolder) paintings).isFault());
            assertEquals(2, paintings.size());
            List<String> expectedPaintingsNames = new ArrayList<String>();
            expectedPaintingsNames.add("P11");
            expectedPaintingsNames.add("P12");
            Iterator<Painting> paintingsIterator = paintings.iterator();
            while (paintingsIterator.hasNext()) {
                Painting p = paintingsIterator.next();
                assertEquals(PersistenceState.COMMITTED, p.getPersistenceState());
                assertNotNull(p.getPaintingTitle());
                assertTrue(expectedPaintingsNames.contains(p.getPaintingTitle()));
            }
            String artistName = (String) firstRow[1];
            assertEquals("A1", artistName);
            Gallery g1 = (Gallery) firstRow[2];
            assertEquals("gallery1", g1.getGalleryName());
            List<Exhibit> exibits = g1.getExhibitArray();
            assertNotNull(exibits);
            assertFalse(((ValueHolder) exibits).isFault());
            assertEquals(2, exibits.size());
            Object[] secondRow = (Object[]) objects.get(1);
            a = (Artist) secondRow[0];
            assertEquals("A2", a.getArtistName());
            paintings = a.getPaintingArray();
            assertNotNull(paintings);
            assertFalse(((ValueHolder) paintings).isFault());
            assertEquals(1, paintings.size());
            expectedPaintingsNames = new ArrayList<String>();
            expectedPaintingsNames.add("P2");
            paintingsIterator = paintings.iterator();
            while (paintingsIterator.hasNext()) {
                Painting p = paintingsIterator.next();
                assertEquals(PersistenceState.COMMITTED, p.getPersistenceState());
                assertNotNull(p.getPaintingTitle());
                assertTrue(expectedPaintingsNames.contains(p.getPaintingTitle()));
            }
            artistName = (String) secondRow[1];
            assertEquals("A2", artistName);
            Gallery g2 = (Gallery) secondRow[2];
            assertEquals(g1, g2);
        }
    });
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) ArrayList(java.util.ArrayList) ValueHolder(org.apache.cayenne.ValueHolder) Painting(org.apache.cayenne.testdo.testmap.Painting) Gallery(org.apache.cayenne.testdo.testmap.Gallery) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 58 with EJBQLQuery

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

the class DataContextEJBQLFetchJoinIT method testFetchJoinForOneEntity.

@Test
public void testFetchJoinForOneEntity() throws Exception {
    createOneFetchJoinDataSet();
    String ejbql = "SELECT a FROM Artist a JOIN FETCH a.paintingArray ";
    EJBQLQuery query = new EJBQLQuery(ejbql);
    final List<?> objects = context.performQuery(query);
    queryBlocker.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            assertEquals(2, objects.size());
            Iterator<?> it = objects.iterator();
            while (it.hasNext()) {
                Artist a = (Artist) it.next();
                List<Painting> list = a.getPaintingArray();
                assertNotNull(list);
                assertFalse(((ValueHolder) list).isFault());
                for (Painting p : list) {
                    assertEquals(PersistenceState.COMMITTED, p.getPersistenceState());
                    // make sure properties are not null..
                    assertNotNull(p.getPaintingTitle());
                }
            }
        }
    });
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) ValueHolder(org.apache.cayenne.ValueHolder) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 59 with EJBQLQuery

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

the class DataContextEJBQLFlattenedRelationshipsIT method testCollectionSubquery.

@Test
public void testCollectionSubquery() throws Exception {
    createFt123();
    String ejbql = "SELECT ft FROM FlattenedTest1 ft " + "WHERE (SELECT COUNT(f) FROM ft.ft3Array f) = 1";
    EJBQLQuery query = new EJBQLQuery(ejbql);
// TODO fails but not because of flattened relationship,
// the reason is that property "ft3Array" inside the subquery
// parses as unmapped
/*
         * List<?> objects = context.performQuery(query); assertNotNull(objects);
         * assertFalse(objects.isEmpty()); assertEquals(1, objects.size()); Set<Object>
         * ids = new HashSet<Object>(); Iterator<?> it = objects.iterator(); while
         * (it.hasNext()) { Object id = Cayenne.pkForObject((Persistent) it.next());
         * ids.add(id); } assertTrue(ids.contains(new Integer(2)));
         */
}
Also used : EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) Test(org.junit.Test)

Example 60 with EJBQLQuery

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

the class DataContextEJBQLFlattenedRelationshipsIT method testCollectionMemberOfThetaJoinLongRelationshipSequence.

@Test
public void testCollectionMemberOfThetaJoinLongRelationshipSequence() throws Exception {
    createFt1234();
    String ejbql = "SELECT f FROM FlattenedTest4 f, FlattenedTest1 ft " + "WHERE f MEMBER OF ft.ft4ArrayFor1 AND ft = :ft";
    FlattenedTest1 ft = Cayenne.objectForPK(context, FlattenedTest1.class, 1);
    EJBQLQuery query = new EJBQLQuery(ejbql);
    query.setParameter("ft", ft);
    List<?> objects = context.performQuery(query);
    assertEquals(1, objects.size());
    Set<Object> ids = new HashSet<Object>();
    Iterator<?> it = objects.iterator();
    while (it.hasNext()) {
        Object id = Cayenne.pkForObject((Persistent) it.next());
        ids.add(id);
    }
    assertTrue(ids.contains(new Integer(1)));
    ft = Cayenne.objectForPK(context, FlattenedTest1.class, 2);
    query = new EJBQLQuery(ejbql);
    query.setParameter("ft", ft);
    objects = context.performQuery(query);
    assertEquals(0, objects.size());
}
Also used : FlattenedTest1(org.apache.cayenne.testdo.relationships_flattened.FlattenedTest1) EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

EJBQLQuery (org.apache.cayenne.query.EJBQLQuery)160 Test (org.junit.Test)158 Artist (org.apache.cayenne.testdo.testmap.Artist)39 HashSet (java.util.HashSet)35 Painting (org.apache.cayenne.testdo.testmap.Painting)15 QueryResponse (org.apache.cayenne.QueryResponse)12 BigDecimal (java.math.BigDecimal)10 Persistent (org.apache.cayenne.Persistent)10 List (java.util.List)9 ArrayList (java.util.ArrayList)7 FlattenedTest1 (org.apache.cayenne.testdo.relationships_flattened.FlattenedTest1)6 CompoundPainting (org.apache.cayenne.testdo.testmap.CompoundPainting)5 UnitTestClosure (org.apache.cayenne.unit.di.UnitTestClosure)5 HashMap (java.util.HashMap)4 Iterator (java.util.Iterator)4 ValueHolder (org.apache.cayenne.ValueHolder)4 CompoundPkTestEntity (org.apache.cayenne.testdo.compound.CompoundPkTestEntity)4 Gallery (org.apache.cayenne.testdo.testmap.Gallery)4 Calendar (java.util.Calendar)3 EJBQLCompiledExpression (org.apache.cayenne.ejbql.EJBQLCompiledExpression)3