繁体   English   中英

超级类类型的ArrayList

[英]ArrayList of super class type

我在我的项目中使用mongodb-datanucleus。 我将jdoconfig.xml配置如下:

    <persistence-manager-factory name="mongodb-factory">
    <property name="javax.jdo.PersistenceManagerFactoryClass" value="org.datanucleus.api.jdo.JDOPersistenceManagerFactory" />
    <property name="javax.jdo.option.ConnectionURL" value="mongodb:localhost/test" />
    <property name="javax.jdo.option.Mapping" value="mongodb" />
    <property name="javax.jdo.option.ConnectionUserName" value="username" />
    <property name="javax.jdo.option.ConnectionPassword" value="psw" />
    <property name="javax.jdo.option.Optimistic" value="false" />
    <property name="datanucleus.autoCreateSchema" value="true" /> 
    <property name="datanucleus.DetachAllOnCommit" value="true" />
    <property name="datanucleus.DetachOnClose" value="true" />
    </persistence-manager-factory> 

我创建超类:

    @PersistenceCapable(detachable="true")
    public class Definition implements Serializable {
        private String label;
    }

我创建一个子类:

    @PersistenceCapable(detachable="true")
    public class SubDefinition extends Definition implements Serializable {
        private String label;
    }

然后,我创建一个类,用于存储Definition的数组列表:

    @PersistenceCapable(detachable="true")
    public class Master implements Serializable {
        @Persistent(defaultFetchGroup="true")
        @Element(dependent = "true")
        private List<Definition> subDef;
    }

我的定义列表可以包含类型为Definition或SubDefinition的对象。 我创建一个Master对象并将其持久化。

当我从数据库中检索对象时,就会发生问题:

    Transaction tx = pm.currentTransaction();
    tx.begin();
    Query query = pm.newQuery();
    query.setClass(Master.class);
    Collection<Master> masterList = (Collection<Master>)query.execute();
    tx.commit();

如果不重新启动服务器,代码将检索正确的对象,“ subDef”列表已正确加载。 但是,重新启动服务器数据库后,此对象未正确加载。 变量“ subDef”包含一个空数组。 它应该包含2个子元素。

每次我重新启动服务器后,都会发生此问题。 之后,我重新启动一些代码以使数组为空。 这不是我的代码之一。

如果我检入数据库,则两个子元素都存在,但不再与其父级链接。 在我保留对象之后,直接在数据库中正确存在了该关系。

存储对象的图形表示:

    Master
      ->subDef
        ->Definition (children 1)
        ->Definition (children 2)

为什么会有这个问题? 也许不允许创建超类列表?

非常感谢,

根据http://www.datanucleus.org/products/accessplatform_3_1/jdo/fetchgroup.html#static关于“提取深度”的部分,全部包含在文档中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM