【发布时间】:2016-01-16 19:54:53
【问题描述】:
假设我的数据存储模型如下所示:
@Entity
public class User {
@Id
private Long id;
@Index
private String email;
private Long dateOfBirth;
// More fields...
}
@Entity
public class Topic {
@Id
private Long id;
private String topicTitle;
private Long date;
}
@Entity
public class Comment {
@Id
private Long id;
@Parent
private Key<Topic> topicKey;
private Long commenterId;
private String text;
private Long date;
}
实体 Comment 具有父实体 Topic 的地方。我知道在指定@Parent 时应该存储密钥,就像我在评论实体中所做的那样,但是还应该存储commenterId 的密钥吗?或者存储该用户的Long id 是否足够?
只是想知道在其他实体不是父实体时存储对其他实体的引用的最佳做法是什么 - 您应该存储 id 并稍后生成密钥,还是只存储实体的密钥。有充分的理由为什么你可能会做一个而不是另一个?
编辑:由于我使用的是 Cloud Endpoints,因此我从 AppEngine 项目中获得的响应是 JSON。客户端库中不允许使用参数化类型的密钥。所以对我来说,id 可以工作,Key<?> 也可以工作。请注意,您应该使用以下方式将网络安全版本返回给您的客户端:
myKey.getString();
【问题讨论】:
标签: google-app-engine google-cloud-endpoints google-cloud-datastore