- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有如下城市类模型:
@Entity
public class City {
@Id
Long id;
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
我有下面给出的另一个模型类 Person:
@Entity
public class Person {
@Id
Long id;
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@ApiResourceProperty(ignored = AnnotationBoolean.TRUE)
Key<City> city;
}
之后,我使用 android studio 为这两个类生成端点并进行部署。
这里是生成端点的代码:
PersonEndpoint
@Api(
name = "personApi",
version = "v1",
resource = "person",
namespace = @ApiNamespace(
ownerDomain = "backend.faceattendence.morpho.com",
ownerName = "backend.faceattendence.morpho.com",
packagePath = ""
)
)
public class PersonEndpoint {
private static final Logger logger = Logger.getLogger(PersonEndpoint.class.getName());
private static final int DEFAULT_LIST_LIMIT = 20;
static {
// Typically you would register this inside an OfyServive wrapper. See: https://code.google.com/p/objectify-appengine/wiki/BestPractices
ObjectifyService.register(Person.class);
}
/**
* Returns the {@link Person} with the corresponding ID.
*
* @param id the ID of the entity to be retrieved
* @return the entity with the corresponding ID
* @throws NotFoundException if there is no {@code Person} with the provided ID.
*/
@ApiMethod(
name = "get",
path = "person/{id}",
httpMethod = ApiMethod.HttpMethod.GET)
public Person get(@Named("id") Long id) throws NotFoundException {
logger.info("Getting Person with ID: " + id);
Person person = ofy().load().type(Person.class).id(id).now();
if (person == null) {
throw new NotFoundException("Could not find Person with ID: " + id);
}
return person;
}
/**
* Inserts a new {@code Person}.
*/
@ApiMethod(
name = "insert",
path = "person",
httpMethod = ApiMethod.HttpMethod.POST)
public Person insert(Person person) {
// Typically in a RESTful API a POST does not have a known ID (assuming the ID is used in the resource path).
// You should validate that person.id has not been set. If the ID type is not supported by the
// Objectify ID generator, e.g. long or String, then you should generate the unique ID yourself prior to saving.
//
// If your client provides the ID then you should probably use PUT instead.
ofy().save().entity(person).now();
logger.info("Created Person.");
return ofy().load().entity(person).now();
}
/**
* Updates an existing {@code Person}.
*
* @param id the ID of the entity to be updated
* @param person the desired state of the entity
* @return the updated version of the entity
* @throws NotFoundException if the {@code id} does not correspond to an existing
* {@code Person}
*/
@ApiMethod(
name = "update",
path = "person/{id}",
httpMethod = ApiMethod.HttpMethod.PUT)
public Person update(@Named("id") Long id, Person person) throws NotFoundException {
// TODO: You should validate your ID parameter against your resource's ID here.
checkExists(id);
ofy().save().entity(person).now();
logger.info("Updated Person: " + person);
return ofy().load().entity(person).now();
}
/**
* Deletes the specified {@code Person}.
*
* @param id the ID of the entity to delete
* @throws NotFoundException if the {@code id} does not correspond to an existing
* {@code Person}
*/
@ApiMethod(
name = "remove",
path = "person/{id}",
httpMethod = ApiMethod.HttpMethod.DELETE)
public void remove(@Named("id") Long id) throws NotFoundException {
checkExists(id);
ofy().delete().type(Person.class).id(id).now();
logger.info("Deleted Person with ID: " + id);
}
/**
* List all entities.
*
* @param cursor used for pagination to determine which page to return
* @param limit the maximum number of entries to return
* @return a response that encapsulates the result list and the next page token/cursor
*/
@ApiMethod(
name = "list",
path = "person",
httpMethod = ApiMethod.HttpMethod.GET)
public CollectionResponse<Person> list(@Nullable @Named("cursor") String cursor, @Nullable @Named("limit") Integer limit) {
limit = limit == null ? DEFAULT_LIST_LIMIT : limit;
Query<Person> query = ofy().load().type(Person.class).limit(limit);
if (cursor != null) {
query = query.startAt(Cursor.fromWebSafeString(cursor));
}
QueryResultIterator<Person> queryIterator = query.iterator();
List<Person> personList = new ArrayList<Person>(limit);
while (queryIterator.hasNext()) {
personList.add(queryIterator.next());
}
return CollectionResponse.<Person>builder().setItems(personList).setNextPageToken(queryIterator.getCursor().toWebSafeString()).build();
}
private void checkExists(Long id) throws NotFoundException {
try {
ofy().load().type(Person.class).id(id).safe();
} catch (com.googlecode.objectify.NotFoundException e) {
throw new NotFoundException("Could not find Person with ID: " + id);
}
}
}
城市端点
@Api(
name = "cityApi",
version = "v1",
resource = "city",
namespace = @ApiNamespace(
ownerDomain = "backend.faceattendence.morpho.com",
ownerName = "backend.faceattendence.morpho.com",
packagePath = ""
)
)
public class CityEndpoint {
private static final Logger logger = Logger.getLogger(CityEndpoint.class.getName());
private static final int DEFAULT_LIST_LIMIT = 20;
static {
// Typically you would register this inside an OfyServive wrapper. See: https://code.google.com/p/objectify-appengine/wiki/BestPractices
ObjectifyService.register(City.class);
}
/**
* Returns the {@link City} with the corresponding ID.
*
* @param id the ID of the entity to be retrieved
* @return the entity with the corresponding ID
* @throws NotFoundException if there is no {@code City} with the provided ID.
*/
@ApiMethod(
name = "get",
path = "city/{id}",
httpMethod = ApiMethod.HttpMethod.GET)
public City get(@Named("id") Long id) throws NotFoundException {
logger.info("Getting City with ID: " + id);
City city = ofy().load().type(City.class).id(id).now();
if (city == null) {
throw new NotFoundException("Could not find City with ID: " + id);
}
return city;
}
/**
* Inserts a new {@code City}.
*/
@ApiMethod(
name = "insert",
path = "city",
httpMethod = ApiMethod.HttpMethod.POST)
public City insert(City city) {
// Typically in a RESTful API a POST does not have a known ID (assuming the ID is used in the resource path).
// You should validate that city.id has not been set. If the ID type is not supported by the
// Objectify ID generator, e.g. long or String, then you should generate the unique ID yourself prior to saving.
//
// If your client provides the ID then you should probably use PUT instead.
ofy().save().entity(city).now();
logger.info("Created City.");
return ofy().load().entity(city).now();
}
/**
* Updates an existing {@code City}.
*
* @param id the ID of the entity to be updated
* @param city the desired state of the entity
* @return the updated version of the entity
* @throws NotFoundException if the {@code id} does not correspond to an existing
* {@code City}
*/
@ApiMethod(
name = "update",
path = "city/{id}",
httpMethod = ApiMethod.HttpMethod.PUT)
public City update(@Named("id") Long id, City city) throws NotFoundException {
// TODO: You should validate your ID parameter against your resource's ID here.
checkExists(id);
ofy().save().entity(city).now();
logger.info("Updated City: " + city);
return ofy().load().entity(city).now();
}
/**
* Deletes the specified {@code City}.
*
* @param id the ID of the entity to delete
* @throws NotFoundException if the {@code id} does not correspond to an existing
* {@code City}
*/
@ApiMethod(
name = "remove",
path = "city/{id}",
httpMethod = ApiMethod.HttpMethod.DELETE)
public void remove(@Named("id") Long id) throws NotFoundException {
checkExists(id);
ofy().delete().type(City.class).id(id).now();
logger.info("Deleted City with ID: " + id);
}
/**
* List all entities.
*
* @param cursor used for pagination to determine which page to return
* @param limit the maximum number of entries to return
* @return a response that encapsulates the result list and the next page token/cursor
*/
@ApiMethod(
name = "list",
path = "city",
httpMethod = ApiMethod.HttpMethod.GET)
public CollectionResponse<City> list(@Nullable @Named("cursor") String cursor, @Nullable @Named("limit") Integer limit) {
limit = limit == null ? DEFAULT_LIST_LIMIT : limit;
Query<City> query = ofy().load().type(City.class).limit(limit);
if (cursor != null) {
query = query.startAt(Cursor.fromWebSafeString(cursor));
}
QueryResultIterator<City> queryIterator = query.iterator();
List<City> cityList = new ArrayList<City>(limit);
while (queryIterator.hasNext()) {
cityList.add(queryIterator.next());
}
return CollectionResponse.<City>builder().setItems(cityList).setNextPageToken(queryIterator.getCursor().toWebSafeString()).build();
}
private void checkExists(Long id) throws NotFoundException {
try {
ofy().load().type(City.class).id(id).safe();
} catch (com.googlecode.objectify.NotFoundException e) {
throw new NotFoundException("Could not find City with ID: " + id);
}
}
}
我想在 City 和 Person 之间建立关系,使得许多人可以属于一个城市。问题:
对于这种关系,这是类的正确建模吗?如果不是请告诉我一对一和一对多关系的正确模型
如何通过 java 代码(端点)和 API 资源管理器在数据存储中为这种关系插入记录?
是否需要使用@Parent注解或@Index注解?
建立这种关系后,如果我删除一个城市,那么所有属于该城市的人都必须自动删除。这种建模能够实现吗?请告诉我执行此操作的代码。如果不是那么我怎样才能实现这种使用关系?
最佳答案
我无法回答有关 Google Endpoints 的任何问题,但是使用指向 City 的关键字段对 Person 进行建模的基本思想可能是正确的 - 假设有很多人到一个城市。您需要 @Index 关键字段,以便您可以查询城市中的人。请注意,此查询将最终保持一致,因此如果您要添加/删除城市中的大量人员,您需要在停止添加人员和执行删除之间设置延迟。
您可以对此建模,使 City 成为 Person 的@Parent。这会消除最终的一致性,但这意味着你永远不能将一个人移动到一个新的城市。这也意味着由于单个实体组的事务吞吐量限制,该城市中的任何城市或个人每秒都不能更改超过一次。假设您实际上是在谈论 Persons and Cities,您可能不想要这个。但这取决于您的数据集。
关于android - 如何在android中具有一对一或一对多关系的对象化实体中插入记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37475621/
下面的说法正确吗? “人最好的 friend 是狗。” public class Mann { private BestFriend dog; //etc } 最佳答案 我想说这样
我一直在 documentation 中查看 Laravel 4 中的关系我正在尝试解决以下问题。 我的数据库中有一个名为“事件”的表。该表具有各种字段,主要包含与其他表相关的 ID。例如,我有一个“
我的表具有如下关系: 我有相互链接的级联下拉框,即当您选择国家/地区时,该国家/地区下的区域将加载到区域下拉列表中。但现在我想将下拉菜单更改为基于 Ajax 的自动完成文本框。 我的问题是,我应该有多
我正在尝试弄清楚如何构建这个数据库。我之前用过Apple的核心数据就好了,现在我只是在做一个需要MySQL的不同项目。我是 MySQL 的新手,所以请放轻松。 :) 对于这个例子,假设我有三个表,Us
MongoDB 的关系表示多个文档之间在逻辑上的相互联系。 文档间可以通过嵌入和引用来建立联系。 MongoDB 中的关系可以是: 1:1 (1对1) 1: N (1对多)
您能解释一下 SQL 中“范围”和“分配单元”之间的区别或关系吗? 最佳答案 分配单元基本上只是一组页面。它可以很小(一页)或很大(很多页)。它在 sys.allocation_units 中有一个元
我有一个表 geoLocations,其中包含两列纬度和经度。还有第二个表(让我们将其命名为城市),其中包含每对唯一的纬度和经度对应的城市。 如何使用 PowerPivot 为这种关系建模?创建两个单
我想用 SQLDelight 建模关系,尤其是 一对多关系。 我有 2 张 table :recipe和 ingredient .为简单起见,它们看起来像这样: CREATE TABLE recipe
我是 Neo4J 新手,我有一个带有源和目标 IP 的简单 CSV。我想在具有相同标签的节点之间创建关系。 类似于... source_ip >> ALERTS >> dest_ip,或者相反。 "d
我正在创建一个类图,但我想知道下面显示的两个类之间是否会有任何关联 - 据我了解,对于关联,ClassA 必须有一个 ClassB 的实例,在这种情况下没有但是,它确实需要知道 ClassB 的一个变
是否可以显示其他属性,即“hasTopping”等? 如何在 OWLViz 中做到这一点? 最佳答案 OWLViz 仅 显示类层次结构(断言和推断的类层次结构)。仅使用“is-a”关系进行描述。 OW
public class MainClass { ArrayList mans = new ArrayList(); // I'm filling in this arraylist,
我想知道“多对二”的关系。 child 可以与两个 parent 中的任何一个联系,但不能同时与两个 parent 联系。有什么办法可以加强这一点吗?我也想防止 child 重复条目。 一个真实的例子
我有一个已经创建的Grails插件,旨在支持许多应用程序。该插件具有一个Employee域对象。问题在于,当在主应用程序中使用该应用程序中的域对象时,需要将其引用回Employee对象。因此,我的主应
我有一个类(class)表、类(class)hasMany部分和部分hasMany讲座以及讲座hasMany评论。如果我有评论 ID 并且想知道其类(class)名称,我应该如何在 LectureCo
我有一个模型团队,包含 ID 和名称。所有可能的团队都会被存储。 我的模型游戏有两列 team_1 和 team_2..我需要哪种关系? 我已经测试了很多,但它只适用于一列.. 最佳答案 也许你可以试
我读了很多关于 ICE 或 Corba 等技术中使用的仆人和对象的文章。有很多资源我可以读到这样的东西: 一个仆人可以处理多个对象(为了节省资源)。 一个对象可以由多个仆人处理(为了可靠性)。 有人可
嗨, 我有一个令人沮丧的问题,我在这方面有点生疏。我有两个这样的类(class): class A{ int i; String j ; //Getters and setters} class B
class Employee { private String name; void setName(String n) { name = n; } String getNam
如果您有这样的关系: 员工与其主管员工之间存在多对一关系 员工与其部门的多对一关系 部门与其经理一对一 我会在 Employee 实体中写入: @ManyToOne (cascade=CascadeT
我是一名优秀的程序员,十分优秀!