Search in sources :

Example 56 with SelectQuery

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

the class EmbeddingIT method testUpdateEmbeddedProperties.

@Test
public void testUpdateEmbeddedProperties() throws Exception {
    createUpdateDataSet();
    SelectQuery query = new SelectQuery<>(EmbedEntity1.class);
    query.addOrdering(EmbedEntity1.NAME.asc());
    List<?> results = context.performQuery(query);
    EmbedEntity1 o1 = (EmbedEntity1) results.get(0);
    Embeddable1 e11 = o1.getEmbedded1();
    e11.setEmbedded10("x1");
    assertEquals(PersistenceState.MODIFIED, o1.getPersistenceState());
    context.commitChanges();
    SelectQuery<DataRow> query1 = SelectQuery.dataRowQuery(EmbedEntity1.class);
    DataRow row = (DataRow) Cayenne.objectForQuery(context, query1);
    assertNotNull(row);
    assertEquals("x1", row.get("EMBEDDED10"));
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Embeddable1(org.apache.cayenne.testdo.embeddable.Embeddable1) DataRow(org.apache.cayenne.DataRow) EmbedEntity1(org.apache.cayenne.testdo.embeddable.EmbedEntity1) Test(org.junit.Test)

Example 57 with SelectQuery

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

the class EmbeddingIT method testUpdateEmbedded.

@Test
public void testUpdateEmbedded() throws Exception {
    createUpdateDataSet();
    SelectQuery query = new SelectQuery<>(EmbedEntity1.class);
    query.addOrdering(EmbedEntity1.NAME.asc());
    List<?> results = context.performQuery(query);
    EmbedEntity1 o1 = (EmbedEntity1) results.get(0);
    Embeddable1 e11 = new Embeddable1();
    e11.setEmbedded10("x1");
    e11.setEmbedded20("x2");
    o1.setEmbedded1(e11);
    assertEquals(PersistenceState.MODIFIED, o1.getPersistenceState());
    context.commitChanges();
    SelectQuery<DataRow> query1 = SelectQuery.dataRowQuery(EmbedEntity1.class);
    DataRow row = query1.selectOne(context);
    assertNotNull(row);
    assertEquals("x1", row.get("EMBEDDED10"));
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Embeddable1(org.apache.cayenne.testdo.embeddable.Embeddable1) DataRow(org.apache.cayenne.DataRow) EmbedEntity1(org.apache.cayenne.testdo.embeddable.EmbedEntity1) Test(org.junit.Test)

Example 58 with SelectQuery

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

the class FlattenedPrefetchIT method testJointManyToMany.

@Test
public void testJointManyToMany() throws Exception {
    createPrefetchDataSet1();
    SelectQuery q = new SelectQuery(Artist.class);
    q.addPrefetch(Artist.GROUP_ARRAY.joint());
    final List<Artist> objects = context.performQuery(q);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            assertEquals(3, objects.size());
            for (Artist a : objects) {
                List<ArtGroup> list = a.getGroupArray();
                assertNotNull(list);
                assertFalse("artist's groups not resolved: " + a, ((ValueHolder) list).isFault());
                assertTrue(list.size() > 0);
                for (ArtGroup g : list) {
                    assertEquals(PersistenceState.COMMITTED, g.getPersistenceState());
                }
                // assert no duplicates
                Set<ArtGroup> s = new HashSet<ArtGroup>(list);
                assertEquals(s.size(), list.size());
            }
        }
    });
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Artist(org.apache.cayenne.testdo.testmap.Artist) Set(java.util.Set) HashSet(java.util.HashSet) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) List(java.util.List) ValueHolder(org.apache.cayenne.ValueHolder) ArtGroup(org.apache.cayenne.testdo.testmap.ArtGroup) Test(org.junit.Test)

Example 59 with SelectQuery

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

the class FlattenedPrefetchIT method testJointMultiPrefetch.

@Test
public void testJointMultiPrefetch() throws Exception {
    createPrefetchDataSet2();
    SelectQuery q = new SelectQuery(Painting.class);
    q.addPrefetch(Painting.TO_ARTIST.joint());
    q.addPrefetch(Painting.TO_ARTIST.dot(Artist.GROUP_ARRAY).joint());
    final List<Painting> objects = context.performQuery(q);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            assertEquals(3, objects.size());
            for (Painting p : objects) {
                Artist a = p.getToArtist();
                assertEquals(PersistenceState.COMMITTED, a.getPersistenceState());
                List<ArtGroup> list = a.getGroupArray();
                assertNotNull(list);
                assertFalse("artist's groups not resolved: " + a, ((ValueHolder) list).isFault());
                assertTrue(list.size() > 0);
                for (ArtGroup g : list) {
                    assertEquals(PersistenceState.COMMITTED, g.getPersistenceState());
                }
                // assert no duplicates
                Set<ArtGroup> s = new HashSet<ArtGroup>(list);
                assertEquals(s.size(), list.size());
            }
        }
    });
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Artist(org.apache.cayenne.testdo.testmap.Artist) Set(java.util.Set) HashSet(java.util.HashSet) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) List(java.util.List) ValueHolder(org.apache.cayenne.ValueHolder) ArtGroup(org.apache.cayenne.testdo.testmap.ArtGroup) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 60 with SelectQuery

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

the class FlattenedRelationshipInContextIT method testIsToOneTargetModifiedFlattenedFault1.

@Test
public void testIsToOneTargetModifiedFlattenedFault1() throws Exception {
    createFlattenedTestDataSet();
    // fetch
    List<?> ft3s = context.performQuery(new SelectQuery(FlattenedTest3.class));
    assertEquals(1, ft3s.size());
    FlattenedTest3 ft3 = (FlattenedTest3) ft3s.get(0);
    // mark as dirty for the purpose of the test...
    ft3.setPersistenceState(PersistenceState.MODIFIED);
    assertTrue(ft3.readPropertyDirectly("toFT1") instanceof Fault);
    // test that checking for modifications does not trigger a fault, and generally
    // works well
    ClassDescriptor d = context.getEntityResolver().getClassDescriptor("FlattenedTest3");
    ArcProperty flattenedRel = (ArcProperty) d.getProperty("toFT1");
    ObjectDiff diff = context.getObjectStore().registerDiff(ft3.getObjectId(), null);
    assertFalse(DataRowUtils.isToOneTargetModified(flattenedRel, ft3, diff));
    assertTrue(ft3.readPropertyDirectly("toFT1") instanceof Fault);
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) FlattenedTest3(org.apache.cayenne.testdo.relationships_flattened.FlattenedTest3) ArcProperty(org.apache.cayenne.reflect.ArcProperty) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) Fault(org.apache.cayenne.Fault) 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