GreenDao的简单使用说明(四)特殊的单表1:n
2016-06-30 10:43
369 查看
转载自:http://blog.csdn.net/chenguang79/article/details/50467383
我们在做系统的时候,有时间会遇到单表自循环的情况,最常见的就是省市信息表,它们通过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="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
/** 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的结构,你怎么应用。就和上一篇文章中的用法一样。这里就不多说了
我们在做系统的时候,有时间会遇到单表自循环的情况,最常见的就是省市信息表,它们通过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="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
/** 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的结构,你怎么应用。就和上一篇文章中的用法一样。这里就不多说了
相关文章推荐
- Quartz
- Qt学习九:控件六:任务栏托盘菜单
- 编程常见的技术和管理诟病
- struts自定义验证规则
- 你手机注册过哪些网站,测一测
- DataStage最佳实践之存储过程调用
- 代码查找
- 适配器模式
- 工厂模式与老化模式驱动中分别添加内容
- spring使用模板发送邮件
- Retrofit的(get以及post)的简单使用
- GreenDao的简单使用说明(三)多表的操作1:n
- js 对时间格式化
- Oracle递归查询:使用prior实现树操作
- 自制的Docker镜像
- Android事件总线分发库的使用EventBus(和Otto一样,只是效率高点)
- SQL Case When 嵌套
- Android上传文件到FTP
- 【leetcode】24. Swap Nodes in Pairs
- 一道关于链表的基本操作题