无法在应用引擎中创建新对象:重复的主键(id:long 型)
Failed to create a new object in app-engine: repeated primary key (id: type long)
1.- 创建对象 id
// Allocate a key for the conference -- let App Engine allocate the ID
final Key<Conference> conferenceKey = factory().allocateId(profileKey, Conference.class);
// Get the Conference Id from the Key
final long conferenceId = conferenceKey.getId();
2.- 创建对象,添加id
// Create Conference
Conference conference = new Conference(conferenceId,userId,conferenceForm);
3.- 保存对象:
// Save Conference and Profile Entities
ofy().save().entities(profile,conference).now();
ofy().save().entity(conference).now();
4.- 错误,多次使用相同的 ID(数据存储 google)
Note: Same objects created with same ANDROID_CLIENT_ID (release mode)
您看到的是正确的。您的屏幕截图显示了 2 个 ID = 1 的实体,但父级(祖先路径)不同。
Datastore 键由其完整的祖先路径构成,并且是唯一的键 - 而不是 ID/Name。 ID/Name 仅在其父级范围内是唯一的。如果实体没有祖先,那么您会期望 ID 是唯一的。
This page 很好地概述了键。
1.- 创建对象 id
// Allocate a key for the conference -- let App Engine allocate the ID
final Key<Conference> conferenceKey = factory().allocateId(profileKey, Conference.class);
// Get the Conference Id from the Key
final long conferenceId = conferenceKey.getId();
2.- 创建对象,添加id
// Create Conference
Conference conference = new Conference(conferenceId,userId,conferenceForm);
3.- 保存对象:
// Save Conference and Profile Entities
ofy().save().entities(profile,conference).now();
ofy().save().entity(conference).now();
4.- 错误,多次使用相同的 ID(数据存储 google)
Note: Same objects created with same ANDROID_CLIENT_ID (release mode)
您看到的是正确的。您的屏幕截图显示了 2 个 ID = 1 的实体,但父级(祖先路径)不同。
Datastore 键由其完整的祖先路径构成,并且是唯一的键 - 而不是 ID/Name。 ID/Name 仅在其父级范围内是唯一的。如果实体没有祖先,那么您会期望 ID 是唯一的。
This page 很好地概述了键。