2012-06-08 45 views
0

我面临这种情况:我有三个实体(EntA,EntB和EntC)EntA和EntB具有密钥< EntC>参数。此外,EntC还有一个名为<的String>参数,该参数将EntA和EntB的密钥保存为字符串。更新实体与物化

为了获得任何实体的密钥,我首先必须坚持它们(id字段是自动生成的)。所以我这样做:

  1. 创建并保存EntC,这将创建密钥< EntC>值我需要为EntA和EntB。
  2. 我使用密钥< EntC创建EntA和EntB并保存它们。
  3. 通过向对应列表中添加密钥<入口>和关键字<入口>来更新入口。我发现实体没有在DS中更新,但重新编写。因此,我首先获取EntC,获取其ID,我使用它创建一个包含密钥< EntA>和密钥< EntB>的新EntC实体。最后,我删除原来的EntC并保存新的EntC。

我想知道是否有更好的方法来实现这一点。另外,在我删除它并保存新的EntC实体之前,另一个实体是否有可能获取原始EntC实体的ID?

回答

0

分配的ID提前:

例如与父母做的事:

Key<YourEntity> k= dao.fact().allocateId(parentObj, YourEntity.class); 
(obviously dao extends DAOBase, I think you know that from using objectify) 


allocateId(java.lang.Class<T> clazz) 
     Allocates a single id from the allocator for the specified kind. 


allocateId(java.lang.Object parentKeyOrEntity, java.lang.Class<T> clazz) 
     Allocates a single id from the allocator for the specified kind. 

http://objectify-appengine.googlecode.com/svn/trunk/javadoc/com/googlecode/objectify/ObjectifyFactory.html