GreenDao的简单使用说明(四)特殊的单表1:n
2016-01-06 11:18
387 查看
我们在做系统的时候,有时间会遇到单表自循环的情况,最常见的就是省市信息表,它们通过parentid来确定父子关系,这就是一种比较特殊的1:n的关系,我们来看一下,在GreenDao中是如何实现的。
一,我们先要MyDaoGenerator.java中添加这个新的bean
看到了吧,就是自己和自己相连,即是1,又是n
别忘了,修改上面的Schema schema = new Schema(4, "greendao"); 告诉系统,我们升级了,有新表进来。
二,修改THDevOpenHelper.java,我们继承的这个类
帮我们生成了相应了Areas.java和AreasDao.java两个类,我们来看一下,Areas.java中的两段代在码:
这两段代码,我不说,大家也知道是干什么用的了。就这么简单,我们就实现了单表1:n的结构,你怎么应用。就和上一篇文章中的用法一样。这里就不多说了
一,我们先要MyDaoGenerator.java中添加这个新的bean
Entity areaBean = schema.addEntity("Areas"); areaBean.implementsSerializable(); areaBean.addIdProperty(); areaBean.addStringProperty("areaName"); Property parentId = areaBean.addLongProperty("parentId").getProperty(); areaBean.addToOne(areaBean,parentId).setName("parent"); areaBean.addToMany(areaBean,parentId).setName("children");
看到了吧,就是自己和自己相连,即是1,又是n
别忘了,修改上面的Schema schema = new Schema(4, "greendao"); 告诉系统,我们升级了,有新表进来。
二,修改THDevOpenHelper.java,我们继承的这个类
@Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { switch (oldVersion) { case 4: //创建新表,注意createTable()是静态方法 //infosDao.createTable(db, true); //infoTypeDao.createTable(db,true); AreasDao.createTable(db,true); // 加入新字段 // db.execSQL("ALTER TABLE 'moments' ADD 'audio_path' TEXT;"); // TODO break; } } 三, 在Gradle面板中,运行MyDaoGenerator,为其生成新的表和操作层 运行后的结果: <img src="http://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中的两段代在码:
/** To-one relationship, resolved on first access. */ public Areas getParent() { Long __key = this.parentId; if (parent__resolvedKey == null || !parent__resolvedKey.equals(__key)) { if (daoSession == null) { throw new DaoException("Entity is detached from DAO context"); } AreasDao targetDao = daoSession.getAreasDao(); Areas parentNew = targetDao.load(__key); synchronized (this) { parent = parentNew; parent__resolvedKey = __key; } } return parent; }
/** To-many relationship, resolved on first access (and after reset). Changes to to-many relations are not persisted, make changes to the target entity. */ public List<Areas> getChildren() { if (children == null) { if (daoSession == null) { throw new DaoException("Entity is detached from DAO context"); } AreasDao targetDao = daoSession.getAreasDao(); List<Areas> childrenNew = targetDao._queryAreas_Children(id); synchronized (this) { if(children == null) { children = childrenNew; } } } return children; }
这两段代码,我不说,大家也知道是干什么用的了。就这么简单,我们就实现了单表1:n的结构,你怎么应用。就和上一篇文章中的用法一样。这里就不多说了
相关文章推荐
- 使用clang命令行工具/终端直接 编译链接Objective-C应用程序
- AnimationDrawabl的使用
- eclipse git 解决冲突
- 在 Golang 中使用 Protobuf
- SpringMVC学习系列之 基于Hibernate+Spring+Spring MVC+Bootstrap的管理系统实现
- 兰州烧饼(南阳oj779)
- PHP判断PC端或移动端
- 这是最坏的时代,也是最好的时代
- std::priority_queue
- android中的AIDL进程间通信
- BlockingQueue简介及使用
- CTypedPtrList
- Clojure环境的搭建
- [转载] 之前碰过的木马分析
- onCreate的savedInstanceState和onRestoreInstanceState 方法的区别。
- Java 实验:常用类
- SecureCRT的使用
- ios原生api识别二维码图片
- cas 服务器安装
- UIWindow