问题
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