【问题标题】:ORMLite : Internal DAO object is nullORMLite:内部 DAO 对象为空
【发布时间】:2013-12-17 09:08:47
【问题描述】:

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

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

我有一个名为 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;
    }

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

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 中循环时,应用程序在尝试访问子区域时崩溃:

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

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

【问题讨论】:

    标签: java android foreign-key-relationship ormlite


    【解决方案1】:

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

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

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

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

    如果您需要获取Zone 子代,您将不得不在反序列化实体之后调用dao.refresh(),或者通过zoneDao 自己进行查询。

    【讨论】:

    • 已经尝试过刷新但没有工作...我改变了我的数据库模型它工作得更好但没有自我关系。
    • 这个问题对@Max 有帮助吗? stackoverflow.com/questions/20802976/…
    • 调用refresh 确实确实有效,在您从中获取foreignCollection的对象上调用它,您通过Extras发送的对象
    【解决方案2】:

    我按照 Gray 的建议解决了这个问题:在 Bundle 中传递主键属性,然后在目标 Activity 中再次从数据库中获取对象:

    例子:

    假设我想传递一个 Person 对象,并且我已将 Person.name 声明为:

    @DatabaseField (columnName ="name")
    private String name;
    

    然后:

    活动A

    Intent intent = new Intent(ActivityA.this, ActivityB.class);
    Bundle bundle = new Bundle();
    bundle.putString("NAME" Person.getName());
    intent.putExtras(bundle);
    

    活动B

    String name = getIntent().getExtras().getString("NAME"));
    Person p = getHelper().getPersonDao().queryForEq("name", name);
    

    然后,您的收藏将被刷新。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-06
      • 2017-02-26
      • 1970-01-01
      • 2016-10-05
      • 2014-05-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多