Hesam
Hesam

Reputation: 53600

How to delete entity by Id from data-store in AppEngine?

I created an API to delete an Entity by its key however I'm gettin Http 204 and Entity does not delete from data-store.

This is my API,

@ApiMethod(name = "deleteContact", path = "contact", httpMethod = ApiMethod.HttpMethod.DELETE)
    public void deleteContact(final @Named("id") long contactId)
    {
        ofy().delete().type(Contact.class).id(contactId).now();
    }

and my Contact class is like this:

@Entity
@Cache
public class Contact
{
    @Id
    private long id;
    @Index
    private String cName;
    private Email cEmail;
    private PhoneNumber cPhoneNumber;

    // private key, to connect this Entity to Profile Entity
    @Parent
    @ApiResourceProperty(ignored = AnnotationBoolean.TRUE)
    private Key<Profile> profileKey;
    @ApiResourceProperty(ignored = AnnotationBoolean.TRUE)
    private String profileId;

    // default constructor is private
    private Contact()
    {

    }

    public Contact(final long id, final String profileId, final ContactForm contactForm)
    {
        Preconditions.checkNotNull(contactForm.getUserName(), "The name is required");

        this.id = id;
        this.profileKey = Key.create(Profile.class, profileId);
        this.profileId = profileId;

        updateWithContactForm(contactForm);
    }

    /**
     * Updates the Contact with ContactForm.
     * This method is used upon object creation as well as updating existing Contact.
     *
     * @param contactForm contains form data sent from the client.
     */
    public void updateWithContactForm(final ContactForm contactForm)
    {
        this.cName = contactForm.getUserName();
        this.cEmail = contactForm.getUserEmailAddress();
        this.cPhoneNumber = contactForm.getUserPhoneNumber();
    }



    public long getId() {
        return id;
    }

    public String getcName() {
        return cName;
    }

    public Email getcEmail() {
        return cEmail;
    }

    public PhoneNumber getcPhoneNumber() {
        return cPhoneNumber;
    }

    @ApiResourceProperty(ignored = AnnotationBoolean.TRUE)
    public Key<Profile> getProfileKey() {
        return profileKey;
    }

    // Get a String version of the key
    public String getWebSafeKey()
    {
        return Key.create(profileKey, Contact.class, id).getString();
    }

    @ApiResourceProperty(ignored = AnnotationBoolean.TRUE)
    public String getProfileId() {
        return profileId;
    }

    @Override
    public String toString() {
        return "Contact{" +
                "id=" + id +
                ", cName='" + cName + '\'' +
                ", cEmail=" + cEmail +
                ", profileId='" + profileId + '\'' +
                ", cPhoneNumber=" + cPhoneNumber +
                '}';
    }
}

Any idea would be appreciated.

Upvotes: 1

Views: 442

Answers (1)

tj-recess
tj-recess

Reputation: 1809

You've got a parent associated with your class Contact.

// private key, to connect this Entity to Profile Entity
@Parent
@ApiResourceProperty(ignored = AnnotationBoolean.TRUE)
private Key<Profile> profileKey;

In datastore, Contact entities are stored as:

/User1Profile/SomeContact1
/User1Profile/SomeContact2

Datastore can't search any entity with just the ID of contact (i.e. "SomeContact1") but it can search if you provide parent as well. The right way to delete would be:

 ofy().delete().type(Contact.class).parent(profileKey).ids(contactId).now();

Read this for more details: https://code.google.com/p/objectify-appengine/wiki/BasicOperations#Deleting

Upvotes: 3

Related Questions