【发布时间】:2015-04-06 16:50:49
【问题描述】:
我有两个实体。
@Entity
public class Recipe {
@Id
private Long id;
private List<Step> steps;
}
@Entity
public class Step {
@Id
private Long id;
private String instruction;
}
以及以下 Cloud Endpoint
@ApiMethod(
name = "insert",
path = "recipe",
httpMethod = ApiMethod.HttpMethod.POST)
public Recipe insert(Recipe recipe) {
ofy().save().entities(recipe.getSteps()).now(); //superfluous?
ofy().save().entity(recipe).now();
logger.info("Created Recipe with ID: " + recipe.getId());
return ofy().load().entity(recipe).now();
}
我想知道如何跳过必须首先保存嵌入实体的步骤。两个实体的Id 均未设置。我希望 objectify 自动创建这些。但是如果不保存嵌入的实体,我会得到一个异常。
com.googlecode.objectify.SaveException:保存 com.devmoon.meadule.backend.entities.Recipe@59e4ff19 时出错:您无法为 @Id 为空的对象创建密钥。对象是 com.devmoon.meadule.backend.entities.Step@589a3afb
由于我的对象结构会变得更加复杂,我需要找到一种方法来跳过这个手动步骤。
【问题讨论】:
标签: java google-app-engine google-cloud-endpoints objectify