gpt4 book ai didi

android - 如何在android中具有一对一或一对多关系的对象化实体中插入记录

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:58:42 27 4
gpt4 key购买 nike

我有如下城市类模型:

@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 之间建立关系,使得许多人可以属于一个城市。问题:

  1. 对于这种关系,这是类的正确建模吗?如果不是请告诉我一对一和一对多关系的正确模型

  2. 如何通过 java 代码(端点)和 API 资源管理器在数据存储中为这种关系插入记录?

  3. 是否需要使用@Parent注解或@Index注解?

  4. 建立这种关系后,如果我删除一个城市,那么所有属于该城市的人都必须自动删除。这种建模能够实现吗?请告诉我执行此操作的代码。如果不是那么我怎样才能实现这种使用关系?

最佳答案

我无法回答有关 Google Endpoints 的任何问题,但是使用指向 City 的关键字段对 Person 进行建模的基本思想可能是正确的 - 假设有很多人到一个城市。您需要 @Index 关键字段,以便您可以查询城市中的人。请注意,此查询将最终保持一致,因此如果您要添加/删除城市中的大量人员,您需要在停止添加人员和执行删除之间设置延迟。

您可以对此建模,使 City 成为 Person 的@Parent。这会消除最终的一致性,但这意味着你永远不能将一个人移动到一个新的城市。这也意味着由于单个实体组的事务吞吐量限制,该城市中的任何城市或个人每秒都不能更改超过一次。假设您实际上是在谈论 Persons and Cities,您可能不想要这个。但这取决于您的数据集。

关于android - 如何在android中具有一对一或一对多关系的对象化实体中插入记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37475621/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com