【问题标题】:Update and delete not working with Google Cloud Endpoints更新和删除不适用于 Google Cloud Endpoints
【发布时间】:2014-05-27 14:27:31
【问题描述】:

我有一个类 Student 和为它生成的 Endpoint 类。 ListStudents 和 insertStudents 方法可以正常工作,但更新和删除不会导致数据存储区发生任何变化。这些方法不会引发任何错误并且调用会返回,但不会进行任何更改。 我的端点代码主要是google插件为eclipse生成的代码:

@ApiMethod(name = "removeStudent", path="remove_student")
public void removeStudent(@Named("email") String email) {
EntityManager mgr = getEntityManager();
    try {       
        Student student = getStudentByEmailName(email);         
        mgr.remove(student);
    } finally {
        mgr.close();
    }
}

实体管理器getter方法:

private static EntityManager getEntityManager() {  
return EMF.get().createEntityManager();
}

@ApiMethod(name = "updateStudent")
public Student updateStudent(Student student) {
    EntityManager mgr = getEntityManager();
    try {
        if (!containsStudent(student)) {
            throw new EntityNotFoundException("Object does not exist");
        }
        mgr.persist(student);
    } finally {
        mgr.close();
    }
    return student;
}

还有我的 EMF 课:

public final class EMF {
private static final EntityManagerFactory emfInstance = Persistence
        .createEntityManagerFactory("transactions-optional");

private EMF() {
}

public static EntityManagerFactory get() {
    return emfInstance;
}

}

使用此端点的客户端是 Android。我只尝试在我的本地服务器上进行测试。 如果我做错了什么,请告诉我。谢谢

【问题讨论】:

    标签: java android google-app-engine persistence google-cloud-endpoints


    【解决方案1】:

    您的学生实体是否通过电子邮件编入索引?

    当您迁移到 nosql 并希望所有查询都可以在没有索引的情况下工作时,这是一个典型的问题。

    请注意,在定义索引之前插入的记录不会在索引中。

    【讨论】:

      【解决方案2】:

      数据存储最终是一致的,您的代码应该可以工作。您从 updateStudent 方法在 Student 对象中获得的返回值是多少。

      尽管我不想这样做,但在您执行 mgr.persist(...) 之后,添加 mgr.flush() 看看是否会有所不同。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-23
        • 1970-01-01
        • 2021-11-16
        • 2014-09-12
        • 2014-07-05
        • 2019-07-14
        相关资源
        最近更新 更多