GreenDao教程(3):一对一,一对多,多对多

来源:互联网 发布:c语言求数组最小值 编辑:程序博客网 时间:2024/06/09 23:54

  • 前言
  • 一对一
    • 1 注解
    • 2 示例
      • 21 实体类定义
      • 22 insert一组数据
      • 23 delete一组数据
    • 3 结语一对一
  • 一对多
    • 1 注解
    • 2 示例
      • 21 实体类定义
      • 22 insert一组数据
    • 3 结语一对多
  • 多对多
    • 1 注解
    • 2 示例
      • 21 实体类定义
      • 22 绑定类定义
      • 23 insert一组数据
    • 3 结语多对多

1. 前言

  • 主要介绍GreenDao 一对一,一对多,多对多应用
  • 这部分还是有些坑的,注意看示例代码中注释即可避开
  • 本文资料来源网络公开资源,并根据个人实践见解纯手打整理,如有错误请随时指出。
  • 本文主要用于个人积累及分享,文中可能引用其他技术大牛文章(仅引用链接不转载),如有侵权请告知必妥善处理。

2. 一对一

2.1. 注解

@ToOne(joinProperty = "childId")

2.2. 示例

  • 这里举一个简单的例子,用户对象UserBean和用户其他信息对象OtherUserInfoBean,每个用户都对应一个用户其他信息
  • 查看以下代码和注释,可以清楚的了解一对一如何使用(自动注入代码可以适当忽略)
  • 以下代码主要包括实体类定义(UserBean.java、OtherUserInfoBean.java),insert一组数据,delete一组数据3个部分

2.2.1. 实体类定义

以下示例为rebuild之后的代码

  • UserBean.java
