GreenDAO:实体与DAO上下文分离

时间:2013-03-25 09:33:12

标签: android dao greendao

我使用GreenDAO。但是,抛出这个例外:

"de.greenrobot.dao.DaoException: Entity is detached from DAO context".

在生成的代码中,我找到了这段代码。

/** called by internal mechanisms, do not call yourself. */
public void __setDaoSession(DaoSession daoSession) {
    this.daoSession = daoSession;
    myDao = daoSession != null ? daoSession.getPeerGroupDao() : null;
}

有人知道GreenDAO什么时候打电话吗?另外,如何触发它来调用。

感谢。

2 个答案:

答案 0 :(得分:7)

我找到了解决方案。

当你调用loadDeep和queryDeep时,greenDAO在内部调用__setDaoSession。在调用这些方法之后,建立一对多或一对一的关系。如果您只是使用SQLiteDatabase来查询结果,那么您只需获取数据,但不会建立关系。

详细说明,您可以执行此site

感谢。

答案 1 :(得分:1)

要完成吴王的回答, 当您自己实例化对象时,问题就出现了同样的问题。 要解决问题juste,请将daoSession设置为对象

foodtype    amount
Fruit       3
Meat        0
Vegetable   2

然后你就可以了

Product product = new Product(null, 1L, categoryId, null);
product.__setDaoSession(MyApplication.getDaoSession());