如何存储用户拥有的数据?

时间:2015-03-24 22:19:51

标签: google-app-engine objectify

我正在学习Google App Engine + Google Cloud Endpoints + Objectify,我正在尝试了解如何创建REST API,让每个特定用户将他的数据保存在云中。

我目前的奋斗是如何存储用户所拥有的实体(com.google.appengine.api.users.User)?

到目前为止,我有终点:

@ApiMethod(name = "saveBook")
public void saveBook(Book book, User user) throws OAuthRequestException, IOException {
    if (user == null) {
        throw new OAuthRequestException("User is not authorized");
    }

    ofy().save()
            .entity(BookRecord.fromBook(user, book))
            .now();
}

实体(在这种情况下,让我们假设User写了这本书):

@Entity
public class BookRecord {

    @Parent
    private Key<User> user;
    @Id
    private String id;
    @Index
    private String name;

    public static BookRecord fromBook(User user, Book book) {
        return new BookRecord(
                Key.create(user),
                book.getId(),
                book.getName()
        );
    }

    public BookRecord() {
    }

    private BookRecord(Key<User> user, String id, String name, String author) {
        this.user = user;
        this.id = id;
        this.name = name;
        this.author = author;
    }

}

问题来自于User不是Entity,因此我无法真正使用此解决方案并直接使用User作为@Parent。解决此问题和存储用户拥有的数据的一般解决方案是什么?

1 个答案:

答案 0 :(得分:1)

创建您自己的User实体,您可以在此处存储您应用的自定义信息/偏好设置(正如我建议here)。

您将使用getUserId()将其与Google用户绑定,然后您可以根据需要随意使用它。

相关问题