ORMLite:内部 DAO 对象为空

我正在使用 ORMLite,尝试使用 ForeignCollectionKey 但我收到以下错误:

I'm using ORMLite, trying to use the ForeignCollectionKey but I got the following error :

内部 DAO 对象为空.如果 LazyCollections 已反序列化,则无法使用.

Internal DAO object is null. LazyCollections cannot be used if they have been deserialized.

我有一个名为 Zone 的对象:

I've my object named Zone :

public class Zone implements Serializable {

    private static final long serialVersionUID = 1L;
    public static final String ZONE_ID = "id"; 
    public static final String ZONE_PARENT_ID = "parentZoneId";

    @DatabaseField(generatedId=true)
    private int id;
    @DatabaseField()
    String name;
    @DatabaseField(foreign=true, foreignAutoRefresh = true)
    Zone parentZone;

    @ForeignCollectionField(foreignFieldName = "parentZone", eager = true)
    private ForeignCollection<Zone> zoneChild;

    public Zone() {
        // TODO Auto-generated constructor stub
    }
    public ForeignCollection<Zone> getZoneChild() {
        return zoneChild;
    }
    public void setZoneChild(ForeignCollection<Zone> zoneChild) {
        this.zoneChild = zoneChild;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

在一个类中,我正在使用递归方法来获取我所有的区域子对象:

In a class i'm doing a recursive method to get all my zone child objects :

public void getZone(Zone zone, Dao<Zone, Integer> tempZoneDao){
    ZoneListEntity zoneEntity = new ZoneListEntity();
    zoneEntity.setName(zone.getName());
    zoneEntity.setNiveau(0);
    zoneEntity.setZone(zone);
    mainZoneList.add(zoneEntity);

    List<Zone> childList = new ArrayList<Zone>(zone.getZoneChild());
    //set rootZone's children as ZoneListEntity
    for(Zone currentZone : childList){
        ZoneListEntity zoneGroup = new ZoneListEntity();
        zoneGroup.setName(currentZone.getName());
        zoneGroup.setZone(currentZone);
        System.out.println("Zone : "+currentZone.getName());
        getZone(currentZone, tempZoneDao);
    }
}

当我第一次进入我的 getZone 时,一切顺利.然后,当我在 getZone 中循环时,应用程序在尝试访问子区域时崩溃:

When i'm entering for the first time in my getZone, everything going well. Then when I loop in getZone the application crashes trying to access to the child zone :

List<Zone> childList = new ArrayList<Zone>(zone.getZoneChild());

你有什么想法吗?我的模型构造正确吗?谢谢

Do you have any ideas ? Is my model construction right ? Thanks

推荐答案

你有什么想法吗?我的模型构造正确吗?谢谢

Do you have any ideas ? Is my model construction right ? Thanks

所以异常消息试图解释发生了什么.我不确定如何改进.

So the exception message is trying to explain what is going on. I'm not sure how it can be improved.

内部 DAO 对象为空.如果 LazyCollections 已反序列化,则无法使用.

Internal DAO object is null. LazyCollections cannot be used if they have been deserialized.

您正在尝试访问 zoneChild,这是一个已反序列化的 ForeignCollection.由于它已被反序列化,因此无法重新建立所有底层数据库配置和连接.我猜当它存储在 Android Bundle 中时会发生这种情况?我不确定这是否是唯一的情况.

You are trying to access zoneChild which is a ForeignCollection that has been deserialized. Since it has been deserialized all of the underlying database configurations and connections could not be reestablished. I guess this can happen when it stored in an Android Bundle? I'm not sure if this is the only case.

如果您需要获得 Zone 子项,您将不得不在 after 实体上调用 dao.refresh()反序列化它或通过执行 zoneDao 自己进行查询.

If you need to get the Zone children you are going to have to either call dao.refresh() on the entity after you deserialize it or do the query yourself by doing the zoneDao.

相关文章