sennin
sennin

Reputation: 8972

App Engine Datastore - Data Model

I am working with App Engine since couple of days. 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. I have Buddy class as well which looks like that:

@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. The question is If I get from Datastore one sample buddy, I get this MyUser as well? If yes what when in MyUser class will be embedded another Entity and in that Entity one more, etc...? Maybe I should persist only Key fields to other entities? 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?

Upvotes: 0

Views: 393

Answers (2)

Verilocos
Verilocos

Reputation: 115

I think you are modeling an n:m relationship. 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).
  • Each Buddy entity has the MyUser entity as its ancestor.
  • Each Buddy entity represents the relationship between the owner MyUser and another MyUser object, by containing a field: the id of the other MyUser

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)
  • Query all buddies for a MyUser entity (list all children)

This structure has several benefits with the gae-datastore high replaca. 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).

Upvotes: 1

Kirill Lebedev
Kirill Lebedev

Reputation: 650

DataStore object model design should differs from design usually used for Relational Databases. 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. 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. 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.

Upvotes: 1

Related Questions