开发者

Objectify and entity groups

开发者 https://www.devze.com 2023-04-01 09:29 出处:网络
Here is a question for all the objectify/ app engine gurus out there: I am creating ojectes with a parent/child relationship by storing the key of the parent object in the child.

Here is a question for all the objectify/ app engine gurus out there:

I am creating ojectes with a parent/child relationship by storing the key of the parent object in the child. This is stored in a ob开发者_如何学编程ject of type Key. For example let's say I have a car object and tire objects. The tire objects store the parent key in a variable of type Key.

@Entity
Public class Tire{
     @Id Long id;
     Key<Car> ParentKey;

     int size;
}

In my app I will need to get all the tires given a certain car. I can do this with a query: Tire tires = oft.query(Tire.class).filter("ParentKey",carKey).get();

Is this an approipriate way to accomplish this? Will this cause any issues with entity groups? Will this be efficient for a large number of cars and tires?


Right now you're not creating a parent/child relationship, at least as is defined by app engine. Check out the documentation: adding a parent/child relationship can speed up things because the car and its tyres will be stored physically together, but they can be difficult to remove if at some point they are not longer needed.

To create a parent/child relationship using Objectify, add the @Parent annotation:

// Use com.googlecode.objectify.Key instead of 
// com.google.appengine.api.datastore.Key
@Parent Key<Car> parentKey;

Now, in order to get all the tires that belong to a specific car:

List<Tyre> tires = ofy().query(Tyre.class).ancestor(carKey).list();


I'm using exactly same way - no problem.

I don't see there any conflicts with entity groups, and it's working fine for a large groups (at least for a thousands of entities)

P.S. If you need to fetch data that belongs to same group - you don't need to use GAE groups. Even more: entity groups are best for transactions, not for filtering.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号