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);
}
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++;
}
}
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++;
}
}
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]);
}
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());
}
Aggregations