GreenDao使用说明(四)特殊的单表

我们在做系统的时候,有时间会遇到单表自循环的情况,最常见的就是省市信息表,它们通过parentid来确定父子关系,这就是一种比较特殊的1:n的关系,我们来看一下,在GreenDao中是如何实现的。

      一,我们先要MyDaoGenerator.Java中添加这个新的bean

     

[java]   view plain  copy
  1. Entity areaBean = schema.addEntity("Areas");  
  2. areaBean.implementsSerializable();  
  3. areaBean.addIdProperty();  
  4. areaBean.addStringProperty("areaName");  
  5. Property parentId = areaBean.addLongProperty("parentId").getProperty();  
  6. areaBean.addToOne(areaBean,parentId).setName("parent");  
  7. areaBean.addToMany(areaBean,parentId).setName("children");  

     看到了吧,就是自己和自己相连,即是1,又是n

     别忘了,修改上面的Schema schema = new Schema(4, "greendao"); 告诉系统,我们升级了,有新表进来。

     二,修改THDevOpenHelper.java,我们继承的这个类

           

[java]   view plain  copy
  1.  @Override  
  2.  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {  
  3.      switch (oldVersion) {  
  4.          case 4:  
  5.              //创建新表,注意createTable()是静态方法  
  6.              //infosDao.createTable(db, true);  
  7.              //infoTypeDao.createTable(db,true);  
  8.              AreasDao.createTable(db,true);  
  9.   
  10.              // 加入新字段  
  11.              // db.execSQL("ALTER TABLE 'moments' ADD 'audio_path' TEXT;");  
  12.   
  13.              // TODO  
  14.              break;  
  15.      }  
  16.  }  
  17.   
  18. 三, 在Gradle面板中,运行MyDaoGenerator,为其生成新的表和操作层  
  19.     运行后的结果:  
  20.     <img src="https://img-blog.csdn.net/20160106111447135?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />  

             帮我们生成了相应了Areas.java和AreasDao.java两个类,我们来看一下,Areas.java中的两段代在码:

            

[java]   view plain  copy
  1. /** To-one relationship, resolved on first access. */  
  2. public Areas getParent() {  
  3.     Long __key = this.parentId;  
  4.     if (parent__resolvedKey == null || !parent__resolvedKey.equals(__key)) {  
  5.         if (daoSession == null) {  
  6.             throw new DaoException("Entity is detached from DAO context");  
  7.         }  
  8.         AreasDao targetDao = daoSession.getAreasDao();  
  9.         Areas parentNew = targetDao.load(__key);  
  10.         synchronized (this) {  
  11.             parent = parentNew;  
  12.             parent__resolvedKey = __key;  
  13.         }  
  14.     }  
  15.     return parent;  
  16. }  

[java]   view plain  copy
  1. /** To-many relationship, resolved on first access (and after reset). Changes to to-many relations are not persisted, make changes to the target entity. */  
  2. public List<Areas> getChildren() {  
  3.     if (children == null) {  
  4.         if (daoSession == null) {  
  5.             throw new DaoException("Entity is detached from DAO context");  
  6.         }  
  7.         AreasDao targetDao = daoSession.getAreasDao();  
  8.         List<Areas> childrenNew = targetDao._queryAreas_Children(id);  
  9.         synchronized (this) {  
  10.             if(children == null) {  
  11.                 children = childrenNew;  
  12.             }  
  13.         }  
  14.     }  
  15.     return children;  
  16. }  

      这两段代码,我不说,大家也知道是干什么用的了。就这么简单,我们就实现了单表1:n的结构,你怎么应用。就和上一篇文章中的用法一样。这里就不多说了

猜你喜欢

转载自blog.csdn.net/zhourui_1021/article/details/74230943
今日推荐