@Entitypublic class UserBean {    @Id    //必须使用包装类对象类型Long,而非基本类型long    private Long id;    @NotNull    private String name;    private int age;    private String province;    private boolean isMale;    private String idCard;    //ToOne注解使用的joinProperty = "otherUserInfoId"指的是UserBean中自定义的这个otherUserInfoId    private Long otherUserInfoId;    //在insert时,将otherUserInfoId同一对一关联的OtherUserInfoBean的主键id绑定    @ToOne(joinProperty = "otherUserInfoId")    private OtherUserInfoBean otherUserInfoBean;    //-------------------------------自动注入代码部分---开始    @Generated(hash = 4979683)    private transient Long otherUserInfoBean__resolvedKey;    /**     * Used for active entity operations.     */    @Generated(hash = 83707551)    private transient UserBeanDao myDao;    /**     * Used to resolve relations     */    @Generated(hash = 2040040024)    private transient DaoSession daoSession;    @Generated(hash = 359442482)    public UserBean(Long id, @NotNull String name, int age, String province,                    boolean isMale, String idCard, Long otherUserInfoId) {        this.id = id;        this.name = name;        this.age = age;        this.province = province;        this.isMale = isMale;        this.idCard = idCard;        this.otherUserInfoId = otherUserInfoId;    }    @Generated(hash = 1203313951)    public UserBean() {    }    /**     * Convenient call for {@link org.greenrobot.greendao.AbstractDao#refresh(Object)}.     * Entity must attached to an entity context.     */    @Generated(hash = 1942392019)    public void refresh() {        if (myDao == null) {            throw new DaoException("Entity is detached from DAO context");        }        myDao.refresh(this);    }    /**     * Convenient call for {@link org.greenrobot.greendao.AbstractDao#update(Object)}.     * Entity must attached to an entity context.     */    @Generated(hash = 713229351)    public void update() {        if (myDao == null) {            throw new DaoException("Entity is detached from DAO context");        }        myDao.update(this);    }    /**     * Convenient call for {@link org.greenrobot.greendao.AbstractDao#delete(Object)}.     * Entity must attached to an entity context.     */    @Generated(hash = 128553479)    public void delete() {        if (myDao == null) {            throw new DaoException("Entity is detached from DAO context");        }        myDao.delete(this);    }    /**     * called by internal mechanisms, do not call yourself.     */    @Generated(hash = 1952804343)    public void setOtherUserInfoBean(OtherUserInfoBean otherUserInfoBean) {        synchronized (this) {            this.otherUserInfoBean = otherUserInfoBean;            otherUserInfoId = otherUserInfoBean == null ? null : otherUserInfoBean.getId();            otherUserInfoBean__resolvedKey = otherUserInfoId;        }    }    /**     * To-one relationship, resolved on first access.     */    @Generated(hash = 1275005484)    public OtherUserInfoBean getOtherUserInfoBean() {        Long __key = this.otherUserInfoId;        if (otherUserInfoBean__resolvedKey == null || !otherUserInfoBean__resolvedKey.equals(__key)) {            final DaoSession daoSession = this.daoSession;            if (daoSession == null) {                throw new DaoException("Entity is detached from DAO context");            }            OtherUserInfoBeanDao targetDao = daoSession.getOtherUserInfoBeanDao();            OtherUserInfoBean otherUserInfoBeanNew = targetDao.load(__key);            synchronized (this) {                otherUserInfoBean = otherUserInfoBeanNew;                otherUserInfoBean__resolvedKey = __key;            }        }        return otherUserInfoBean;    }    public Long getOtherUserInfoId() {        return this.otherUserInfoId;    }    public void setOtherUserInfoId(Long otherUserInfoId) {        this.otherUserInfoId = otherUserInfoId;    }    public String getIdCard() {        return this.idCard;    }    public void setIdCard(String idCard) {        this.idCard = idCard;    }    public boolean getIsMale() {        return this.isMale;    }    public void setIsMale(boolean isMale) {        this.isMale = isMale;    }    public String getProvince() {        return this.province;    }    public void setProvince(String province) {        this.province = province;    }    public int getAge() {        return this.age;    }    public void setAge(int age) {        this.age = age;    }    public String getName() {        return this.name;    }    public void setName(String name) {        this.name = name;    }    public Long getId() {        return this.id;    }    public void setId(Long id) {        this.id = id;    }    /** called by internal mechanisms, do not call yourself. */    @Generated(hash = 1491512534)    public void __setDaoSession(DaoSession daoSession) {        this.daoSession = daoSession;        myDao = daoSession != null ? daoSession.getUserBeanDao() : null;    }    //-------------------------------自动注入代码部分---结束    @Override    public String toString() {        //toString UserBean一对一关联的otherUserInfoBean时,        //不能直接使用变量otherUserInfoBean,需要调用自动注入生成的getOtherUserInfoBean()获取关联对象        return "UserBean{" +                "id=" + id +                ", name='" + name + '\'' +                ", age=" + age +                ", province='" + province + '\'' +                ", isMale=" + isMale +                ", idCard='" + idCard + '\'' +                ", otherUserInfoId=" + otherUserInfoId +                ", otherUserInfoBean=" + (getOtherUserInfoBean() == null ? "null" : getOtherUserInfoBean().toString()) +                '}';    }
  • OtherUserInfoBean.java
@Entitypublic class OtherUserInfoBean {    @Id    //必须使用包装类对象类型Long,而非基本类型long    private Long id;    private String address;    private String tel;    private Date birthday;    //-------------------------------自动注入代码部分---开始    public Date getBirthday() {        return this.birthday;    }    public void setBirthday(Date birthday) {        this.birthday = birthday;    }    public String getTel() {        return this.tel;    }    public void setTel(String tel) {        this.tel = tel;    }    public String getAddress() {        return this.address;    }    public void setAddress(String address) {        this.address = address;    }    public Long getId() {        return this.id;    }    public void setId(Long id) {        this.id = id;    }    @Generated(hash = 647485055)    public OtherUserInfoBean(Long id, String address, String tel, Date birthday) {        this.id = id;        this.address = address;        this.tel = tel;        this.birthday = birthday;    }    @Generated(hash = 59605238)    public OtherUserInfoBean() {    }    //-------------------------------自动注入代码部分---结束    @Override    public String toString() {        return "OtherUserInfoBean{" +                "id=" + id +                ", address='" + address + '\'' +                ", tel='" + tel + '\'' +                ", birthday=" + birthday +                '}';    }

2.2.2. insert一组数据

以下代码,其中GreenDaoUtil是获取DaoSession的一个单例工具工具类。获取DaoSession可以通过自己的方式获取,但请保持单例。

UserBean user = new UserBean();user.setAge((int) (Math.random() * 100 + 10));user.setIdCard((int) (Math.random() * 10000) + "123456789");if (Math.random() * 100 > 50) {    user.setIsMale(true);} else {    user.setIsMale(false);}user.setName("李看啥" + (int) (Math.random() * 1000));user.setProvince("shanhaijing");Long otherUserInfoId = (long) (Math.random() * 1000);//只setOtherUserInfoId,不setOtherUserInfoBeanuser.setOtherUserInfoId(otherUserInfoId);//insert userGreenDaoUtil.getDaoSession(activity).getUserBeanDao().insert(user);OtherUserInfoBean otherUserInfoBean = new OtherUserInfoBean();//主键id使用user的otherUserInfoIdotherUserInfoBean.setId(otherUserInfoId);otherUserInfoBean.setTel(String.valueOf((i + 100) * 19 + Math.random() * 100));otherUserInfoBean.setAddress("dongshanjing");Date date = new Date();date.setTime((long) (System.currentTimeMillis() - Math.random() * 2000000000));otherUserInfoBean.setBirthday(date);//insert otherUserInfoBeanGreenDaoUtil.getDaoSession(activity).getOtherUserInfoBeanDao().insert(otherUserInfoBean);

2.2.3. delete一组数据

