/** * Returns an object matching ObjectId. If this object is already cached in * the ObjectStore, it is returned without a query. Otherwise a query is * built and executed against the database. * * @return A persistent object that matched the id, null if no matching * objects were found * @throws CayenneRuntimeException * if more than one object matched ObjectId. */ public static Object objectForPK(ObjectContext context, ObjectId id) { return objectForQuery(context, new ObjectIdQuery(id, false, ObjectIdQuery.CACHE)); }
protected boolean interceptOIDQuery() { if (query instanceof ObjectIdQuery) { ObjectIdQuery oidQuery = (ObjectIdQuery) query; if (!oidQuery.isFetchMandatory() && !oidQuery.isFetchingDataRows()) { Object object = actingContext.getGraphManager().getNode( oidQuery.getObjectId()); if (object != null) { this.response = new ListResponse(object); return DONE; } } } return !DONE; }
private boolean interceptOIDQuery() { if (query instanceof ObjectIdQuery) { ObjectIdQuery oidQuery = (ObjectIdQuery) query; DataRow row = null; if (!oidQuery.isFetchMandatory()) { row = cache.getCachedSnapshot(oidQuery.getObjectId()); } // 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; }
/** * An object is considered equal to this query if it is also a SingleObjectQuery with * an equal ObjectId. */ public boolean equals(Object object) { if (this == object) { return true; } if (!(object instanceof ObjectIdQuery)) { return false; } ObjectIdQuery query = (ObjectIdQuery) object; return Util.nullSafeEquals(objectId, query.getObjectId()); }
/** * An object is considered equal to this query if it is also a ObjectIdQuery with * an equal ObjectId. */ @Override public boolean equals(Object object) { if (this == object) { return true; } if (!(object instanceof ObjectIdQuery)) { return false; } ObjectIdQuery query = (ObjectIdQuery) object; return Util.nullSafeEquals(objectId, query.getObjectId()); }
/** * Returns an object matching ObjectId. If this object is already cached in the * ObjectStore, it is returned without a query. Otherwise a query is built and * executed against the database. * * @return A DataObject that matched the id, null if no matching objects were found * @throws CayenneRuntimeException if more than one object matched ObjectId. */ public static DataObject objectForPK(ObjectContext context, ObjectId id) { return (DataObject) DataObjectUtils.objectForQuery(context, new ObjectIdQuery( id, false, ObjectIdQuery.CACHE)); }
protected boolean interceptOIDQuery() { if (query instanceof ObjectIdQuery) { ObjectIdQuery oidQuery = (ObjectIdQuery) query; if (!oidQuery.isFetchMandatory() && !oidQuery.isFetchingDataRows()) { Object object = polymorphicObjectFromCache( oidQuery.getObjectId()); if (object != null) { // do not return hollow objects if (((Persistent) object).getPersistenceState() == PersistenceState.HOLLOW) { return !DONE; } this.response = new ListResponse(object); return DONE; } } } return !DONE; }
ObjectId oid = oidQuery.getObjectId(); if (cache != null && !oidQuery.isFetchMandatory()) { row = polymorphicRowFromCache(oid); if (oidQuery.isFetchAllowed()) {
public void setById(ObjectContext context, Object entity, Object foreignId) throws Exception { IdCoder idCoder = new IdCoder(context.getEntityResolver()); ObjectId objectId = idCoder.getObjectId(String.valueOf(foreignId)); ObjectIdQuery query = new ObjectIdQuery(objectId); List<?> list = context.performQuery(query); Object newForeignObject = list.size() == 0 ? null : list.get(0); setter.invoke(entity, newForeignObject); }
/** * Overrides super implementation to property handle data row fetches. */ protected boolean interceptOIDQuery() { if (query instanceof ObjectIdQuery) { ObjectIdQuery oidQuery = (ObjectIdQuery) query; if (!oidQuery.isFetchMandatory()) { Object object = actingContext.getGraphManager().getNode( oidQuery.getObjectId()); if (object != null) { if (oidQuery.isFetchingDataRows()) { object = ((DataContext) actingContext) .currentSnapshot((DataObject) object); } this.response = new ListResponse(object); return DONE; } } } return !DONE; }
/** * Returns a snapshot for ObjectId from the underlying snapshot cache. If cache * contains no snapshot, it will attempt fetching it using provided QueryEngine. If * fetch attempt fails or inconsistent data is returned, underlying cache will throw a * CayenneRuntimeException. * * @since 1.2 */ public synchronized DataRow getSnapshot(ObjectId oid) { if (context != null && context.getChannel() != null) { ObjectIdQuery query = new ObjectIdQuery(oid, true, ObjectIdQuery.CACHE); List<?> results = context.getChannel().onQuery(context, query).firstList(); return results.isEmpty() ? null : (DataRow) results.get(0); } else { return null; } }
/** * Overrides super implementation to property handle data row fetches. */ @Override protected boolean interceptOIDQuery() { if (query instanceof ObjectIdQuery) { ObjectIdQuery oidQuery = (ObjectIdQuery) query; if (!oidQuery.isFetchMandatory()) { Object object = polymorphicObjectFromCache(oidQuery.getObjectId()); if (object != null) { // TODO: andrus, 10/14/2006 - obtaining a row from an object is the // only piece that makes this method different from the super // implementation. This is used in NEW objects sorting on insert. It // would be nice to implement an alternative algorithm that wouldn't // require this hack. if (oidQuery.isFetchingDataRows()) { object = actingDataContext.currentSnapshot((Persistent) object); } // do not return hollow objects else if (((Persistent) object).getPersistenceState() == PersistenceState.HOLLOW) { return !DONE; } this.response = new ListResponse(object); return DONE; } } } return !DONE; }
Query query = new ObjectIdQuery(oid, true, ObjectIdQuery.CACHE_REFRESH); List results = channel.onQuery(null, query).firstList();
/** * Returns a snapshot for ObjectId from the underlying snapshot cache. If cache * contains no snapshot, it will attempt fetching it using provided QueryEngine. If * fetch attempt fails or inconsistent data is returned, underlying cache will throw a * CayenneRuntimeException. * * @since 1.2 */ public synchronized DataRow getSnapshot(ObjectId oid) { if (context != null && context.getChannel() != null) { ObjectIdQuery query = new ObjectIdQuery(oid, true, ObjectIdQuery.CACHE); List results = context.getChannel().onQuery(context, query).firstList(); return results.isEmpty() ? null : (DataRow) results.get(0); } else { return null; } }
for (Object listItemId : listItemIds) { ObjectId objectId = idCoder.getObjectId(String.valueOf(listItemId)); ObjectIdQuery query = new ObjectIdQuery(objectId); List<?> list = context.performQuery(query); Object newForeignObject = list.size() == 0 ? null : list.get(0);
/** * Resolves an object if it is HOLLOW. */ public void prepareForAccess(Persistent object, String property) { if (object.getPersistenceState() == PersistenceState.HOLLOW) { ObjectId gid = object.getObjectId(); List objects = performQuery(new ObjectIdQuery(gid)); if (objects.size() == 0) { throw new FaultFailureException( "Error resolving fault, no matching row exists in the database for GlobalID: " + gid); } else if (objects.size() > 1) { throw new FaultFailureException( "Error resolving fault, more than one row exists in the database for GlobalID: " + gid); } } }
/** * Get a persistent entity based on an id for a specific class. CayennePersistenceManager * use a string representation of Cayenne's ObjectId to identify individual objects. */ private Object getEntity(Object objectContext, Class<?> clazz, Object id) { try { ObjectContext context = objectContext == null ? this.getReadOnlyObjectContext() : (ObjectContext) objectContext; EntityResolver entityResolver = this.factory.getDataDomain().getEntityResolver(); IdCoder idCoder = new IdCoder(entityResolver); ObjectId objectId = idCoder.getObjectId((String) id); ObjectIdQuery query = new ObjectIdQuery(objectId); List<?> list = context.performQuery(query); return list.size() == 0 ? null : list.get(0); } catch (Exception e) { return null; } }
@SuppressWarnings("unchecked") private Object getOptionalExistingObject(Class<?> type, ObjectContext context, Object id) { ObjEntity entity = context.getEntityResolver().getObjEntity(type); // sanity checking... if (entity == null) { throw new AgException(Response.Status.INTERNAL_SERVER_ERROR, "Unknown entity class: " + type); } // TODO: should we start using optimistic locking on PK by default // instead of SELECT/DELETE|UPDATE? String idName = entity.getPrimaryKeyNames().iterator().next(); ObjectIdQuery select = new ObjectIdQuery(new ObjectId(entity.getName(), idName, id)); return Cayenne.objectForQuery(context, select); } }
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; // this can happen if a NEW object is deleted and after that its // relationships are // 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); }
ObjectId id = new EntityIdCoder(entity).toObjectId(uuid); Object object = Cayenne.objectForQuery(context, new ObjectIdQuery(id)); if (object == null) { return Collections.emptyMap();