【问题标题】:Update Entity with Objectify使用 Objectify 更新实体
【发布时间】:2012-06-12 03:08:51
【问题描述】:

我正面临这种情况:我有三个实体(EntA、EntB 和 EntC) EntA 和 EntB 有一个 Key 参数。 EntC 也有一个 List 参数,它将 EntA 和 EntB 的键保存为字符串。

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

  1. 创建并保存 EntC,这将为 EntA 和 EntB 创建我需要的 Key 值。
  2. 我使用 Key 创建 EntA 和 EntB 并保存它们。
  3. 通过将 Key 和 Key 添加到相应的列表来更新 EntC。我发现实体没有在 DS 中更新,而是重新编写。所以,我首先获取 EntC,获取它的 id,我用它来创建一个包含 Key 和 Key 的新 EntC 实体。最后,我删除了原来的 EntC 并保存了新的 EntC。

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

【问题讨论】:

    标签: google-app-engine objectify


    【解决方案1】:

    提前分配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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-15
      • 2018-06-18
      • 1970-01-01
      • 2012-10-01
      • 2012-10-01
      • 2014-02-21
      • 2019-02-09
      相关资源
      最近更新 更多