ORMLite : Internal DAO object is null

有些话、适合烂在心里 提交于 2019-12-19 09:55:37

问题


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

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

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

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


回答1:


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.

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

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.

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.




回答2:


I solved this problem like Gray suggested: pass the primary key attribute in the Bundle and then obtain the object again from the database in the destination Activity:

Example:

Let's suppose I want to pass a Person object and that I've declared Person.name as:

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

Then:

ActivityA

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

ActivityB

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

And there you are, your Collection will be refreshed.



来源:https://stackoverflow.com/questions/20629985/ormlite-internal-dao-object-is-null

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