1

My app-engine server create an entity from client data and returns(via RPC callback) the entity's id derived from it's key. In another event I am trying [and failing] to delete the entity based on the id i.e. create a key from the id and delete the original entity.

Is it possible to reproduce the original key from the id?

If not how should I go about deleting the Entity from the client?

Here is my RemoteServiceServlet:

import java.util.Date; import java.util.List;

import gid.mff.item_manager.client.I_MFF_ItemService; import gid.mff.item_manager.client.mffobjects.MFF_Item1;

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.FetchOptions; import com.google.appengine.api.datastore.Key; import com.google.appengine.api.datastore.KeyFactory; import com.google.appengine.api.datastore.Query; import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class ItemServiceImpl extends RemoteServiceServlet implements I_MFF_ItemService { private static final String MFF_ITEM1_ROOT_KEY = "MFF_Item1_Root_Key";

private final static String LOG = "I_MFF_ItemService";

@Override
public MFF_Item1 getItem(long id) 
{
    //TODO get one from the data store
    System.out.println("the id is: " + id);

    MFF_Item1 m = new MFF_Item1();
    m.setTitle("corck");
    m.setId(10);
    return m;
}

@Override
public Long createItem(MFF_Item1 mff_Item1) 
{
    Key itemListKey = KeyFactory.createKey("MffItems", MFF_ITEM1_ROOT_KEY);
    Date date = new Date();

    Entity item = new Entity("Item1", itemListKey);
    item.setProperty("date", date);
    item.setProperty("header", mff_Item1.getTitle());

    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    datastore.put(item);

   printItems(datastore, itemListKey);

    return item.getKey().getId();
}

private void printItems(DatastoreService datastore, Key itemListKey) 
{
     Query query = new Query("Item1", itemListKey).addSort("header", Query.SortDirection.DESCENDING);
        List<Entity> items = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(40));

        for(Entity e: items)
        {
          System.out.print("The Header is: " + e.getProperty("header"));
          System.out.println("   The key id is: " + e.getKey().getId());
        }

}

@Override
public boolean deleteItem(Long id) 
{
    System.out.println(LOG +" delete item with this id: " + id);

    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Key key = KeyFactory.createKey("MffItems", id);

    datastore.delete(key);


    // TODO Auto-generated method stub
    return true;
}

}

4

1 回答 1

3

You can use the generating keys option to get the key of an entity using its kind and id.

In your case, while creating the entity you have used the constructor

new Entity(kind,Key) 

This will create an entity of the given kind and with a parent of the given key and a numeric ID that will be auto-generated by datastore. Thus the Key will be used as ancestor key and not as entity key

Now for deleting the entity, you are creating the key using the option

createKey(kind, id)

This will create a key having no parent and of the given kind and id. But for accessing your entity correctly, you should be using :

createKey(Key, kind, id)

This will create a key with the given ancestor key, kind and id

If initially while creating an entity, you intended to use the key you generated, as the entity key and not as an ancestor key, then you should be using:

new Entity(Key)

Check entity constructor details and KeyFactory method to understand this better.

EDIT: The above solution has worked as per Rubber Duck and so adding below the corrected steps used by him to access the required entity for deletion

Create a key for the parent: Key parent = KeyFactory.createKey("MffItems", MFF_ITEM1_ROOT_KEY);

Create a key for the entity using the created parent's key, kind and id : Key key = KeyFactory.createKey(parent, "Item1", id);

Then use this key to delete the required entity

于 2013-06-25T04:35:49.240 回答