Search in sources :

Example 31 with EJBQLQuery

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

the class DataContextEJBQLUpdateIT method testUpdateQualifier.

@Test
public void testUpdateQualifier() throws Exception {
    createThreeArtistsTwoPaintings();
    EJBQLQuery check = new EJBQLQuery("select count(p) from Painting p " + "WHERE p.paintingTitle is NULL or p.paintingTitle <> 'XX'");
    Object notUpdated = Cayenne.objectForQuery(context, check);
    assertEquals(new Long(2l), notUpdated);
    String ejbql = "UPDATE Painting AS p SET p.paintingTitle = 'XX' WHERE p.paintingTitle = 'P1'";
    EJBQLQuery query = new EJBQLQuery(ejbql);
    QueryResponse result = context.performGenericQuery(query);
    int[] count = result.firstUpdateCount();
    assertNotNull(count);
    assertEquals(1, count.length);
    assertEquals(1, count[0]);
    notUpdated = Cayenne.objectForQuery(context, check);
    assertEquals(new Long(1l), notUpdated);
}
Also used : EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) QueryResponse(org.apache.cayenne.QueryResponse) Test(org.junit.Test)

Example 32 with EJBQLQuery

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

the class DataContextFlattenedAttributesIT method testSelectEJQBQLCollectionTheta.

@Test
public void testSelectEJQBQLCollectionTheta() throws Exception {
    createTestDataSet();
    EJBQLQuery query = new EJBQLQuery("SELECT DISTINCT a FROM CompoundPainting cp, Artist a " + "WHERE a.artistName=cp.artistName ORDER BY a.artistName");
    List<?> objects = context.performQuery(query);
    assertNotNull(objects);
    assertEquals(4, objects.size());
    Iterator<?> i = objects.iterator();
    int index = 1;
    while (i.hasNext()) {
        Artist artist = (Artist) i.next();
        assertEquals("artist" + index, artist.getArtistName());
        index++;
    }
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) Test(org.junit.Test)

Example 33 with EJBQLQuery

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

the class DataContextFlattenedAttributesIT method testSelectEJQBQLLike.

@Test
public void testSelectEJQBQLLike() throws Exception {
    createTestDataSet();
    EJBQLQuery query = new EJBQLQuery("SELECT a FROM CompoundPainting a WHERE a.artistName LIKE 'artist%' " + "ORDER BY a.paintingTitle");
    List<?> objects = context.performQuery(query);
    assertNotNull(objects);
    assertEquals(8, objects.size());
    Iterator<?> i = objects.iterator();
    int index = 1;
    while (i.hasNext()) {
        CompoundPainting painting = (CompoundPainting) i.next();
        assertEquals("painting" + index, painting.getPaintingTitle());
        index++;
    }
}
Also used : EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) CompoundPainting(org.apache.cayenne.testdo.testmap.CompoundPainting) Test(org.junit.Test)

Example 34 with EJBQLQuery

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

the class DataContextFlattenedAttributesIT method testSelectEJQBQLHaving.

@Test
public void testSelectEJQBQLHaving() throws Exception {
    createTestDataSet();
    EJBQLQuery query = new EJBQLQuery("SELECT cp.galleryName, COUNT(a) from  Artist a, CompoundPainting cp " + "WHERE cp.artistName = a.artistName " + "GROUP BY cp.galleryName " + "HAVING cp.galleryName LIKE 'gallery1'");
    List<Object[]> objects = context.performQuery(query);
    assertNotNull(objects);
    assertEquals(1, objects.size());
    Object[] galleryItem = objects.get(0);
    assertEquals("gallery1", galleryItem[0]);
    assertEquals(3L, galleryItem[1]);
}
Also used : EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) Test(org.junit.Test)

Example 35 with EJBQLQuery

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

the class DataContextFlattenedAttributesIT method testDelete.

@Test
public void testDelete() throws Exception {
    // throw in a bit of random overlapping data, to make sure FK/PK correspondence is
    // not purely coincidental
    Artist a = context.newObject(Artist.class);
    a.setArtistName("AX");
    context.commitChanges();
    CompoundPainting o1 = context.newObject(CompoundPainting.class);
    o1.setArtistName("A1");
    o1.setEstimatedPrice(new BigDecimal(1.0d));
    o1.setGalleryName("G1");
    o1.setPaintingTitle("P1");
    o1.setTextReview("T1");
    context.commitChanges();
    context.deleteObjects(o1);
    context.commitChanges();
    Number artistCount = (Number) Cayenne.objectForQuery(context, new EJBQLQuery("select count(a) from Artist a"));
    assertEquals(1, artistCount.intValue());
    Number paintingCount = (Number) Cayenne.objectForQuery(context, new EJBQLQuery("select count(a) from Painting a"));
    assertEquals(0, paintingCount.intValue());
    Number galleryCount = (Number) Cayenne.objectForQuery(context, new EJBQLQuery("select count(a) from Gallery a"));
    assertEquals(0, galleryCount.intValue());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) CompoundPainting(org.apache.cayenne.testdo.testmap.CompoundPainting) BigDecimal(java.math.BigDecimal) 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