【发布时间】:2014-06-29 06:02:31
【问题描述】:
在 Google App Engine Java 中,
当我这样做时:
ProjectData pd = new ProjectData();
pd.id = null; // let Objectify auto-generate the project id
pd.dateCreated = date;
datastore.put(pd); // put the project in the db so that it gets assigned an id
当运行“datastore.put(pd)”时,数据库会自动为每个 pd 分配一个唯一的 ID。 但是,如果我添加父键字段:
ProjectData pd_new= new ProjectData();
pd_new.id = null; // let Objectify auto-generate the project id
pd_new.parentKey= new Key<OtherData>(OtherData.class, anotherId);
pd_new.dateCreated = date;
datastore.put(pd_new);
尽管 id 仍然会自动生成,但它不再是唯一的。 id 总是相同的。例如,我在数据库中有 7 个 ProjectData,它们的 Id 都是“5629499534213120”
我的数据结构是
public class ProjectData {
@Id Long id;
@Parent Key<OtherData> parentKey;
long dataCreated;
}
我是不是在这里做错了什么,这让我困扰了好几天。
顺便说一句,我可以通过手动分配它们来修复它
pd_new.id = System.currentTimeMillis();
但我认为这不是一个合适的方法。
【问题讨论】:
标签: java database google-app-engine google-cloud-datastore