簡體   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