GreenDao使用说明(四)特殊的单表
2017-07-03 17:29
239 查看
我们在做系统的时候,有时间会遇到单表自循环的情况,最常见的就是省市信息表,它们通过parentid来确定父子关系,这就是一种比较特殊的1:n的关系,我们来看一下,在GreenDao中是如何实现的。
一,我们先要MyDaoGenerator.Java中添加这个新的bean
[java] view
plain copy
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,我们继承的这个类
[java] view
plain copy
@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中的两段代在码:
[java] view
plain copy
/** 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;
}
[java] view
plain copy
/** 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的结构,你怎么应用。就和上一篇文章中的用法一样。这里就不多说了
一,我们先要MyDaoGenerator.Java中添加这个新的bean
[java] view
plain copy
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,我们继承的这个类
[java] view
plain copy
@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中的两段代在码:
[java] view
plain copy
/** 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;
}
[java] view
plain copy
/** 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的结构,你怎么应用。就和上一篇文章中的用法一样。这里就不多说了
相关文章推荐
- GreenDao的简单使用说明(四)特殊的单表1:n
- GreenDao的简单使用说明(四)特殊的单表1:n
- GreenDao的简单使用说明(六)补遗
- GreenDao使用说明(三)多表的使用
- GreenDao的简单使用说明(六)补遗
- GreenDao的简单使用说明(三)多表的操作1:n
- GreenDao的简单使用说明(五)多表n:m
- GreenDao的简单使用说明(五)多表n:m
- GreenDao的简单使用说明(一)android studio中引入GreenDao
- Android:greenDao与Parcelable使用总结
- GreenDao 的使用
- android greenDao使用
- greenDao的使用---重点来啦 实现ORM连表 增删改查 颤抖吧凡人
- greenDao的使用--- 配置 androidstudio
- greenDao的使用教程
- Android GreenDao数据库框架使用
- Python 中特殊变量/方法命名规则说明(特别是私有变量)及使用实例
- Android greenDao 数据库的使用(三)
- Android Studio平台使用GreenDao操作数据库
- GreenDao的使用心得和分享