@Override public void addWithDeferredIdAllocation(final FullEntity<?>... entities) { raw.addWithDeferredIdAllocation(entities); }
@Override public void run(Transaction tx, Key userKey, String content) { Entity user = tx.get(userKey); if (user == null) { System.out.println("Adding a new user."); user = Entity.newBuilder(userKey).set("count", 1).build(); tx.add(user); } else { user = Entity.newBuilder(user).set("count", user.getLong("count") + 1L).build(); tx.update(user); } IncompleteKey commentKey = IncompleteKey.newBuilder(userKey, COMMENT_KIND).build(); FullEntity<IncompleteKey> comment = FullEntity.newBuilder(commentKey) .set("content", content) .set("timestamp", Timestamp.now()) .build(); tx.addWithDeferredIdAllocation(comment); System.out.printf("Adding a comment to user '%s'.%n", userKey.getName()); }
/** Example of adding multiple entities with deferred id allocation. */ // [TARGET addWithDeferredIdAllocation(FullEntity...)] public List<Key> multipleAddEntitiesDeferredId() { Datastore datastore = transaction.getDatastore(); // [START multipleAddEntitiesDeferredId] IncompleteKey key1 = datastore.newKeyFactory().setKind("MyKind").newKey(); FullEntity.Builder entityBuilder1 = FullEntity.newBuilder(key1); entityBuilder1.set("propertyName", "value1"); FullEntity entity1 = entityBuilder1.build(); IncompleteKey key2 = datastore.newKeyFactory().setKind("MyKind").newKey(); FullEntity.Builder entityBuilder2 = FullEntity.newBuilder(key2); entityBuilder2.set("propertyName", "value2"); FullEntity entity2 = entityBuilder2.build(); transaction.addWithDeferredIdAllocation(entity1, entity2); Response response = transaction.commit(); // [END multipleAddEntitiesDeferredId] return response.getGeneratedKeys(); }
@Override public void addWithDeferredIdAllocation(FullEntity<?>... entities) { transaction().addWithDeferredIdAllocation(entities); }
@Override public <E> void insertWithDeferredIdAllocation(List<E> entities) { if (entities == null || entities.isEmpty()) { return; } try { DatastoreUtils.validateDeferredIdAllocation(entities.get(0)); FullEntity<?>[] nativeEntities = toNativeFullEntities(entities, entityManager, Intent.INSERT); nativeTransaction.addWithDeferredIdAllocation(nativeEntities); } catch (DatastoreException exp) { throw DatastoreUtils.wrap(exp); } }
@Override public <E> void insertWithDeferredIdAllocation(E entity) { try { DatastoreUtils.validateDeferredIdAllocation(entity); FullEntity<?> nativeEntity = (FullEntity<?>) Marshaller.marshal(entityManager, entity, Intent.INSERT); nativeTransaction.addWithDeferredIdAllocation(nativeEntity); } catch (DatastoreException exp) { throw DatastoreUtils.wrap(exp); } }