GreenDAO: Entity is detached from DAO context

隐身守侯 提交于 2019-12-10 17:47:25

问题


I use GreenDAO. But, throws this exception:

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

In the generated code, i find this code.

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

Anyone know when the GreenDAO call it? Also, how to trigger it to call.

Thanks.


回答1:


I find solution.

The greenDAO call __setDaoSession internally when you call loadDeep and queryDeep. After calling these method, the one to many or one to one relation is built up. If you just use SQLiteDatabase to query your result, you just get your data but the relationship is not built up.

In detail, you can go this site

Thanks.




回答2:


To complete King Wu answer, It's the same probleme when you instantiate your object yourself. To solve the probleme juste set the daoSession to the object

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

and after you can do

product.getCategory()


来源:https://stackoverflow.com/questions/15611564/greendao-entity-is-detached-from-dao-context

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