Search code examples
google-cloud-datastorenon-relational-database

Unable to delete persistent objects from Google App Engine datastore


I have 2 classes AAA and BBB, where BBB contains a field of AAA type.

When I call makePersistent() on 10 AAA and 10 BBB objects, the datastore ends up with 20 AAA objects and 10 BBB objects. I understand this is normal since GAE's datastore is non-relational.

However, when I try to deletePersistentAll using the following,

pm.newQuery(BBB.class).deletePersistentAll();
pm.newQuery(AAA.class).deletePersistentAll();

All BBB objects get removed as expected, but all 20 AAA objects remain. Is there something I'm missing?


Solution

  • Isolation and Consistency
    The datastore's isolation level outside of transactions is closest to READ_COMMITTED.

    These tests pass.

    PersistenceManager pm = pmfInstance.getPersistenceManager();
    
    // make 10 AAA objects
    for (int i = 0; i < 10; i++) {
        pm.makePersistent(new AAA());
    }
    
    // make 10 BBB objects
    for (int i = 0; i < 10; i++) {
        pm.makePersistent(new BBB(new AAA()));
    }
    
    // make sure we have 20 AAA's
    @SuppressWarnings("unchecked")
    List<AAA> aaa = (List<AAA>) pm.newQuery(AAA.class).execute();
    
    assertEquals(20, aaa.size());
    
    // make sure we have 10 BBB's
    List<BBB> bbb = (List<BBB>) pm.newQuery(BBB.class).execute();
    
    assertEquals(10, bbb.size());
    
    // try to delete
    pm.newQuery(BBB.class).deletePersistentAll();
    bbb = (List<BBB>) pm.newQuery(BBB.class).execute();
    assertEquals(0, bbb.size());
    
    pm.newQuery(AAA.class).deletePersistentAll();
    aaa = (List<AAA>) pm.newQuery(AAA.class).execute();
    assertEquals(0, aaa.size());
    assertEquals(0, bbb.size());