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

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

问题描述

我正在开发带有后端的应用程序,并决定尝试将Google App Engine用于我的后端。由于我在Google App Engine上真的很新,所以我对这个逻辑有点困惑。



基本上,我有几个模型类来表示我的对象类型。可以说,其中一个是用户,另一个是项目。用户具有项目并且项目可以属于多个用户。因此,用户X可以有25个项目,包括项目A,用户Y可以有完全不同的20个项目,也可以是项目A.现在我的用户类看起来像这样:

  @Entity 
public class User {

@Id private Long ID;
私人字符串名称;
私人字符串emailAddress;
私人字符串photoURL;

//所有获得者和设置者...
}

和我的物品类大致相同。我的一个问题是,我应该在哪里添加某种列表,如项目列表中的用户列表。我应该使用哪个注释?这个注解是什么使我成为一个结果(一个引用,一个id或一个完整的对象)?

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

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

解决方案

您可以在数据存储中搜索两件事:密钥和索引属性。

  class Thing {
@Id Long id;
@Index字符串属性;

$ / code>

在某些时候,您可以保存一些实体

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

现在您可以根据索引属性搜索所有实体。例如。对于属性==是

 列表<事> ();。();。 

准确返回 thing1



与属性列表一样。

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

class Item {
@Id
Long ID;
}

列表< User> searchUsersWithItem(long itemId){
Key< Item> itemKey = Key.create(Item.class,itemId);
return ofy()。load()。type(User.class).filter(items,itemKey).list();
}
列表<用户> searchUsersWithItem(Item item){
return ofy()。load()。type(User.class).filter(items,item).list();
}
//只加载所有者中的所有引用项目
List< Item> searchItemsWithOwner(User owner){
返回新的ArrayList< Item>(ofy()。load()。< Item> values(owner.items).values());
}

过滤器 refs,keys和entitiy实例。

被发现的东西必须被编入索引 https://cloud.google.com/datastore/docs/concepts/indexes / https://github.com/objectify/objectify/wiki/Queries



您需要决定的是您建模你的关系。有多种方式。拥有一组可由用户组拥有的项目的用户实际上是多对多关系。您可以像

  class User {List< Key< Item>>项目; } 
class Item {}

  class User {} 
class Item {List< Key< user>>拥有者; }

  class User {List< Key< Item>>项目; } 
class Item {List< Key< User>>拥有者; }

甚至

  class User {} 
class Item {}
class拥有权{Key< Item>项目;密钥<使用者>用户; }

每种方法在数据一致性和可搜索性/性能方面都有起伏。在最初的例子中,搜索用户的所有项目是微不足道的,因为您只需加载该用户并且拥有项目列表。另一个方向需要查询方法。



因此,就搜索性能而言,您可以从项目中的所有者列表以及用户中的项目列表中受益因为这样你根本不需要查询。数据一致性的缺点在于大的缺点。如果您无法同时更新用户和物品,则可以让相信由用户拥有的物品在用户认为不同的情况下使用。



最后一种方法是,使用明确的所有权实体实质上是传统的枢轴/联结表 https: //en.wikipedia.org/wiki/Many-to-many_%28data_model%29 ,这是将多关系转化为2个一对多关系的结果。使用这种方法会导致一致性较差,但是查询性能最差。



父关系有时可能很有用,但只有在父类需要的情况下存在实际的1对多关系存在。

另请注意,键不像传统SQL数据库那样是外键,因为它们可以在没有实体的情况下存在。所以无论你做什么,你都必须注意一致性。


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.

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.

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...
}

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)?

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?

解决方案

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

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

At some point you save some entities

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

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();

Would return exactly 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 works with refs, keys and entitiy instances.

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 { }

or

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

or

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

or even

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.

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.

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; Android Studio上的Google App Engine的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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