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;
}
}
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;
}
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;
}
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));
}
});
}
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());
}
Aggregations