Search in sources :

Example 36 with ObjectIdQuery

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

the class ObjectStore method getCachedSnapshot.

/**
 * Returns a snapshot for ObjectId from the underlying snapshot cache. If cache
 * contains no snapshot, a null is returned.
 *
 * @since 1.1
 */
public DataRow getCachedSnapshot(ObjectId oid) {
    if (context != null && context.getChannel() != null) {
        ObjectIdQuery query = new CachedSnapshotQuery(oid);
        List<?> results = context.getChannel().onQuery(context, query).firstList();
        return results.isEmpty() ? null : (DataRow) results.get(0);
    } else {
        return null;
    }
}
Also used : ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery)

Example 37 with ObjectIdQuery

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

the class ObjectIdBatchFault method fetchObjects.

private Map<String, Object> fetchObjects() {
    if (sources == null) {
        return Collections.emptyMap();
    }
    EntityResolver resolver = context.getEntityResolver();
    // reasons
    if (sources.size() == 1) {
        String uuid = sources.get(0).getId();
        String entityName = EntityIdCoder.getEntityName(uuid);
        ObjEntity entity = resolver.getObjEntity(entityName);
        ObjectId id = new EntityIdCoder(entity).toObjectId(uuid);
        Object object = Cayenne.objectForQuery(context, new ObjectIdQuery(id));
        if (object == null) {
            return Collections.emptyMap();
        } else {
            return Collections.singletonMap(uuid, object);
        }
    }
    Map<String, SelectQuery<DataObject>> queriesByEntity = new HashMap<>();
    Map<String, EntityIdCoder> codersByEntity = new HashMap<>();
    for (ObjectIdBatchSourceItem source : sources) {
        String uuid = source.getId();
        String entityName = EntityIdCoder.getEntityName(uuid);
        EntityIdCoder coder = codersByEntity.get(entityName);
        SelectQuery<DataObject> query;
        if (coder == null) {
            coder = new EntityIdCoder(resolver.getObjEntity(entityName));
            codersByEntity.put(entityName, coder);
            query = new SelectQuery<>(entityName);
            queriesByEntity.put(entityName, query);
        } else {
            query = queriesByEntity.get(entityName);
        }
        ObjectId id = coder.toObjectId(uuid);
        Expression idExp = ExpressionFactory.matchAllDbExp(id.getIdSnapshot(), Expression.EQUAL_TO);
        query.orQualifier(idExp);
    }
    int capacity = (int) Math.ceil(sources.size() / 0.75d);
    Map<String, Object> results = new HashMap<>(capacity);
    for (SelectQuery<DataObject> query : queriesByEntity.values()) {
        EntityIdCoder coder = codersByEntity.get(query.getRoot());
        @SuppressWarnings("unchecked") List<DataObject> objects = context.performQuery(query);
        for (DataObject object : objects) {
            String uuid = coder.toStringId(object.getObjectId());
            results.put(uuid, object);
        }
    }
    return results;
}
Also used : EntityIdCoder(org.apache.cayenne.lifecycle.id.EntityIdCoder) ObjectId(org.apache.cayenne.ObjectId) HashMap(java.util.HashMap) EntityResolver(org.apache.cayenne.map.EntityResolver) SelectQuery(org.apache.cayenne.query.SelectQuery) ObjEntity(org.apache.cayenne.map.ObjEntity) DataObject(org.apache.cayenne.DataObject) Expression(org.apache.cayenne.exp.Expression) DataObject(org.apache.cayenne.DataObject) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery)

Example 38 with ObjectIdQuery

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

the class AshwoodEntitySorter method findReflexiveMaster.

protected Object findReflexiveMaster(Persistent object, ObjRelationship toOneRel, String targetEntityName) {
    DbRelationship finalRel = toOneRel.getDbRelationships().get(0);
    ObjectContext context = object.getObjectContext();
    // the method
    if (object.getObjectId().isTemporary()) {
        return null;
    }
    ObjectIdQuery query = new ObjectIdQuery(object.getObjectId(), true, ObjectIdQuery.CACHE);
    QueryResponse response = context.getChannel().onQuery(null, query);
    List<?> result = response.firstList();
    if (result == null || result.size() == 0) {
        return null;
    }
    DataRow snapshot = (DataRow) result.get(0);
    ObjectId id = snapshot.createTargetObjectId(targetEntityName, finalRel);
    // hollow objects
    return (id != null) ? context.getGraphManager().getNode(id) : null;
}
Also used : ObjectId(org.apache.cayenne.ObjectId) DbRelationship(org.apache.cayenne.map.DbRelationship) QueryResponse(org.apache.cayenne.QueryResponse) ObjectContext(org.apache.cayenne.ObjectContext) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) DataRow(org.apache.cayenne.DataRow)

Example 39 with ObjectIdQuery

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

the class CayenneContextWithDataContextIT method testOIDQueryInterception.

@Test
public void testOIDQueryInterception() throws Exception {
    final ClientMtTable1 o = clientContext.newObject(ClientMtTable1.class);
    o.setGlobalAttribute1("aaa");
    // fetch new
    final ObjectIdQuery q1 = new ObjectIdQuery(o.getObjectId(), false, ObjectIdQuery.CACHE);
    clientServerInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            List<?> objects = clientContext.performQuery(q1);
            assertEquals(1, objects.size());
            assertSame(o, objects.get(0));
        }
    });
    clientContext.commitChanges();
    // fetch committed
    final ObjectIdQuery q2 = new ObjectIdQuery(o.getObjectId(), false, ObjectIdQuery.CACHE);
    clientServerInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            List<?> objects = clientContext.performQuery(q2);
            assertEquals(1, objects.size());
            assertSame(o, objects.get(0));
        }
    });
}
Also used : ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) RemoteIncrementalFaultList(org.apache.cayenne.remote.RemoteIncrementalFaultList) List(java.util.List) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Test(org.junit.Test)

Example 40 with ObjectIdQuery

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

the class PersistentObjectInContextIT method testToOneRelationship.

@Test
public void testToOneRelationship() 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);
    assertEquals("g1", mtTable1.getGlobalAttribute1());
}
Also used : ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ClientMtTable2(org.apache.cayenne.testdo.mt.ClientMtTable2) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) 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