无法使用GAE / J DataNucleus插件版本2.1.2获取新创建的JDO持久实体的ID

时间:2013-02-25 11:07:28

标签: java google-app-engine google-cloud-datastore jdo datanucleus

我的问题

我使用新的1.7.5 GAE / J SDK将我的应用程序从版本1.x移植到GAE / J的DataNucleus插件的2.0版本。这将我的JDO版本从2.3更改为3.0.1。我的持久化实体类具有类型为编码字符串的主键,以及对该对象的数字ID的只读访问权限。每个实例都是其实体组的唯一成员(子级和父级仅通过数字ID链接)。

以前,我能够创建并保留新的MyEntity实例,然后立即访问其数字ID以存储在父MyEntity实例的子ID列表中。

现在我发现新实例的数字ID在持久化后不会立即可用 - 即使它已生成并存储并且稍后可用。

我的问题

在对象创建和持久性之后,我是否可以立即恢复对数字ID的访问?

“jdoconfig.xml”配置摘录

<persistence-manager-factory name="big-table">
  <property
   name="javax.jdo.PersistenceManagerFactoryClass"
   value="org.datanucleus.api.jdo.JDOPersistenceManagerFactory"
  />
  <property name="datanucleus.DetachAllOnCommit" value="true"/>
  <property name="javax.jdo.option.NontransactionalRead" value="true"/>
  <property name="javax.jdo.option.NontransactionalWrite" value="true"/>
  <property
   name="datanucleus.appengine.autoCreateDatastoreTxns"
   value="true"
  />
  [...]
</persistence-manager-factory>

持久性实体类代码提取

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
public class MyEntity implements Serializable
{
  private static final long serialVersionUID = 1L;

  // No setter for this read-only data member
  @PrimaryKey
  @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
  @Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
  private String sEncodedKey;

  // No setter for this read-only data member
  @Persistent
  @Extension(vendorName="datanucleus", key="gae.pk-id", value="true")
  private Long loID;

  @Persistent
  private Long loParentID;

  //
  // Other persistent data members
  //

  public Long getID()
  {
    return loID;
  }

  //
  // Other getters and setters
  //
}

包含3个记录点的持久性代码

/**
 * Create a new entity.
 * @param loParentID
 *   The ID of the entity,
 *   a new child of which is to be created.
 * @param sChildName
 *   The name of the new child to be created.
 * @return
 *   The created entity child,
 *   or <code>null</code> if the operation was carried out unsuccessfully.
 */
public static MyEntity createEntityChild(Long loParentID, String sChildName)
{
  MyEntity meResult = null;
  MyEntity mePersistedChild = null;

  PersistenceManagerFactory pmf =
   DataExchange.getPersistenceManagerFactory();    // My own method
  PersistenceManager pm = pmf.getPersistenceManager();
  Transaction tx = pm.currentTransaction();
  try
  {
    tx.begin();

    MyEntity meChild = new MyEntity();
    meChild.setParentID(loParentID);
    meChild.setName(sChildName);
    meChild.setActive(true);
    mePersistedChild = pm.makePersistent(meChild);

    // "Touch" data member not in the default fetch group
    ArrayList<Long> liChildIDs = mePersistedChild.getChildIDs();
    if (liChildIDs != null)
      liChildIDs.size();

    if (mePersistedChild != null)
      g_logger.log(Level.FINE, String.format(
       "Pre-commit: mePersistedChild.getID() = %d,"
       + " mePersistedChild.getEncodedKey() = \"%s\".",
       mePersistedChild.getID(), mePersistedChild.getEncodedKey()));

    tx.commit();

    if (mePersistedChild != null)
      g_logger.log(Level.FINE, String.format(
       "Post-commit: mePersistedChild.getID() = %d,"
       + " mePersistedChild.getEncodedKey() = \"%s\".",
       mePersistedChild.getID(), mePersistedChild.getEncodedKey()));
  }
  finally
  {
    try
    {
      if (tx.isActive())    // Because of an exception, say
        tx.rollback();
    }
    finally
    {
      pm.close();
    }
  }

  if (mePersistedChild != null)
    g_logger.log(Level.FINE, String.format(
     "Post-pm-close: mePersistedChild.getID() = %d,"
     + " mePersistedChild.getEncodedKey() = \"%s\".",
     mePersistedChild.getID(), mePersistedChild.getEncodedKey()));

  [...]

  return meResult;
}

开发服务器日志记录输出

24-Feb-2013 13:28:02 [...].MyEntityBusiness createMyEntityChild
FINE: Pre-commit: mePersistedChild.getID() = null, mePersistedChild.getEncodedKey() = "agttYXJrZXQtdHJlZXISCxIMSXRlbUNhdGVnb3J5GAUM".