  • 以下代码,其中GreenDaoUtil是获取DaoSession的一个单例工具类。DaoSession可以通过自己的方式获取,但请保持单例。
  • 之所以把简单的delete拿出来说,是因为,delete任意一组一对一关联数据其中一个一部分(比如只delete userBean,不delete otherUserInfoBean),另一部分不受影响
GreenDaoUtil.getDaoSession(activity).getUserBeanDao().deleteByKey(user.getId());

2.3. 结语(一对一)

  • 关于id的绑定,注意以上示例代码注释
  • 关于删除数据,并没有关联删除

3. 一对多

3.1. 注解

@ToMany(referencedJoinProperty = "parentId")

3.2. 示例

  • 举例,LeaderBean和MemberBean,每个leader可能对应多个member
  • 查看以下代码和注释,可以清楚的了解一对多如何使用(自动注入代码可以适当忽略)
  • 以下代码主要包括实体类定义(LeaderBean.java、MemberBean.java),insert一组数据2个部分

3.2.1. 实体类定义

以下示例为rebuild之后的代码

  • LeaderBean.java
/** * 一对多 * Created by songyuan on 2017/3/31. */@Entitypublic class LeaderBean {    @Id    private Long id;    private String name;    //此处的leaderId是在MemberBean中定义的一个变量(请看下面的MemberBean.java)    @ToMany(referencedJoinProperty = "leaderId")    private List<MemberBean> memberBeanList;    //-------------------------------自动注入代码部分---开始    /**     * Convenient call for {@link org.greenrobot.greendao.AbstractDao#refresh(Object)}.     * Entity must attached to an entity context.     */    @Generated(hash = 1942392019)    public void refresh() {        if (myDao == null) {            throw new DaoException("Entity is detached from DAO context");        }        myDao.refresh(this);    }    /**     * Convenient call for {@link org.greenrobot.greendao.AbstractDao#update(Object)}.     * Entity must attached to an entity context.     */    @Generated(hash = 713229351)    public void update() {        if (myDao == null) {            throw new DaoException("Entity is detached from DAO context");        }        myDao.update(this);    }    /**     * Convenient call for {@link org.greenrobot.greendao.AbstractDao#delete(Object)}.     * Entity must attached to an entity context.     */    @Generated(hash = 128553479)    public void delete() {        if (myDao == null) {            throw new DaoException("Entity is detached from DAO context");        }        myDao.delete(this);    }    /**     * Resets a to-many relationship, making the next get call to query for a fresh result.     */    @Generated(hash = 73665005)    public synchronized void resetMemberBeanList() {        memberBeanList = null;    }    /**     * To-many relationship, resolved on first access (and after reset).     * Changes to to-many relations are not persisted, make changes to the target entity.     */    @Generated(hash = 1515955432)    public List<MemberBean> getMemberBeanList() {        if (memberBeanList == null) {            final DaoSession daoSession = this.daoSession;            if (daoSession == null) {                throw new DaoException("Entity is detached from DAO context");            }            MemberBeanDao targetDao = daoSession.getMemberBeanDao();            List<MemberBean> memberBeanListNew = targetDao._queryLeaderBean_MemberBeanList(id);            synchronized (this) {                if (memberBeanList == null) {                    memberBeanList = memberBeanListNew;                }            }        }        return memberBeanList;    }    /**     * Used for active entity operations.     */    @Generated(hash = 703242250)    private transient LeaderBeanDao myDao;    /**     * Used to resolve relations     */    @Generated(hash = 2040040024)    private transient DaoSession daoSession;    public String getName() {        return this.name;    }    public void setName(String name) {        this.name = name;    }    public Long getId() {        return this.id;    }    public void setId(Long id) {        this.id = id;    }    @Generated(hash = 590590377)    public LeaderBean(Long id, String name) {        this.id = id;        this.name = name;    }    @Generated(hash = 1761610276)    public LeaderBean() {    }    /** called by internal mechanisms, do not call yourself. */    @Generated(hash = 1648395956)    public void __setDaoSession(DaoSession daoSession) {        this.daoSession = daoSession;        myDao = daoSession != null ? daoSession.getLeaderBeanDao() : null;    }    //-------------------------------自动注入代码部分---结束    @Override    public String toString() {        String memberBeanListStr = "";        if (getMemberBeanList().size() > 0) {            for (MemberBean memberBean : getMemberBeanList()) {                memberBeanListStr = memberBeanListStr + memberBean.toString() + ";";            }        }else{            memberBeanListStr = "null";        }        return "LeaderBean{" +                "id=" + id +                ", name='" + name + '\'' +                ", memberBeanList=" + memberBeanListStr +                '}';    }}
  • MemberBean.java
/** * Created by songyuan on 2017/3/31. */@Entitypublic class MemberBean {    @Id    private Long id;    //此处自定义leaderId,用于和LeaderBean对应    private Long leaderId;    private String name;    //-------------------------------自动注入代码部分---开始    public String getName() {        return this.name;    }    public void setName(String name) {        this.name = name;    }    public Long getLeaderId() {        return this.leaderId;    }    public void setLeaderId(Long leaderId) {        this.leaderId = leaderId;    }    public Long getId() {        return this.id;    }    public void setId(Long id) {        this.id = id;    }    @Generated(hash = 1935793106)    public MemberBean(Long id, Long leaderId, String name) {        this.id = id;        this.leaderId = leaderId;        this.name = name;    }    @Generated(hash = 1592035565)    public MemberBean() {    }    //-------------------------------自动注入代码部分---结束    @Override    public String toString() {        return "MemberBean{" +                "id=" + id +                ", leaderId=" + leaderId +                ", name='" + name + '\'' +                '}';    }}

3.2.2. insert一组数据

for (int i = 0; i < 2; i++) {    LeaderBean leader = new LeaderBean();    leader.setName("张吃啥" + (int) (Math.random() * 1000));    GreenDaoUtil.getDaoSession(activity).getLeaderBeanDao().insert(leader);    for (int j = 0; j < 3; j++) {        MemberBean memberBean = new MemberBean();        //setLeaderId,值为其所属的leader的id(leaderBean保存完成后,缓存即可获取到数据库中生成的主键id)        memberBean.setLeaderId(leader.getId());        memberBean.setName("赵小" + (int) (Math.random() * 1000));        GreenDaoUtil.getDaoSession(activity).getMemberBeanDao().insert(memberBean);    }}

3.3. 结语(一对多)

  • 关于id的绑定,注意以上示例代码注释
  • delete一个LeaderBean数据,对应的MemberBean数据不会被删除(同一对一的效果)

4. 多对多

4.1. 注解

@JoinEntity(entity = TeacherJoinStudentBean.class,            sourceProperty = "tId",            targetProperty = "sId")

4.2. 示例

  • 举例,TeacherBean和StudentBean,每个teacher可能对应多个student,每个student也可能对应多个teacher,就如同大学的选修课
  • 多对多的实现关键是一个关系绑定类(TeacherJoinStudentBean.java),这个类中只有变量teacherId和studentId,用来记录绑定的关系
  • 查看以下代码和注释,可以清楚的了解多对多如何使用(自动注入代码可以适当忽略)
  • 以下代码主要包括实体类定义(TeacherBean.java、StudentBean.java),绑定类定义(TeacherJoinStudentBean.java),insert一组数据3个部分

4.2.1. 实体类定义

以下示例为rebuild之后的代码

  • TeacherBean.java
@Entitypublic class TeacherBean {    @Id    private Long id;    //entity指的是绑定类    //sourceProperty填写绑定类中标示自身的id,此处为tId,指TeacherBean的id    //targetProperty填写绑定类中标示关联类的id,此处为sId,指StudentBean的id    @ToMany    @JoinEntity(entity = TeacherJoinStudentBean.class,            sourceProperty = "tId",            targetProperty = "sId")    private List<StudentBean> studentBeanList;    //-------------------------------自动注入代码部分---开始    /**     * Convenient call for {@link org.greenrobot.greendao.AbstractDao#refresh(Object)}.     * Entity must attached to an entity context.     */    @Generated(hash = 1942392019)    public void refresh() {        if (myDao == null) {            throw new DaoException("Entity is detached from DAO context");        }        myDao.refresh(this);    }    /**     * Convenient call for {@link org.greenrobot.greendao.AbstractDao#update(Object)}.     * Entity must attached to an entity context.     */    @Generated(hash = 713229351)    public void update() {        if (myDao == null) {            throw new DaoException("Entity is detached from DAO context");        }        myDao.update(this);    }    /**     * Convenient call for {@link org.greenrobot.greendao.AbstractDao#delete(Object)}.     * Entity must attached to an entity context.     */    @Generated(hash = 128553479)    public void delete() {        if (myDao == null) {            throw new DaoException("Entity is detached from DAO context");        }        myDao.delete(this);    }    /**     * Resets a to-many relationship, making the next get call to query for a fresh result.     */    @Generated(hash = 747728825)    public synchronized void resetStudentBeanList() {        studentBeanList = null;    }    /**     * To-many relationship, resolved on first access (and after reset).     * Changes to to-many relations are not persisted, make changes to the target entity.     */    @Generated(hash = 767851859)    public List<StudentBean> getStudentBeanList() {        if (studentBeanList == null) {            final DaoSession daoSession = this.daoSession;            if (daoSession == null) {                throw new DaoException("Entity is detached from DAO context");            }            StudentBeanDao targetDao = daoSession.getStudentBeanDao();            List<StudentBean> studentBeanListNew = targetDao._queryTeacherBean_StudentBeanList(id);            synchronized (this) {                if (studentBeanList == null) {                    studentBeanList = studentBeanListNew;                }            }        }        return studentBeanList;    }    /**     * Used for active entity operations.     */    @Generated(hash = 121856788)    private transient TeacherBeanDao myDao;    /**     * Used to resolve relations     */    @Generated(hash = 2040040024)    private transient DaoSession daoSession;    public Long getId() {        return this.id;    }    public void setId(Long id) {        this.id = id;    }    @Generated(hash = 121852154)    public TeacherBean(Long id) {        this.id = id;    }    @Generated(hash = 1376380279)    public TeacherBean() {    }    /** called by internal mechanisms, do not call yourself. */    @Generated(hash = 1588249045)    public void __setDaoSession(DaoSession daoSession) {        this.daoSession = daoSession;        myDao = daoSession != null ? daoSession.getTeacherBeanDao() : null;    }    //-------------------------------自动注入代码部分---结束    @Override    public String toString() {        return "TeacherBean{" +                "id=" + id +                ", studentBeanList=" + (getStudentBeanList().size() == 0 ? "null" : getStudentBeanListStr()) +                '}';    }    private String getStudentBeanListStr() {        String studentBeanListStr = "";        for (StudentBean studentBean : getStudentBeanList()) {            studentBeanListStr = studentBeanListStr + studentBean.toStringStudentBeanOnly() + ";";        }        return studentBeanListStr;    }    /**     * 用于student打印时调用     * 仅打印TeacherBean的属性,不打印关联的student,否则死循环StackOverflowError     *     * @return     */    public String toStringTeacherBeanOnly() {        return "TeacherBean{" +                "id=" + id +                '}';    }}
  • StudentBean.java
@Entitypublic class StudentBean {    @Id    private Long id;    //entity指的是绑定类    //sourceProperty填写绑定类中标示自身的id,此处为sId,指StudentBean的id    //targetProperty填写绑定类中标示关联类的id,此处为tId,指TeacherBean的id    @ToMany    @JoinEntity(entity = TeacherJoinStudentBean.class,            sourceProperty = "sId",            targetProperty = "tId")    private List<TeacherBean> teacherBeanList;    //-------------------------------自动注入代码部分---开始    /**     * Convenient call for {@link org.greenrobot.greendao.AbstractDao#refresh(Object)}.     * Entity must attached to an entity context.     */    @Generated(hash = 1942392019)    public void refresh() {        if (myDao == null) {            throw new DaoException("Entity is detached from DAO context");        }        myDao.refresh(this);    }    /**     * Convenient call for {@link org.greenrobot.greendao.AbstractDao#update(Object)}.     * Entity must attached to an entity context.     */    @Generated(hash = 713229351)    public void update() {        if (myDao == null) {            throw new DaoException("Entity is detached from DAO context");        }        myDao.update(this);    }    /**     * Convenient call for {@link org.greenrobot.greendao.AbstractDao#delete(Object)}.     * Entity must attached to an entity context.     */    @Generated(hash = 128553479)    public void delete() {        if (myDao == null) {            throw new DaoException("Entity is detached from DAO context");        }        myDao.delete(this);    }    /**     * Resets a to-many relationship, making the next get call to query for a fresh result.     */    @Generated(hash = 832141143)    public synchronized void resetTeacherBeanList() {        teacherBeanList = null;    }    /**     * To-many relationship, resolved on first access (and after reset).     * Changes to to-many relations are not persisted, make changes to the target entity.     */    @Generated(hash = 299074847)    public List<TeacherBean> getTeacherBeanList() {        if (teacherBeanList == null) {            final DaoSession daoSession = this.daoSession;            if (daoSession == null) {                throw new DaoException("Entity is detached from DAO context");            }            TeacherBeanDao targetDao = daoSession.getTeacherBeanDao();            List<TeacherBean> teacherBeanListNew = targetDao._queryStudentBean_TeacherBeanList(id);            synchronized (this) {                if (teacherBeanList == null) {                    teacherBeanList = teacherBeanListNew;                }            }        }        return teacherBeanList;    }    /**     * Used for active entity operations.     */    @Generated(hash = 1251043925)    private transient StudentBeanDao myDao;    /**     * Used to resolve relations     */    @Generated(hash = 2040040024)    private transient DaoSession daoSession;    public Long getId() {        return this.id;    }    public void setId(Long id) {        this.id = id;    }    @Generated(hash = 881285408)    public StudentBean(Long id) {        this.id = id;    }    @Generated(hash = 2097171990)    public StudentBean() {    }    /** called by internal mechanisms, do not call yourself. */    @Generated(hash = 2072577263)    public void __setDaoSession(DaoSession daoSession) {        this.daoSession = daoSession;        myDao = daoSession != null ? daoSession.getStudentBeanDao() : null;    }    //-------------------------------自动注入代码部分---结束    @Override    public String toString() {        String teacherBeanListStr = "";        if (getTeacherBeanList().size() > 0) {            for (TeacherBean teacherBean : getTeacherBeanList()) {                teacherBeanListStr = teacherBeanListStr + teacherBean.toStringTeacherBeanOnly() + ";";            }        } else {            teacherBeanListStr = "null";        }        return "StudentBean{" +                "id=" + id +                ", teacherBeanList=" + (getTeacherBeanList().size() == 0 ? "null" : getTeacherBeanListStr()) +                '}';    }    private String getTeacherBeanListStr() {        String teacherBeanListStr = "";        for (TeacherBean teacherBean : getTeacherBeanList()) {            teacherBeanListStr = teacherBeanListStr + teacherBean.toStringTeacherBeanOnly() + ";";        }        return teacherBeanListStr;    }    /**     * 用于teacher打印时调用     * 仅打印StudentBean的属性,不打印关联的teacher,否则死循环StackOverflowError     *     * @return     */    public String toStringStudentBeanOnly() {        return "StudentBean{" +                "id=" + id +                '}';    }}

4.2.2. 绑定类定义

  • TeacherJoinStudentBean.java

绑定类,包含teacher的id和student的id

@Entitypublic class TeacherJoinStudentBean {    @Id    private Long id;    private Long tId;    private Long sId;    //-------------------------------自动注入代码部分---开始    public Long getSId() {        return this.sId;    }    public void setSId(Long sId) {        this.sId = sId;    }    public Long getTId() {        return this.tId;    }    public void setTId(Long tId) {        this.tId = tId;    }    public Long getId() {        return this.id;    }    public void setId(Long id) {        this.id = id;    }    @Generated(hash = 998000662)    public TeacherJoinStudentBean(Long id, Long tId, Long sId) {        this.id = id;        this.tId = tId;        this.sId = sId;    }    @Generated(hash = 1542025906)    public TeacherJoinStudentBean() {    }    //-------------------------------自动注入代码部分---结束}

4.2.3. insert一组数据

//2个教师和3个学生的关系//教师1,带学生1、2//教师2,带学生1、3//学生1,选修教师1和教师2的课List<TeacherBean> teacherList = new ArrayList<TeacherBean>();for (long i = 1; i < 3; i++) {    TeacherBean teacherBean = new TeacherBean();    teacherBean.setId(i);    teacherList.add(teacherBean);}GreenDaoUtil.getDaoSession(activity).getTeacherBeanDao().insertInTx(teacherList);List<StudentBean> studentList = new ArrayList<StudentBean>();for (long j = 1; j < 4; j++) {    StudentBean studentBean = new StudentBean();    studentBean.setId(j);    studentList.add(studentBean);}GreenDaoUtil.getDaoSession(activity).getStudentBeanDao().insertInTx(studentList);List<TeacherJoinStudentBean> teacherJoinStudentList = new ArrayList<TeacherJoinStudentBean>();//教师1带学生1、2TeacherJoinStudentBean teacherJoinStudentBean1 = new TeacherJoinStudentBean(null,1l,1l);teacherJoinStudentList.add(teacherJoinStudentBean1);TeacherJoinStudentBean teacherJoinStudentBean2 = new TeacherJoinStudentBean(null,1l,2l);teacherJoinStudentList.add(teacherJoinStudentBean2);//教师2带学生1、3TeacherJoinStudentBean teacherJoinStudentBean3 = new TeacherJoinStudentBean(null,2l,1l);teacherJoinStudentList.add(teacherJoinStudentBean3);TeacherJoinStudentBean teacherJoinStudentBean4 = new TeacherJoinStudentBean(null,2l,3l);teacherJoinStudentList.add(teacherJoinStudentBean4);GreenDaoUtil.getDaoSession(activity).getTeacherJoinStudentBeanDao().insertInTx(teacherJoinStudentList);

4.3. 结语(多对多)

  • 关于id的绑定,注意以上示例代码注释
  • 说实话多对多比较坑,感觉没什么使用的必要,可能我还没遇到能用到的项目,如果你有好的例子,欢迎留言链接
5 0
原创粉丝点击