关于GreenDao 一对多关系中 新增数据的问题

在一对一或一对多关系中, 若是新建一个对象 其中的子对象是不会建立的 ,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 this

我测试了下对象

/**
 * 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 是能够在建立对象的, 其实也不算是建立对象而是在数据库中查不到相应的数据。get

注释上说 不要咱们本身去建立, 而是在greendao 查询的时候由greendao 来肯定的hash

因此咱们能够 在构造方法中加入 it

recordChoice = new ArrayList<>();

这样来初始化数据 io

可是这样greendao 也会访问, 而报错, 而后加上 List

@Keep

就万事大吉了方法

相关文章
相关标签/搜索