如何通过 Id 从 AppEngine 的数据存储中删除实体?

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

我创建了一个 API 来通过其键删除一个实体,但是我开始使用 Http 204 并且实体不会从数据存储中删除。

这是我的 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();
    }

我的Contactclass是这样的:

@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 +
                '}';
    }
}

如有任何想法,我们将不胜感激。

您有一位家长与您的 class 联系人相关联。

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

在数据存储中,联系人实体存储为:

/User1Profile/SomeContact1
/User1Profile/SomeContact2

Datastore 无法搜索仅具有联系人 ID 的任何实体(即 "SomeContact1"),但如果您也提供父级,它可以搜索。正确的删除方式是:

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

阅读此内容了解更多详情:https://code.google.com/p/objectify-appengine/wiki/BasicOperations#Deleting