超级类类型的ArrayList [英] ArrayList of super class type

查看:69
本文介绍了超级类类型的ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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> 

我创建了超类:

    @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数组列表的类:

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

我的定义列表"可以包含定义"或子定义"类型的对象.我创建一个Master对象并将其持久化.

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();

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

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.

存储对象的图形表示:

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

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

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

非常感谢

推荐答案

所有文档,均符合

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

这篇关于超级类类型的ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