Search in sources :

Example 31 with ObjectIdQuery

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

the class CDOOneToOneFKIT method testReplaceNull1.

@Test
public void testReplaceNull1() throws Exception {
    ToOneFK2 src = context.newObject(ToOneFK2.class);
    context.commitChanges();
    // test database data
    ObjectIdQuery refetch = new ObjectIdQuery(src.getObjectId(), false, ObjectIdQuery.CACHE_REFRESH);
    ToOneFK2 src2 = (ToOneFK2) Cayenne.objectForQuery(context1, refetch);
    assertEquals(src.getObjectId(), src2.getObjectId());
    // *** TESTING THIS ***
    src2.setToOneToFK(null);
    assertNull(src2.getToOneToFK());
}
Also used : ToOneFK2(org.apache.cayenne.testdo.relationships_to_one_fk.ToOneFK2) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Test(org.junit.Test)

Example 32 with ObjectIdQuery

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

the class CDOOneToOneFKIT method testReplaceNull2.

@Test
public void testReplaceNull2() throws Exception {
    ToOneFK2 src = context.newObject(ToOneFK2.class);
    context.commitChanges();
    ToOneFK1 target = context.newObject(ToOneFK1.class);
    // *** TESTING THIS ***
    src.setToOneToFK(target);
    // test before save
    assertSame(target, src.getToOneToFK());
    // do save
    context.commitChanges();
    // test database data
    ObjectIdQuery refetch = new ObjectIdQuery(src.getObjectId(), false, ObjectIdQuery.CACHE_REFRESH);
    ToOneFK2 src2 = (ToOneFK2) Cayenne.objectForQuery(context1, refetch);
    ToOneFK1 target2 = src2.getToOneToFK();
    assertNotNull(target2);
    assertEquals(src.getObjectId(), src2.getObjectId());
    assertEquals(target.getObjectId(), target2.getObjectId());
}
Also used : ToOneFK2(org.apache.cayenne.testdo.relationships_to_one_fk.ToOneFK2) ToOneFK1(org.apache.cayenne.testdo.relationships_to_one_fk.ToOneFK1) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Test(org.junit.Test)

Example 33 with ObjectIdQuery

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

the class CDOOneToOneFKIT method testNewAdd.

@Test
public void testNewAdd() throws Exception {
    ToOneFK2 src = context.newObject(ToOneFK2.class);
    ToOneFK1 target = context.newObject(ToOneFK1.class);
    // *** TESTING THIS ***
    src.setToOneToFK(target);
    // test before save
    assertSame(target, src.getToOneToFK());
    // do save
    context.commitChanges();
    // test database data
    ObjectIdQuery refetch = new ObjectIdQuery(src.getObjectId(), false, ObjectIdQuery.CACHE_REFRESH);
    ToOneFK2 src2 = (ToOneFK2) Cayenne.objectForQuery(context1, refetch);
    ToOneFK1 target2 = src2.getToOneToFK();
    assertNotNull(target2);
    assertEquals(src.getObjectId(), src2.getObjectId());
    assertEquals(target.getObjectId(), target2.getObjectId());
}
Also used : ToOneFK2(org.apache.cayenne.testdo.relationships_to_one_fk.ToOneFK2) ToOneFK1(org.apache.cayenne.testdo.relationships_to_one_fk.ToOneFK1) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Test(org.junit.Test)

Example 34 with ObjectIdQuery

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

the class ChildDiffLoader method findObject.

protected Persistent findObject(Object nodeId) {
    // first do a lookup in ObjectStore; if even a hollow object is found,
    // return it;
    // if not - fetch.
    Persistent object = (Persistent) context.getGraphManager().getNode(nodeId);
    if (object != null) {
        return object;
    }
    ObjectId id = (ObjectId) nodeId;
    // modified
    if (id.isTemporary()) {
        return null;
    }
    // skip context cache lookup, go directly to its channel
    Query query = new ObjectIdQuery((ObjectId) nodeId);
    QueryResponse response = context.getChannel().onQuery(context, query);
    List<?> objects = response.firstList();
    if (objects.size() == 0) {
        throw new CayenneRuntimeException("No object for ID exists: %s", nodeId);
    } else if (objects.size() > 1) {
        throw new CayenneRuntimeException("Expected zero or one object, instead query matched: %d", objects.size());
    }
    return (Persistent) objects.get(0);
}
Also used : ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Query(org.apache.cayenne.query.Query) ObjectId(org.apache.cayenne.ObjectId) QueryResponse(org.apache.cayenne.QueryResponse) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) Persistent(org.apache.cayenne.Persistent) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery)

Example 35 with ObjectIdQuery

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

the class DataDomainQueryAction method interceptOIDQuery.

private boolean interceptOIDQuery() {
    if (query instanceof ObjectIdQuery) {
        ObjectIdQuery oidQuery = (ObjectIdQuery) query;
        ObjectId oid = oidQuery.getObjectId();
        // translate and run the query. See for instance CAY-1651
        if (oid.isTemporary() && !oid.isReplacementIdAttached()) {
            response = new ListResponse();
            return DONE;
        }
        DataRow row = null;
        if (cache != null && !oidQuery.isFetchMandatory()) {
            row = polymorphicRowFromCache(oid);
        }
        // refresh is forced or not found in cache
        if (row == null) {
            if (oidQuery.isFetchAllowed()) {
                runQueryInTransaction();
            } else {
                response = new ListResponse();
            }
        } else {
            response = new ListResponse(row);
        }
        return DONE;
    }
    return !DONE;
}
Also used : ListResponse(org.apache.cayenne.util.ListResponse) ObjectId(org.apache.cayenne.ObjectId) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) DataRow(org.apache.cayenne.DataRow)

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