简体   繁体   English

App Engine数据存储 - 数据模型

[英]App Engine Datastore - Data Model

I am working with App Engine since couple of days. 我几天都在使用App Engine。 The most important for me now is modeling data, so I have some question about that. 对我来说最重要的是建模数据,所以我对此有一些疑问。 Let's say that I have simple MyUser class. 假设我有简单的MyUser类。 I have Buddy class as well which looks like that: 我也有Buddy课程,看起来像这样:

@Entity
public class Buddy {
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Key id;
  private Date createAt;
  private MyUser user;
  private Key buddyOf;
}

In this class I have MyUser field because every buddy is an user and I have buddyOf field because there is another MyUser which has this buddy on his buddy list. 在这个课程中,我有MyUser字段,因为每个好友都是用户,我有buddyOf字段,因为还有另一个MyUser在他的好友列表上有这个好友。 The question is If I get from Datastore one sample buddy, I get this MyUser as well? 问题是如果我从Datastore获得一个样本伙伴,我MyUser得到这个MyUser吗? If yes what when in MyUser class will be embedded another Entity and in that Entity one more, etc...? 如果是的话什么时候在MyUser类中将嵌入另一个Entity并在那个EntityMyUser一个......等等? Maybe I should persist only Key fields to other entities? 也许我应该只将Key字段保留给其他实体? The main question is how I should store data in datastore? 主要问题是我应该如何在数据存储中存储数据? I should use composition and has objects inside other objects? 我应该使用合成并在其他对象中包含对象? If yes what with objects in objects in objects, etc... What is the best approach? 如果是,对象中的对象等对象是什么...最好的方法是什么?

DataStore object model design should differs from design usually used for Relational Databases. DataStore对象模型设计应该与通常用于关系数据库的设计不同。 You should check supported types for properties https://developers.google.com/appengine/docs/java/datastore/entities#Properties_and_Value_Types There is no object as a property type. 您应该检查属性的受支持类型https://developers.google.com/appengine/docs/java/datastore/entities#Properties_and_Value_Types没有对象作为属性类型。 You still can use Embedded annotation but it is not a best way for your case. 您仍然可以使用嵌入式注释,但它不适合您的情况。

You should store key or even ID for user as a reference. 您应该为用户存储密钥或甚至ID作为参考。 It will make an instance smaller and solve the problem you describe. 它会使实例变小并解决您描述的问题。 But you will not be able to reach reference integrity. 但是你无法达到参考完整性。 It is a limitation of NoSQL designs. 这是NoSQL设计的一个限制。

I think you are modeling an n:m relationship. 我认为你正在建立一个n:m关系。 You may create a structure like this: 您可以创建这样的结构:

  • For each MyUser entity, it has a group of entities with element type Buddy (The group represents all buddies of this MyUser entity). 对于每个MyUser实体,它具有一组具有元素类型Buddy的实体(该组代表此MyUser实体的所有伙伴)。
  • Each Buddy entity has the MyUser entity as its ancestor. 每个Buddy实体都将MyUser实体作为其祖先。
  • Each Buddy entity represents the relationship between the owner MyUser and another MyUser object, by containing a field: the id of the other MyUser 每个Buddy实体通过包含一个字段来表示所有者MyUser与另一个MyUser对象之间的关系:另一个MyUser的id

So the following operations become easy and natural with the datastore: 因此,使用数据存储区,以下操作变得简单而自然:

  • Add/delete buddy to a MyUser entity (just add/delete new child Buddy entity) 添加/删除伙伴到MyUser实体(只需添加/删除新的子Buddy实体)
  • Query all buddies for a MyUser entity (list all children) 查询MyUser实体的所有好友(列出所有子项)

This structure has several benefits with the gae-datastore high replaca. 这种结构与gae-datastore高重放有几个好处。 For example, if you add a buddy (child entity) to the MyUser entity, they can be immediately queried since they are in the same entity group (you alway see consistent data without delay). 例如,如果您将一个伙伴(子实体)添加到MyUser实体,则可以立即查询它们,因为它们位于同一个实体组中(您总是可以看到一致的数据而没有延迟)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM