Java JDO PersistentManager的makePersistent是否不足以创建子元素?

Java JDO PersistentManager的makePersistent是否不足以创建子元素?,java,jdo,Java,Jdo,我有一些这样的代码 class root extends DomainObject{ Collection childElements; Collection childElements2; ..... //define other child element collections Collection getChildElements(){ return childElements; } // define other childElements getter/setter

我有一些这样的代码

class root extends DomainObject{
Collection childElements;
Collection childElements2;
.....
//define other child element collections


Collection getChildElements(){
 return childElements;
}
// define other childElements getter/setter
      <class name="root" identity-type="datastore" detachable="true" persistence-capable-superclass="com.xxx.DomainObject">
        <inheritance strategy="new-table"/>
        <field name="childElements" persistence-modifier="persistent" default-fetch-group="true">
            <collection element-type="com.my.CustomClass" />
            <join/>
            <order column="idx"/>
            <extension vendor-name="jpox" key="cache-lazy-loading" value="false"/>
        </field>    
class CustomClass extends DomainObject{
String name;
Collection otherElements;
.....


String getName(){
return name;
}    
Collection getOtherElements(){
 return otherElements;
}
persistenceManager().currentTransaction().begin();
CustomClass customObj = new CustomClass();
customobj.setName("someName");
//set other values    
persistenceManager.makePeristent(customObj);//persist
SomeUtil.getRoot().getChildElements().add(customObj);//add to its owner
persistenceManager.currentTransaction().commit();
 Query q = persistenceManager.newQuery( root.class );
 Collection c = (Collection)q.execute();
  persistenceManager.retrieveAll( c );
   return (root)c.iterator().next();
我的package.jdo定义了如下所示的映射

class root extends DomainObject{
Collection childElements;
Collection childElements2;
.....
//define other child element collections


Collection getChildElements(){
 return childElements;
}
// define other childElements getter/setter
      <class name="root" identity-type="datastore" detachable="true" persistence-capable-superclass="com.xxx.DomainObject">
        <inheritance strategy="new-table"/>
        <field name="childElements" persistence-modifier="persistent" default-fetch-group="true">
            <collection element-type="com.my.CustomClass" />
            <join/>
            <order column="idx"/>
            <extension vendor-name="jpox" key="cache-lazy-loading" value="false"/>
        </field>    
class CustomClass extends DomainObject{
String name;
Collection otherElements;
.....


String getName(){
return name;
}    
Collection getOtherElements(){
 return otherElements;
}
persistenceManager().currentTransaction().begin();
CustomClass customObj = new CustomClass();
customobj.setName("someName");
//set other values    
persistenceManager.makePeristent(customObj);//persist
SomeUtil.getRoot().getChildElements().add(customObj);//add to its owner
persistenceManager.currentTransaction().commit();
 Query q = persistenceManager.newQuery( root.class );
 Collection c = (Collection)q.execute();
  persistenceManager.retrieveAll( c );
   return (root)c.iterator().next();
现在创建com.my.CustomClass对象的代码如下所示

class root extends DomainObject{
Collection childElements;
Collection childElements2;
.....
//define other child element collections


Collection getChildElements(){
 return childElements;
}
// define other childElements getter/setter
      <class name="root" identity-type="datastore" detachable="true" persistence-capable-superclass="com.xxx.DomainObject">
        <inheritance strategy="new-table"/>
        <field name="childElements" persistence-modifier="persistent" default-fetch-group="true">
            <collection element-type="com.my.CustomClass" />
            <join/>
            <order column="idx"/>
            <extension vendor-name="jpox" key="cache-lazy-loading" value="false"/>
        </field>    
class CustomClass extends DomainObject{
String name;
Collection otherElements;
.....


String getName(){
return name;
}    
Collection getOtherElements(){
 return otherElements;
}
persistenceManager().currentTransaction().begin();
CustomClass customObj = new CustomClass();
customobj.setName("someName");
//set other values    
persistenceManager.makePeristent(customObj);//persist
SomeUtil.getRoot().getChildElements().add(customObj);//add to its owner
persistenceManager.currentTransaction().commit();
 Query q = persistenceManager.newQuery( root.class );
 Collection c = (Collection)q.execute();
  persistenceManager.retrieveAll( c );
   return (root)c.iterator().next();
SomeUtil中getRoot()的代码如下所示

class root extends DomainObject{
Collection childElements;
Collection childElements2;
.....
//define other child element collections


Collection getChildElements(){
 return childElements;
}
// define other childElements getter/setter
      <class name="root" identity-type="datastore" detachable="true" persistence-capable-superclass="com.xxx.DomainObject">
        <inheritance strategy="new-table"/>
        <field name="childElements" persistence-modifier="persistent" default-fetch-group="true">
            <collection element-type="com.my.CustomClass" />
            <join/>
            <order column="idx"/>
            <extension vendor-name="jpox" key="cache-lazy-loading" value="false"/>
        </field>    
class CustomClass extends DomainObject{
String name;
Collection otherElements;
.....


String getName(){
return name;
}    
Collection getOtherElements(){
 return otherElements;
}
persistenceManager().currentTransaction().begin();
CustomClass customObj = new CustomClass();
customobj.setName("someName");
//set other values    
persistenceManager.makePeristent(customObj);//persist
SomeUtil.getRoot().getChildElements().add(customObj);//add to its owner
persistenceManager.currentTransaction().commit();
 Query q = persistenceManager.newQuery( root.class );
 Collection c = (Collection)q.execute();
  persistenceManager.retrieveAll( c );
   return (root)c.iterator().next();
我不明白的是,为什么我们需要向所有者添加这个新的自定义对象<代码>SomeUtil.getRoot().getChildElements().add(customObj) 删除代码看起来也很相似,即首先从其所有者的集合中删除对象,然后调用PersistenceManager的
deletePersistent

我的问题是 我们真的需要显式地将这个对象添加到父对象吗?仅仅
makePeristent()
还不够吗?我之所以这么问,是因为我看到这个“getRoot()”会影响性能(即使底层对象是延迟加载的,但当这些集合的数量很大时,也会影响性能)

在运行时,我们使用此“根”对象的缓存/可分离副本(例如深度克隆),并且仅从此“根”对象检索任何必需的元素。如果对数据库进行了任何修改,则我们将使此缓存无效,并重新加载此根并再次缓存它

删除在父元素中显式添加或删除子元素的代码是否安全?或者,根据我们定义的映射(并且考虑到我们在运行时依赖缓存(克隆)根来检索所有子元素的事实),它真的是必需的吗? 请注意,在运行时(深度用户克隆)和创建对象期间,我们没有使用相同的“root”。只是我们在运行时依赖“root”获取其他元素。
请告诉我是否有人处理过这种情况。

JDO规范中定义了通过可达性实现的JDO持久性。DataNucleus日志告诉您什么是持久性以及何时持久化。我强烈建议阅读这两个参考资料

我已经对此进行了测试,并观察到如果您在不了解父元素的情况下添加子元素,则无法使用父元素检索该子元素

persistenceManager().currentTransaction().begin();
CustomClass customObj = new CustomClass();
customobj.setName("someName");
//set other values    
persistenceManager.makePeristent(customObj);//persist
//comment this
//SomeUtil.getRoot().getChildElements().add(customObj);//add to its owner
persistenceManager.currentTransaction().commit();
后来,当我加载父元素时,我发现没有检索到子元素

 Collection c = SomeUtil.getRoot().getChildElements()
 // Iterate over this collection and found that element is not present
正如我前面所说,我们在运行时依赖这个根对象来检索任何子元素。 尽管直接查询孩子们是可行的,但这不是我们的选择

看来我们只能通过将该子级添加到父级来实现这一点。
但我们希望避免这种情况,因为检索父(根)会影响性能(即使是在延迟加载时),因为此根有许多其他子集合元素。在运行时,我们通常加载此根目录一次并缓存干净的副本。

我的问题实际上正好相反。仅当我从父级开始持久化时,通过可达性进行持久化不适用吗?简单地说,如果我先添加子级,有没有办法告诉父级“拥有”此目录?顺便说一句,我们使用的是jpox 1.1,如果您使用的是“jpox 1.1”,那么为什么要将这个问题标记为“datanucleus”,而且显然从2007年开始jpox就不受支持,所以您可以自己使用。这是因为stackoverflow不允许新手创建新标记(令人惊讶的是jpox标记不存在),无论如何,我认为这个问题也适用于datanucleus