使用Android& amp; Android Studio上的Google App Engine [英] Using Android & Google App Engine on Android Studio

查看:119
本文介绍了使用Android& amp; Android Studio上的Google App Engine的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发具有后端的应用程序,因此决定尝试将Google App Engine用于后端.由于我真的是Google App Engine的新手,因此我对逻辑有点困惑.

I'm developing an app with backend and I decided to try using Google App Engine for my backend. Since I'm really new on Google App Engine, I'm little bit confused with the logic.

基本上,我有几个模型类来表示我的对象类型.可以说其中一个是User,另一个是Item.用户拥有项目,并且一个项目可以属于多个用户.因此,用户X可以有25个项目,包括项目A,而用户Y可以有完全不同的20个项目以及项目A.

Basically, I have a couple of model classes to represent my object types. Lets say one of them is User and another is Item. Users have items and an item can belong more than one user. So User X can have 25 items including Item A, and User Y can have totally different 20 items and also the Item A.

现在我的User类看起来像这样:

Right now my User class looks like this:

@Entity
public class User {

    @Id private Long id;
    private String name;
    private String emailAddress;
    private String photoURL;

    //All getters and setters...
}

我的Item类大致相同.我的问题之一是,我应该在哪里添加某种列表,例如用户"中的项目"列表.我应该使用哪个注释?该注释将为我提供什么结果(引用,ID或完整的对象)?

And my Item class is approximately same. One of my questions is, where should I add some kind of list, like a list of Items into User. And which annotation should I use? What will that annotation provide me as a result (a reference, an id or a complete object)?

与此相关的另一个问题是,在我的端点类中,如何获得特定用户拥有的项目列表(或拥有特定项目的用户列表)?

Another question related to this is, in my endpoint class, how can I get a list of Items that a specific User has (or list of Users that owns a specific Item)?

最后一个完全不相关的问题,我应该做些什么使id自动递增,或者如果我在插入项目时不提供任何ID,它会自动递增吗?

One last totally unrelated question, should I do anything to make id auto increment or will it be automatic if I won't provide any id while inserting an item?

推荐答案

您可以在数据存储区中搜索2件东西:键和索引属性.

You can search in the datastore for 2 things: keys and indexed properties.

class Thing {
   @Id Long id;
   @Index String property;
}

您有时会保存一些实体

Thing thing1 = new Thing();
thing1.property = "yes";
Thing thing2 = new Thing();
thing2.property = "no";
ofy().save().entities(thing1, thing2).now();

现在,您可以根据其索引属性搜索所有实体.例如.与property == "yes"有关的所有事情.

Now you can search for all entities based on their indexed properties. E.g. for all things with property == "yes".

List<Thing> things = ofy().load().type(Thing.class).filter("property", "yes").list();

将准确返回thing1.

这与属性列表相同.而且,它还可以使用其他属性的引用/键列表.

The same works with Lists of properties. And it works with lists of references/keys to other properties.

class User {
    @Id Long id;
    @Index List<Key<Item>> items;
}

class Item {
    @Id
    Long id;
}

List<User> searchUsersWithItem(long itemId) {
    Key<Item> itemKey = Key.create(Item.class, itemId);
    return ofy().load().type(User.class).filter("items", itemKey).list();
}
List<User> searchUsersWithItem(Item item) {
    return ofy().load().type(User.class).filter("items", item).list();
}
// just loads all the referenced items in the owner
List<Item> searchItemsWithOwner(User owner) {
    return new ArrayList<Item>(ofy().load().<Item>values(owner.items).values());
}

filter适用于ref,键和实体实例.

filter works with refs, keys and entitiy instances.

要被发现,必须对事物进行索引 https://cloud.google.com/datastore /docs/concepts/indexes / https://github.com/objectify/objectify/Wiki/查询

To be found things must be indexed https://cloud.google.com/datastore/docs/concepts/indexes / https://github.com/objectify/objectify/wiki/Queries

您需要决定的是如何建立关系模型.有多种方法.拥有可以由一组用户拥有的一组项目的用户实际上是多对多关系.您可以将其表示为

What's left for you to decide is how you model your relation. There are multiple ways. A user that owns a set of items which can be owned by set of users is actually a many-to-many relation. You could represent it like

class User { List<Key<Item>> items; }
class Item { }

class User { }
class Item { List<Key<User>> owners; }

class User { List<Key<Item>> items; }
class Item { List<Key<User>> owners; }

甚至

class User { }
class Item { }
class Ownership { Key<Item> item; Key<User> user; }

每种方法在数据一致性和可搜索性/性能方面都有起有落.在最初的示例中,搜索用户的所有项目很简单,因为您要做的就是加载一个用户,并且您拥有项目列表.另一个方向需要查询方法.

Each approach has it's ups and downs with respect to data consistency and searchability / performance. In the initial example it's trivial to search for all items of a user since all you have to to is to load that one user and you have the list of items. The other direction requires the query approach.

因此,在搜索性能方面,您将受益于项目中的所有者列表以及用户中的项目列表,因为那样您根本就不需要查询.不利的一面是数据一致性.如果您无法同时更新用户和项目,则可以在用户认为与众不同的情况下拥有被认为归用户所有的项目.

So with respect to search performance you benefit from having the list of owners in the items as well as the list of items in the user because that way you don't need queries at all. The big downside becomes data consistency. If you fail to update both user and item at the same time you can have items that believe to be owned by a user where the user thinks different.

使用显式所有权"实体的最后一种方法实质上是传统的数据透视表/联结表 https://en.wikipedia.org/wiki/Many-to-many_%28data_model%29 ,这是将多对多关系转换为2个多对多关系的结果.使用该方法将导致容易的一致性,但查询性能最差.

The last approach, using an explicit "Ownership" entity is essentially the traditional pivot / junction table https://en.wikipedia.org/wiki/Many-to-many_%28data_model%29 that is the result of transforming a many-many relation into 2 one-many relations. Using that would result in easy consistency, but the worst query performance.

父母关系有时可能有用,但前提是必须存在实际的一对多关系.

Parent relations can sometimes be useful but only if there is an actual 1 to many relation where the parent needs to exist.

还请注意,键如何不像传统SQL数据库那样是外键,因为它们可以不存在实体而存在.因此,无论您做什么,都必须保持一致性.

Also note how keys are not foreign keys like in traditional SQL databases as they can exist without an entity. So you'll have to take care of consistency regardless of what you do.

这篇关于使用Android&amp; amp; Android Studio上的Google App Engine的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