Search in sources :

Example 11 with ObjectIdQuery

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

the class PersistentObjectInContextIT method testResolveToOneReverseResolved.

@Test
public void testResolveToOneReverseResolved() throws Exception {
    createTwoMtTable1sAnd2sDataSet();
    ObjectId gid = new ObjectId("MtTable2", MtTable2.TABLE2_ID_PK_COLUMN, new Integer(1));
    ClientMtTable2 mtTable21 = (ClientMtTable2) Cayenne.objectForQuery(context, new ObjectIdQuery(gid));
    assertNotNull(mtTable21);
    ClientMtTable1 mtTable1 = mtTable21.getTable1();
    assertNotNull("To one relationship incorrectly resolved to null", mtTable1);
    List<ClientMtTable2> list = mtTable1.getTable2Array();
    assertNotNull(list);
    assertTrue(list instanceof ValueHolder);
    assertTrue(((ValueHolder) list).isFault());
    // resolve it here...
    assertEquals(2, list.size());
    for (ClientMtTable2 t2 : list) {
        PersistentObjectHolder holder = (PersistentObjectHolder) t2.getTable1Direct();
        assertFalse(holder.isFault());
        assertSame(mtTable1, holder.getValue());
    }
    assertEquals("g1", mtTable1.getGlobalAttribute1());
}
Also used : PersistentObjectHolder(org.apache.cayenne.util.PersistentObjectHolder) ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ClientMtTable2(org.apache.cayenne.testdo.mt.ClientMtTable2) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Test(org.junit.Test)

Example 12 with ObjectIdQuery

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

the class ShallowMergeOperationIT method testMerge_NoOverride.

@Test
public void testMerge_NoOverride() throws Exception {
    createArtistsDataSet();
    ObjectContext childContext = runtime.newContext(context);
    final ShallowMergeOperation op = new ShallowMergeOperation(childContext);
    int modifiedId = 33003;
    final Artist modified = (Artist) Cayenne.objectForQuery(context, new ObjectIdQuery(new ObjectId("Artist", Artist.ARTIST_ID_PK_COLUMN, modifiedId)));
    final Artist peerModified = (Artist) Cayenne.objectForQuery(childContext, new ObjectIdQuery(new ObjectId("Artist", Artist.ARTIST_ID_PK_COLUMN, modifiedId)));
    modified.setArtistName("M1");
    peerModified.setArtistName("M2");
    assertEquals(PersistenceState.MODIFIED, modified.getPersistenceState());
    assertEquals(PersistenceState.MODIFIED, peerModified.getPersistenceState());
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            Persistent peerModified2 = op.merge(modified);
            assertSame(peerModified, peerModified2);
            assertEquals(PersistenceState.MODIFIED, peerModified2.getPersistenceState());
            assertEquals("M2", peerModified.getArtistName());
            assertEquals("M1", modified.getArtistName());
        }
    });
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) ObjectId(org.apache.cayenne.ObjectId) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) Persistent(org.apache.cayenne.Persistent) ObjectContext(org.apache.cayenne.ObjectContext) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Test(org.junit.Test)

Example 13 with ObjectIdQuery

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

the class QuotedIdentifiersIT method testPrefetchQuote.

@Test
public void testPrefetchQuote() throws Exception {
    DbEntity entity = context.getEntityResolver().getObjEntity(QuoteAdress.class).getDbEntity();
    List idAttributes = Collections.singletonList(entity.getAttribute("City"));
    List updatedAttributes = Collections.singletonList(entity.getAttribute("City"));
    UpdateBatchQuery updateQuery = new UpdateBatchQuery(entity, idAttributes, updatedAttributes, Collections.<String>emptySet(), 1);
    List objects3 = context.performQuery(updateQuery);
    assertEquals(0, objects3.size());
    SelectQuery qQuote_Person2 = new SelectQuery(Quote_Person.class);
    List objects4 = context.performQuery(qQuote_Person2);
    assertEquals(2, objects4.size());
    SelectQuery qQuote_Person3 = new SelectQuery(Quote_Person.class, ExpressionFactory.matchExp("salary", 100));
    List objects5 = context.performQuery(qQuote_Person3);
    assertEquals(1, objects5.size());
    SelectQuery qQuote_Person4 = new SelectQuery(Quote_Person.class, ExpressionFactory.matchExp("group", "107324"));
    List objects6 = context.performQuery(qQuote_Person4);
    assertEquals(1, objects6.size());
    SelectQuery quoteAdress1 = new SelectQuery(QuoteAdress.class, ExpressionFactory.matchExp("group", "324"));
    List objects7 = context.performQuery(quoteAdress1);
    assertEquals(1, objects7.size());
    ObjectIdQuery queryObjectId = new ObjectIdQuery(new ObjectId("QuoteAdress", QuoteAdress.GROUP.getName(), "324"));
    List objects8 = context.performQuery(queryObjectId);
    assertEquals(1, objects8.size());
    ObjectIdQuery queryObjectId2 = new ObjectIdQuery(new ObjectId("Quote_Person", "GROUP", "1111"));
    List objects9 = context.performQuery(queryObjectId2);
    assertEquals(1, objects9.size());
    SelectQuery person2Query = new SelectQuery(Quote_Person.class, ExpressionFactory.matchExp("name", "Name"));
    Quote_Person quote_Person2 = (Quote_Person) context.performQuery(person2Query).get(0);
    RelationshipQuery relationshipQuery = new RelationshipQuery(quote_Person2.getObjectId(), "address_Rel");
    List objects10 = context.performQuery(relationshipQuery);
    assertEquals(1, objects10.size());
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) DbEntity(org.apache.cayenne.map.DbEntity) ObjectId(org.apache.cayenne.ObjectId) RelationshipQuery(org.apache.cayenne.query.RelationshipQuery) UpdateBatchQuery(org.apache.cayenne.query.UpdateBatchQuery) QuoteAdress(org.apache.cayenne.testdo.quotemap.QuoteAdress) List(java.util.List) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Quote_Person(org.apache.cayenne.testdo.quotemap.Quote_Person) Test(org.junit.Test)

