xybrek xybrek - 1 year ago 65
Java Question

Cannot Delete Entity by Long Id

I have this JUnit code:

public void test(){
Entity e = new Entity(KeyFactory.createKey("Post", 1L))
Key key = _ds.put(e); // _ds is native DatastoreService
_ds.delete(KeyFactory.createKey("Post", 1L));
Entity deleted = _ds.get(KeyFactory.createKey("Post", 1L));
assertNull(deleted); // Not null, why?

What could be the problem that
cannot get deleted?

I have tried to use the
(generated by the put) as Key to the:

_ds.delete(key); // won't delete also?

(Although what I really need to to find out why it won't delete by Long id)

Answer Source

I modified your test slightly (to compile) and it is working for me:

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;

public class StackoverflowTest {

    private final LocalServiceTestHelper helper = new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());

    public void setUp() {

    public void tearDown() {

    private final DatastoreService _ds = DatastoreServiceFactory.getDatastoreService();

     * http://stackoverflow.com/questions/28031142/cannot-delete-entity-by-long-id
    @Test(expected = EntityNotFoundException.class)
    public void test28031142() throws EntityNotFoundException {
        Entity e = new Entity(KeyFactory.createKey("Post", 1L));
        Key key = _ds.put(e); // _ds is native DatastoreService
        _ds.delete(KeyFactory.createKey("Post", 1L));
        _ds.get(KeyFactory.createKey("Post", 1L));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download