Search in sources :

Example 11 with DatastoreException

use of com.google.cloud.datastore.DatastoreException in project google-cloud-java by GoogleCloudPlatform.

the class DatastoreSnippets method batchAddEntities.

/**
   * Example of adding multiple entities.
   */
// [TARGET add(FullEntity...)]
// [VARIABLE "my_key_name1"]
// [VARIABLE "my_key_name2"]
public void batchAddEntities(String keyName1, String keyName2) {
    // [START batchAddEntities]
    Key key1 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName1);
    Entity.Builder entityBuilder1 = Entity.newBuilder(key1);
    entityBuilder1.set("propertyName", "value1");
    Entity entity1 = entityBuilder1.build();
    Key key2 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName2);
    Entity.Builder entityBuilder2 = Entity.newBuilder(key2);
    entityBuilder2.set("propertyName", "value2");
    Entity entity2 = entityBuilder2.build();
    try {
        datastore.add(entity1, entity2);
    } catch (DatastoreException ex) {
        if ("ALREADY_EXISTS".equals(ex.getReason())) {
        // at least one of entity1.getKey() and entity2.getKey() already exists
        }
    }
// [END batchAddEntities]
}
Also used : Entity(com.google.cloud.datastore.Entity) DatastoreException(com.google.cloud.datastore.DatastoreException) Key(com.google.cloud.datastore.Key) IncompleteKey(com.google.cloud.datastore.IncompleteKey)

Example 12 with DatastoreException

use of com.google.cloud.datastore.DatastoreException in project google-cloud-java by GoogleCloudPlatform.

the class TransactionSnippets method commit.

/**
   * Example of committing a transaction.
   */
// [TARGET commit()]
public Key commit() {
    Datastore datastore = transaction.getDatastore();
    // [START commit]
    // create an entity
    KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind");
    Key key = datastore.allocateId(keyFactory.newKey());
    Entity entity = Entity.newBuilder(key).set("description", "commit()").build();
    // add the entity and commit
    try {
        transaction.put(entity);
        transaction.commit();
    } catch (DatastoreException ex) {
    // handle exception
    }
    return key;
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) Entity(com.google.cloud.datastore.Entity) Datastore(com.google.cloud.datastore.Datastore) DatastoreException(com.google.cloud.datastore.DatastoreException) KeyFactory(com.google.cloud.datastore.KeyFactory) Key(com.google.cloud.datastore.Key) IncompleteKey(com.google.cloud.datastore.IncompleteKey)

Example 13 with DatastoreException

use of com.google.cloud.datastore.DatastoreException in project google-cloud-java by GoogleCloudPlatform.

the class ITDatastoreTest method testUpdate.

@Test
public void testUpdate() {
    List<Entity> keys = DATASTORE.fetch(ENTITY1.getKey(), ENTITY3.getKey());
    assertEquals(ENTITY1, keys.get(0));
    assertNull(keys.get(1));
    assertEquals(2, keys.size());
    try {
        DATASTORE.update(ENTITY3);
        fail("Expecting a failure");
    } catch (DatastoreException expected) {
    // expected;
    }
    DATASTORE.add(ENTITY3);
    assertEquals(ENTITY3, DATASTORE.get(ENTITY3.getKey()));
    Entity entity3 = Entity.newBuilder(ENTITY3).clear().set("bla", new NullValue()).build();
    assertNotEquals(ENTITY3, entity3);
    DATASTORE.update(entity3);
    assertEquals(entity3, DATASTORE.get(ENTITY3.getKey()));
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) Entity(com.google.cloud.datastore.Entity) ProjectionEntity(com.google.cloud.datastore.ProjectionEntity) NullValue(com.google.cloud.datastore.NullValue) DatastoreException(com.google.cloud.datastore.DatastoreException) Test(org.junit.Test)

Aggregations

DatastoreException (com.google.cloud.datastore.DatastoreException)13 Entity (com.google.cloud.datastore.Entity)11 FullEntity (com.google.cloud.datastore.FullEntity)8 ProjectionEntity (com.google.cloud.datastore.ProjectionEntity)8 Test (org.junit.Test)8 IncompleteKey (com.google.cloud.datastore.IncompleteKey)5 Key (com.google.cloud.datastore.Key)4 Transaction (com.google.cloud.datastore.Transaction)4 UnwriteableSessionDataException (org.eclipse.jetty.server.session.UnwriteableSessionDataException)2 Batch (com.google.cloud.datastore.Batch)1 Blob (com.google.cloud.datastore.Blob)1 Datastore (com.google.cloud.datastore.Datastore)1 KeyFactory (com.google.cloud.datastore.KeyFactory)1 NullValue (com.google.cloud.datastore.NullValue)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 SessionData (org.eclipse.jetty.server.session.SessionData)1 UnreadableSessionDataException (org.eclipse.jetty.server.session.UnreadableSessionDataException)1 ClassLoadingObjectInputStream (org.eclipse.jetty.util.ClassLoadingObjectInputStream)1