Example 14 with ObjectIdQuery

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

the class DataContextIT method testCurrentSnapshotWithToOneFault.

/**
 * Testing snapshot with to-one fault. This was a bug CAY-96.
 */
@Test
public void testCurrentSnapshotWithToOneFault() throws Exception {
    createGalleriesAndExhibitsDataSet();
    // Exhibit with Gallery as Fault must still include Gallery
    // Artist and Exhibit (Exhibit has unresolved to-one to gallery as in
    // the
    // CAY-96 bug report)
    ObjectId eId = new ObjectId("Exhibit", Exhibit.EXHIBIT_ID_PK_COLUMN, 2);
    Exhibit e = (Exhibit) context.performQuery(new ObjectIdQuery(eId)).get(0);
    assertTrue(e.readPropertyDirectly(Exhibit.TO_GALLERY.getName()) instanceof Fault);
    DataRow snapshot = context.currentSnapshot(e);
    // assert that after taking a snapshot, we have FK in, but the
    // relationship
    // is still a Fault
    assertTrue(e.readPropertyDirectly(Exhibit.TO_GALLERY.getName()) instanceof Fault);
    assertEquals(new Integer(33002), snapshot.get("GALLERY_ID"));
}
Also used : ObjectId(org.apache.cayenne.ObjectId) Exhibit(org.apache.cayenne.testdo.testmap.Exhibit) Fault(org.apache.cayenne.Fault) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) DataRow(org.apache.cayenne.DataRow) Test(org.junit.Test)

Example 15 with ObjectIdQuery

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

the class DataContextObjectIdQueryIT method testRefreshNullifiedValuesNew.

@Test
public void testRefreshNullifiedValuesNew() {
    Artist a = context.newObject(Artist.class);
    a.setArtistName("X");
    a.setDateOfBirth(new Date());
    context.commitChanges();
    context.performGenericQuery(new SQLTemplate(Artist.class, "UPDATE ARTIST SET DATE_OF_BIRTH = NULL"));
    long id = Cayenne.longPKForObject(a);
    ObjectIdQuery query = new ObjectIdQuery(new ObjectId("Artist", Artist.ARTIST_ID_PK_COLUMN, id), false, ObjectIdQuery.CACHE_REFRESH);
    Artist a1 = (Artist) Cayenne.objectForQuery(context, query);
    assertNull(a1.getDateOfBirth());
    assertEquals("X", a1.getArtistName());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) SQLTemplate(org.apache.cayenne.query.SQLTemplate) ObjectId(org.apache.cayenne.ObjectId) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Date(java.util.Date) Test(org.junit.Test)

Aggregations

ObjectIdQuery (org.apache.cayenne.query.ObjectIdQuery)42 Test (org.junit.Test)34 ObjectId (org.apache.cayenne.ObjectId)15 ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)11 ClientMtTable2 (org.apache.cayenne.testdo.mt.ClientMtTable2)6 ToOneFK2 (org.apache.cayenne.testdo.relationships_to_one_fk.ToOneFK2)6 UnitTestClosure (org.apache.cayenne.unit.di.UnitTestClosure)6 DataRow (org.apache.cayenne.DataRow)5 Artist (org.apache.cayenne.testdo.testmap.Artist)5 ToOneFK1 (org.apache.cayenne.testdo.relationships_to_one_fk.ToOneFK1)4 List (java.util.List)3 ObjectContext (org.apache.cayenne.ObjectContext)3 Persistent (org.apache.cayenne.Persistent)3 QueryResponse (org.apache.cayenne.QueryResponse)3 SQLTemplate (org.apache.cayenne.query.SQLTemplate)3 AbstractPerson (org.apache.cayenne.testdo.inheritance_people.AbstractPerson)3 Date (java.util.Date)2 SelectQuery (org.apache.cayenne.query.SelectQuery)2 AttributeProperty (org.apache.cayenne.reflect.AttributeProperty)2 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)2