Search in sources :

Example 31 with SelectQuery

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

the class SimpleIdIncrementalFaultListPrefetchIT method testPrefetch3.

/**
 * Test that a to-many relationship is initialized.
 */
@Test
public void testPrefetch3() throws Exception {
    createArtistsAndPaintingsDataSet();
    Expression e = ExpressionFactory.likeExp("artistName", "artist1%");
    SelectQuery q = new SelectQuery("Artist", e);
    q.addPrefetch("paintingArray");
    q.setPageSize(3);
    IncrementalFaultList result = (IncrementalFaultList) context.performQuery(q);
    assertEquals(6, result.size());
    // currently queries with prefetch do not resolve their first page
    assertEquals(result.size(), result.getUnfetchedObjects());
    // go through the second page objects and check their to many
    for (int i = 3; i < 6; i++) {
        Artist a = (Artist) result.get(i);
        List paintings = a.getPaintingArray();
        assertFalse(((ValueHolder) paintings).isFault());
        assertEquals(1, paintings.size());
    }
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Artist(org.apache.cayenne.testdo.testmap.Artist) Expression(org.apache.cayenne.exp.Expression) List(java.util.List) Test(org.junit.Test)

Example 32 with SelectQuery

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

the class SimpleIdIncrementalFaultListPrefetchIT method testPrefetch1.

/**
 * Test that all queries specified in prefetch are executed with a single prefetch
 * path.
 */
@Test
public void testPrefetch1() throws Exception {
    createArtistsAndPaintingsDataSet();
    Expression e = ExpressionFactory.likeExp("artistName", "artist1%");
    SelectQuery q = new SelectQuery("Artist", e);
    q.addPrefetch("paintingArray");
    q.setPageSize(3);
    final IncrementalFaultList<?> result = (IncrementalFaultList) context.performQuery(q);
    assertEquals(6, result.size());
    // currently queries with prefetch do not resolve their first page
    assertEquals(result.size(), result.getUnfetchedObjects());
    int count = queryInterceptor.runWithQueryCounter(new UnitTestClosure() {

        public void execute() {
            // go through the second page objects and count queries
            for (int i = 3; i < 6; i++) {
                result.get(i);
            }
        }
    });
    // within the same page only one query should've been executed
    assertEquals(1, count);
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Expression(org.apache.cayenne.exp.Expression) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) Test(org.junit.Test)

Example 33 with SelectQuery

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

the class SingleTableInheritanceIT method testSave.

@Test
public void testSave() throws Exception {
    ClientCompany company = context.newObject(ClientCompany.class);
    company.setName("Boeing");
    CustomerRepresentative rep = context.newObject(CustomerRepresentative.class);
    rep.setName("Joe Schmoe");
    rep.setToClientCompany(company);
    rep.setPersonType("C");
    Employee employee = context.newObject(Employee.class);
    employee.setName("Our Joe Schmoe");
    employee.setPersonType("E");
    context.commitChanges();
    context.invalidateObjects(company, rep, employee);
    SelectQuery query = new SelectQuery(CustomerRepresentative.class);
    List<?> reps = context2.performQuery(query);
    assertEquals(1, reps.size());
    assertEquals(1, countObjectOfClass(reps, CustomerRepresentative.class));
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) ClientCompany(org.apache.cayenne.testdo.inheritance_people.ClientCompany) Employee(org.apache.cayenne.testdo.inheritance_people.Employee) CustomerRepresentative(org.apache.cayenne.testdo.inheritance_people.CustomerRepresentative) Test(org.junit.Test)

Example 34 with SelectQuery

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

the class SingleTableInheritanceIT method testMatchingOnSuperAttributes.

@Test
public void testMatchingOnSuperAttributes() throws Exception {
    create2PersonDataSet();
    // fetch on leaf, but match on a super attribute
    SelectQuery select = new SelectQuery(Manager.class);
    select.andQualifier(AbstractPerson.NAME.eq("E2"));
    List<Manager> results = context.performQuery(select);
    assertEquals(1, results.size());
    assertEquals("E2", results.get(0).getName());
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Manager(org.apache.cayenne.testdo.inheritance_people.Manager) Test(org.junit.Test)

Example 35 with SelectQuery

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

the class SingleTableInheritanceIT method testPaginatedQueries.

@Test
public void testPaginatedQueries() throws Exception {
    create5PersonDataSet();
    SelectQuery select = new SelectQuery(AbstractPerson.class);
    select.addOrdering("db:" + AbstractPerson.PERSON_ID_PK_COLUMN, SortOrder.ASCENDING);
    select.setPageSize(3);
    List<AbstractPerson> results = context.performQuery(select);
    assertEquals(5, results.size());
    assertTrue(results.get(0) instanceof Employee);
    // this is where things would blow up per CAY-1142
    assertTrue(results.get(1) instanceof Manager);
    assertTrue(results.get(3) instanceof Manager);
    assertTrue(results.get(4) instanceof Employee);
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Employee(org.apache.cayenne.testdo.inheritance_people.Employee) Manager(org.apache.cayenne.testdo.inheritance_people.Manager) AbstractPerson(org.apache.cayenne.testdo.inheritance_people.AbstractPerson) 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