/** * Creates and returns a replacement ObjectId. No validation of ID is done. * * @since 1.2 */ public ObjectId createReplacementId() { return new ObjectId(getEntityName(), replacementIdMap); }
/** * Returns an object matching a primary key. PK map parameter should use * database PK column names as keys. * <p> * 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. * </p> * * @see #objectForPK(ObjectContext, ObjectId) */ public static Object objectForPK(ObjectContext context, String objEntityName, Map<String, ?> pk) { if (objEntityName == null) { throw new IllegalArgumentException("Null ObjEntity name."); } return objectForPK(context, new ObjectId(objEntityName, pk)); }
/** * Returns an object matching a primary key. PK map parameter should use database PK * column names as keys. * <p> * 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. * </p> * * @see #objectForPK(ObjectContext, ObjectId) */ public static DataObject objectForPK( ObjectContext context, String objEntityName, Map pk) { if (objEntityName == null) { throw new IllegalArgumentException("Null ObjEntity name."); } return objectForPK(context, new ObjectId(objEntityName, pk)); }
/** * Creates and returns a replacement ObjectId. No validation of ID is done. * * @since 1.2 */ public ObjectId createReplacementId() { // merge existing and replaced ids to handle a replaced subset of // a compound primary key Map<String, Object> newIdMap = new HashMap<>(getIdSnapshot()); if (replacementIdMap != null) { newIdMap.putAll(replacementIdMap); } return new ObjectId(getEntityName(), newIdMap); }
static ObjectId buildId(ObjectContext context, String objEntityName, Object pk) { if (pk == null) { throw new IllegalArgumentException("Null PK"); } if (objEntityName == null) { throw new IllegalArgumentException("Null ObjEntity name."); } ObjEntity entity = context.getEntityResolver().getObjEntity(objEntityName); if (entity == null) { throw new CayenneRuntimeException("Non-existent ObjEntity: %s", objEntityName); } Collection<String> pkAttributes = entity.getPrimaryKeyNames(); if (pkAttributes.size() != 1) { throw new CayenneRuntimeException("PK contains %d columns, expected 1.", pkAttributes.size()); } String attr = pkAttributes.iterator().next(); return new ObjectId(objEntityName, attr, pk); }
static ObjectId buildId(ObjectContext context, Class<?> dataObjectClass, Object pk) { if (pk == null) { throw new IllegalArgumentException("Null PK"); } if (dataObjectClass == null) { throw new IllegalArgumentException("Null DataObject class."); } ObjEntity entity = context.getEntityResolver().getObjEntity(dataObjectClass); if (entity == null) { throw new CayenneRuntimeException("Unmapped DataObject Class: %s", dataObjectClass.getName()); } Collection<String> pkAttributes = entity.getPrimaryKeyNames(); if (pkAttributes.size() != 1) { throw new CayenneRuntimeException("PK contains %d columns, expected 1.", pkAttributes.size()); } String attr = pkAttributes.iterator().next(); return new ObjectId(entity.getName(), attr, pk); }
/** * Returns an ObjectId of an object on the other side of the to-one relationship, for * this DataRow representing a source of relationship. Returns null if snapshot FK * columns indicate a null to-one relationship. */ public ObjectId createTargetObjectId(String entityName, DbRelationship relationship) { if (relationship.isToMany()) { throw new CayenneRuntimeException("Only 'to one' can have a target ObjectId."); } Map<String, Object> target = relationship.targetPkSnapshotWithSrcSnapshot(this); return (target != null) ? new ObjectId(entityName, target) : null; }
/** * Returns an ObjectId of an object on the other side of the to-one relationship, for * this DataRow representing a source of relationship. Returns null if snapshot FK * columns indicate a null to-one relationship. */ public ObjectId createTargetObjectId(String entityName, DbRelationship relationship) { if (relationship.isToMany()) { throw new CayenneRuntimeException("Only 'to one' can have a target ObjectId."); } Map target = relationship.targetPkSnapshotWithSrcSnapshot(this); return (target != null) ? new ObjectId(entityName, target) : null; }
void registerNewObject(Persistent object, String entityName, ClassDescriptor descriptor) { /** * We should create new id only if it is not set for this object. It * could have been created, for instance, in child context */ ObjectId id = object.getObjectId(); if (id == null) { id = new ObjectId(entityName); object.setObjectId(id); } injectInitialValue(object); }
@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); } }
public List<E22> parallelFetcher() { LOGGER.info("running parallel fetcher"); return IntStream.range(0, 6).mapToObj(i -> { E22 proto = new E22(); proto.setObjectId(new ObjectId("E22", "id", i)); proto.setProp1("_" + i); return proto; }).collect(toList()); }
/** * Creates and registers a new Persistent object instance. */ public Persistent newObject(Class persistentClass) { if (persistentClass == null) { throw new NullPointerException("Persistent class can't be null."); } ObjEntity entity = getEntityResolver().lookupObjEntity(persistentClass); if (entity == null) { throw new CayenneRuntimeException("No entity mapped for class: " + persistentClass); } synchronized (graphManager) { return createNewObject(new ObjectId(entity.getName())); } }
static ObjectId buildId(ObjectContext context, String objEntityName, Object pk) { if (pk == null) { throw new IllegalArgumentException("Null PK"); } if (objEntityName == null) { throw new IllegalArgumentException("Null ObjEntity name."); } ObjEntity entity = context.getEntityResolver().getObjEntity(objEntityName); if (entity == null) { throw new CayenneRuntimeException("Non-existent ObjEntity: " + objEntityName); } DbEntity dbEntity = entity.getDbEntity(); if (dbEntity == null) { throw new CayenneRuntimeException("No DbEntity for ObjEntity: " + entity.getName()); } List pkAttributes = dbEntity.getPrimaryKey(); if (pkAttributes.size() != 1) { throw new CayenneRuntimeException("PK contains " + pkAttributes.size() + " columns, expected 1."); } DbAttribute attr = (DbAttribute) pkAttributes.get(0); return new ObjectId(objEntityName, attr.getName(), pk); }
private DataRow polymorphicRowFromCache(EntityInheritanceTree superNode, Map<String, ?> idSnapshot) { for (EntityInheritanceTree child : superNode.getChildren()) { ObjectId id = new ObjectId(child.getEntity().getName(), idSnapshot); DataRow row = cache.getCachedSnapshot(id); if (row != null) { return row; } row = polymorphicRowFromCache(child, idSnapshot); if (row != null) { return row; } } return null; }
/** * Returns an object matching a primary key. PK map parameter should use * database PK column names as keys. * <p> * 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. * </p> * * @see #objectForPK(ObjectContext, ObjectId) */ @SuppressWarnings("unchecked") public static <T> T objectForPK(ObjectContext context, Class<T> dataObjectClass, Map<String, ?> pk) { ObjEntity entity = context.getEntityResolver().getObjEntity(dataObjectClass); if (entity == null) { throw new CayenneRuntimeException("Non-existent ObjEntity for class: %s", dataObjectClass); } return (T) objectForPK(context, new ObjectId(entity.getName(), pk)); }
private Object polymorphicObjectFromCache(EntityInheritanceTree superNode, Map<String, ?> idSnapshot) { for (EntityInheritanceTree child : superNode.getChildren()) { ObjectId id = new ObjectId(child.getEntity().getName(), idSnapshot); Object object = actingContext.getGraphManager().getNode(id); if (object != null) { return object; } object = polymorphicObjectFromCache(child, idSnapshot); if (object != null) { return object; } } return null; }
/** * Returns an object matching a primary key. PK map parameter should use database PK * column names as keys. * <p> * 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. * </p> * * @see #objectForPK(ObjectContext, ObjectId) */ public static DataObject objectForPK( ObjectContext context, Class dataObjectClass, Map pk) { ObjEntity entity = context.getEntityResolver().lookupObjEntity(dataObjectClass); if (entity == null) { throw new CayenneRuntimeException("Non-existent ObjEntity for class: " + dataObjectClass); } return objectForPK(context, new ObjectId(entity.getName(), pk)); }
@Test public void testEncoder_BinaryAttribute() throws IOException { ResourceEntity<E19> descriptor = getResourceEntity(E19.class); descriptor.includeId(); descriptor.getAttributes().put( E19.GUID.getName(), getAgEntity(E19.class).getAttribute(E19.GUID.getName()) ); E19 e19 = new E19(); e19.setObjectId(new ObjectId("E19", E19.ID_PK_COLUMN, 1)); e19.setGuid("abcdefghjklmnopr".getBytes("UTF-8")); assertEquals("{\"data\":[{\"id\":1,\"guid\":\"YWJjZGVmZ2hqa2xtbm9wcg==\"}],\"total\":1}", toJson(e19, descriptor)); }
@Test public void testGetRootEncoder_ExcludedAttributes() throws IOException { // empty filter - must only include id ResourceEntity<E1> descriptor = getResourceEntity(E1.class); descriptor.includeId(); E1 e1 = new E1(); e1.setObjectId(new ObjectId("E1", E1.ID_PK_COLUMN, 777)); e1.setName("XYZ"); e1.setAge(30); e1.setDescription("test"); assertEquals("{\"data\":[{\"id\":777}],\"total\":1}", toJson(e1, descriptor)); }
@Test public void testGetRootEncoder_ExcludedRelationshipAttributes() throws IOException { ResourceEntity<E3> e3Descriptor = getResourceEntity(E3.class); e3Descriptor.includeId(); appendAttribute(e3Descriptor, E3.NAME, String.class); ResourceEntity<E2> descriptor = getResourceEntity(E2.class); descriptor.includeId(); descriptor.getChildren().put(E2.E3S.getName(), e3Descriptor); ObjectContext context = mockCayennePersister.newContext(); E2 e2 = new E2(); e2.setObjectId(new ObjectId("E2", E2.ID_PK_COLUMN, 7)); e2.setName("XYZ"); e2.setAddress("bla bla street"); context.registerNewObject(e2); E3 e31 = new E3(); e31.setObjectId(new ObjectId("E3", E3.ID_PK_COLUMN, 5)); e31.setName("31"); e31.setPhoneNumber("+12345678"); context.registerNewObject(e31); e2.addToE3s(e31); E3 e32 = new E3(); e32.setObjectId(new ObjectId("E3", E3.ID_PK_COLUMN, 6)); e32.setName("32"); e31.setPhoneNumber("+87654321"); context.registerNewObject(e32); e2.addToE3s(e32); assertEquals("{\"data\":[{\"id\":7,\"e3s\":[{\"id\":5,\"name\":\"31\"},{\"id\":6,\"name\":\"32\"}]}],\"total\":1}", toJson(e2, descriptor)); }