24-Feb-2013 13:28:03 [...].MyEntityBusiness createMyEntityChild
FINE: Post-commit: mePersistedChild.getID() = null, mePersistedChild.getEncodedKey() = "agttYXJrZXQtdHJlZXISCxIMSXRlbUNhdGVnb3J5GAUM".

24-Feb-2013 13:28:03 [...].MyEntityBusiness createMyEntityChild
FINE: Post-pm-close: mePersistedChild.getID() = null, mePersistedChild.getEncodedKey() = "agttYXJrZXQtdHJlZXISCxIMSXRlbUNhdGVnb3J5GAUM".

24-Feb-2013 13:28:07 com.google.appengine.api.datastore.dev.LocalDatastoreService$PersistDatastore persist
INFO: Time to persist datastore: 141 ms

JDO增强版验证

构建过程成功完成输出片段:

datanucleusenhancer:
09:33:00,531 (main) INFO  [DataNucleus.Enhancer] - DataNucleus Enhancer for API "JDO"
09:33:01,125 (main) INFO  [DataNucleus.Enhancer] - DataNucleus Enhancer (version 3.1.1) : Enhancement of classes
DataNucleus Enhancer (version 3.1.1) : Enhancement of classes
09:33:03,281 (main) INFO  [DataNucleus.Enhancer] - Writing class file "[Path]\MyEntity.class" with enhanced definition
[... (N entries in all)]
09:33:04,046 (main) INFO  [DataNucleus.Enhancer] - DataNucleus Enhancer completed with success for [N] classes. Timings : input=1922 ms, enhance=984 ms, total=2906 ms. Consult the log for full details
DataNucleus Enhancer completed with success for [N] classes. Timings : input=1922 ms, enhance=984 ms, total=2906 ms. Consult the log for full details

软件环境

  • 网络服务器:Google App Engine for Java 1.7.5版
  • Web框架:Apache Wicket 6.5.0
  • Java版本:1.6.0_39; Java HotSpot(TM)客户端VM 20.14-b01
  • GAE / J DataNucleus插件版本:2.1.2
  • DataNucleus Access Platform版本:3.1.3
  • JDO版本:3.0.1
  • 操作系统:在x86上运行的Microsoft Windows XP 5.1版
  • IDE:NetBeans 7.2(内部版本201207171143)

2 个答案:

答案 0 :(得分:3)

GAE JDO插件只设置了一个&#34; gae.pk-id&#34; /&#34; gae.pk-name&#34;当字段从数据存储区读入标记有该字段的字段时(只需在SVN中继中搜索,FetchFieldManager是唯一加载它的地方 - 它在进行PUT时不设置它)。不知道它在1.x中做了什么,但所有GAE自己的测试都在2.x中传递,就像在1.x中那样。但那个&#34;功能&#34;无论如何,这不是标准的JDO,所以对我没什么兴趣。

JDO提供lifecycle listener,您可以轻松设置 postStore 回调,并在其中设置一些字段(并且不依赖于AppEngine特定的&#34;功能& #34;。)

答案 1 :(得分:2)

受到@ DataNucleus评论的启发,我以一种模糊的相似精神进行了解决。下面的解决方法对我有用,但我发现我的潜在问题仍然存在。

使用编码密钥字符串的(只读)数字ID的所有持久性实体都需要更改其getID()方法以使用解决方法。

Java代码

我修改了我的ID getter方法(前面给出的)如下:

public Long getID()
{
  Long loResult = DataExchange.getIDFromEKSIfIDIsNull(loID, sEncodedKey);
  return loResult;
}

我的DataExchange班级有新方法:

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

/**
 * Get the ID supplied, or get it from the encoded key string if the ID is
 * <code>null</code>.
 * <br/>
 * This method is necessary since JDO version 3.0.1 introduces a long delay
 * between entity first persistence and ID availability using the DataNucleus
 * GAE primary key ID plug-in.
 * @param loID
 *   The persistent entity ID.
 *   This may be <code>null</code> if the entity has been persisted for the
 *   first time but its generation is delayed (a big hello to JDO version
 *   3.0.1).
 * @param sEncodedKey
 *   The persistent entity encoded key string.
 *   This should be not <code>null</code> if the entity has been persisted.
 * @return
 *   <ul>
 *     <li>
 *       If the persistent entity ID supplied is not <code>null</code>
 *       then return it
 *     </li>
 *     <li>
 *       else if the encoded key string is not <code>null</code> then extract
 *       the ID and return it
 *     </li>
 *     <li>
 *       else return <code>null</code>.
 *     </li>
 *   </ul>
 */
public static Long getIDFromEKSIfIDIsNull(Long loID, String sEncodedKey)
{
  Long loResult = null;

  if (loID != null)
    loResult = loID;
  else if (sEncodedKey != null)
  {
    Key key = KeyFactory.stringToKey(sEncodedKey);
    if (key != null)
    {
      long loIDFromEKS = key.getId();
      loResult = Long.valueOf(loIDFromEKS);
    }
  }

  return loResult;
}