ArrayList of super class type

别来无恙 提交于 2019-12-12 01:09:49

问题


I use mongodb-datanucleus in my project. I configure my jdoconfig.xml as follow :

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

I create super class :

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

I create a sub class :

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

Then, I create a class that store an array list of Definition :

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

My List of Definition can contain objects of type Definition or SubDefinition. I create a Master object and persist it.

The problem happens when I retrieve my object form the database :

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

If I don't restart my server, the code retrieves the correct object, my "subDef" list is correctly loaded. However, after I restart my server-database, this object is not correctly loaded. The variable "subDef" contains an empty array. It should contains 2 sub elements.

This problem happens after each time I restart my server. After, I restart some code make the array empty. It's not one of my code.

If I check into the database, the both sub elements are present but not linking any more with their parent. Directly after I persist the object, the relation are correctly present into the DB.

Graphical representation of the stored object :

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

Why do I have this problem? Maybe creating a list of super class is not allow?

Thanks a lot,


回答1:


All in the docs, as per http://www.datanucleus.org/products/accessplatform_3_1/jdo/fetchgroup.html#static section on "Fetch Depth".



来源:https://stackoverflow.com/questions/12955028/arraylist-of-super-class-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!