سؤال

I'm trying to delete an entity from my datastore using objectify but doesn't seem to be deleted even after shutting down the instance and restarting it. This is what the entity looks like in the datastore (both when it's on the production server & dev server):

enter image description here

This is the code i'm using to try and delete it:

@ApiMethod(name = "deleteDataVersion")
public Result deleteDataVersion(@Named("id") String id) {

    // Where id is the id of the entity in the datastore.

    if (id != null && !id.equals("")) {
        ofy().delete().type(DataVersion.class).id(id).now();
        return new Result(Result.STATUS_SUCCESS);
    } else
        return new Result(Result.STATUS_FAILED);
}

I've also tried this code:

@ApiMethod(name = "deleteDataVersion")
public Result deleteDataVersion(@Named("id") String id) {

    if (id != null && !id.equals("")) {

        // DataVersion doesn't have a parent.
        Key<DataVersion> key = Key.create(null, DataVersion.class, id);

        ofy().delete().key(key).now();
        return new Result(Result.STATUS_SUCCESS);
    } else
        return new Result(Result.STATUS_FAILED);
}

But the entity never gets deleted. This is the code for my entity:

@Entity
public class DataVersion {

    @Id
    private Long id;
    String folderName;
    @Index
    String effective;

    public DataVersion() {
    }

    public DataVersion(String folderName, String effective ) {
        this.folderName= folderName;
        this.effective = effective;
    }

    // Getters & setters..
}

I just can't seem to find the problem :( Any help would be greatly appreciated! I'm sure it's something minor I'm overlooking (fairly new to Objectify/AppEngine).

هل كانت مفيدة؟

المحلول

The ID you have in parameter in your Endpoint is a String, and you try to delete the object DataVersion where the ID is a Long.

ofy().delete().type(DataVersion.class).id(Long.valueOf(id)).now();

would work better !

نصائح أخرى

First get the key.
Key<DataVersion> key = Key.create(null, DataVersion.class, id);

Then fetch the entity from the database using the key.
DataVersion dataVersion = ofy().load().key(key).now();

Then delete the entity using objectify.
ofy().delete().entity(dataVersion).now();

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top