[英]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
并在那个Entity
中MyUser
一个......等等? 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: 您可以创建这样的结构:
So the following operations become easy and natural with the datastore: 因此,使用数据存储区,以下操作变得简单而自然:
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.