我的问题
我正在使用新的 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) 客户端虚拟机 20.14-b01
- GAE/J DataNucleus 插件版本:2.1.2
- DataNucleus 访问平台版本:3.1.3
- JDO 版本:3.0.1
- 操作系统:在 x86 上运行的 Microsoft Windows XP 5.1 版
- IDE:NetBeans 7.2(内部版本 201207171143)