在一对一或一对多关系中, 如果新建一个对象 其中的子对象是不会创建的 ,GreenDao 为我们提供了相应的get方法
例如
@Generated(hash = 1538855097) public Person getPerson() { String __key = this.personInfoId; if (person__resolvedKey == null || person__resolvedKey != __key) { final DaoSession daoSession = this.daoSession; if (daoSession == null) { throw new DaoException("Entity is detached from DAO context"); } PersonDao targetDao = daoSession.getPersonDao(); Person personNew = targetDao.load(__key); synchronized (this) { person = personNew; person__resolvedKey = __key; } } return person; }
大家可以看到 需要设置daoSession
我测试了下
/** * called by internal mechanisms, do not call yourself. */ @Generated(hash = 1722282747) public void __setDaoSession(DaoSession daoSession) { this.daoSession = daoSession; myDao = daoSession != null ? daoSession.getPersonInfoDao() : null; }
手工设置了下DaoSession 是可以在创建对象的, 其实也不算是创建对象而是在数据库中查不到相应的数据。
注释上说 不要我们自己去创建, 而是在greendao 查询的时候由greendao 来确定的
所以我们可以 在构造方法中加入
recordChoice = new ArrayList<>();
这样来初始化数据
但是这样greendao 也会访问, 而报错, 然后加上
@Keep
就万事大吉了