【问题标题】:How to delete entity by Id from data-store in AppEngine?如何从 AppEngine 的数据存储中按 ID 删除实体?
【发布时间】:2015-05-02 18:25:26
【问题描述】:

我创建了一个 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();
    }

而我的Contact 班级是这样的:

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

任何想法都将不胜感激。

【问题讨论】:

    标签: google-app-engine google-cloud-datastore objectify objectify-delete


    【解决方案1】:

    您有一个与您的班级联系人相关联的家长。

    // 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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-26
    • 1970-01-01
    • 2011-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多