我上一篇讲到了GreenDao3.0的基本使用,真是真真的快,而且很好用。我在最近的一个项目中就尝试的使用了,并且我的这个项目是半离线的项目,所以用到数据库的地方还是比较多的。用来还是比较顺手的。所以跟大家分享一下。
因为我的项目需求,所以我在使用的时候涉及到要使用ToMany来关联数据。所以这里就讲讲我对ToMany的用法。我下面列举一个栗子:假设你的数据库目前有两张表:班级表、学生表。
班级表:里面全部是班级信息,目前的字段只有简单的班级ID班级名称。
学生表:这个表里面装的是全校的学生,该表里面有一个字段是班级id,学生名称。
问题:我要查询某个班的所有学生,打印出来。
思路:
(1)没用ToMany之前的原生SQL,我会这样来做。去查询班级表拿到该班级表的要查询的班级的班级id,然后拿着这个id去遍历学生表。只要是该学生对应的班级id跟我们拿的这个id一致就拿出来。就这样cursor走完之后我就拿到所有的数据啦。(我之前就是这样干的。。。勿喷。。。)
(2)有了ToMany之后,上面的1的操作我们可以不用去关心具体,直接拿到我们的班级对象,用对象getStudents就可以拿到我们要的所有学生信息了。
我贴一下我写的表的实体把:
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.JoinProperty;
import org.greenrobot.greendao.annotation.ToMany;
import java.util.List;
import org.greenrobot.greendao.annotation.Generated;
import org.greenrobot.greendao.DaoException;
/**
* @author ChenYe
* created by on 2018/3/22 0022. 09:23
**/
@Entity
public class ClassEntity {
@Id(autoincrement = true)
private Long id;
private String classId;
private String className;
@ToMany(joinProperties = {@JoinProperty(name = "classId", referencedName = "classId")})
private List<StudentEntity> students;
/** Used to resolve relations */
@Generated(hash = 2040040024)
private transient DaoSession daoSession;
/** Used for active entity operations. */
@Generated(hash = 1384035737)
private transient ClassEntityDao myDao;
@Generated(hash = 1824774184)
public ClassEntity(Long id, String classId, String className) {
this.id = id;
this.classId = classId;
this.className = className;
}
@Generated(hash = 1465610799)
public ClassEntity() {
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getClassId() {
return this.classId;
}
public void setClassId(String classId) {
this.classId = classId;
}
public String getClassName() {
return this.className;
}
public void setClassName(String className) {
this.className = className;
}
/**
* 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 = 279833155)
public List<StudentEntity> getStudents() {
if (students == null) {
final DaoSession daoSession = this.daoSession;
if (daoSession == null) {
throw new DaoException("Entity is detached from DAO context");
}
StudentEntityDao targetDao = daoSession.getStudentEntityDao();
List<StudentEntity> studentsNew = targetDao._queryClassEntity_Students(classId);
synchronized (this) {
if (students == null) {
students = studentsNew;
}
}
}
return students;
}
/** Resets a to-many relationship, making the next get call to query for a fresh result. */
@Generated(hash = 238993120)
public synchronized void resetStudents() {
students = null;
}
/**
* 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);
}
/**
* 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);
}
/** called by internal mechanisms, do not call yourself. */
@Generated(hash = 1512363421)
public void __setDaoSession(DaoSession daoSession) {
this.daoSession = daoSession;
myDao = daoSession != null ? daoSession.getClassEntityDao() : null;
}
}
我先把学生的实体也贴出来一起说:
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Generated;
/**
* @author ChenYe
* created by on 2018/3/22 0022. 09:46
**/
@Entity(nameInDb = "student_table")
public class StudentEntity {
@Id(autoincrement = true)
private Long id;
private String name;
private String classId;
@Generated(hash = 712916299)
public StudentEntity(Long id, String name, String classId) {
this.id = id;
this.name = name;
this.classId = classId;
}
@Generated(hash = 634333355)
public StudentEntity() {
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getClassId() {
return this.classId;
}
public void setClassId(String classId) {
this.classId = classId;
}
@Override
public String toString() {
return "StudentEntity{" +
"id=" + id +
", name='" + name + '\'' +
", classId='" + classId + '\'' +
'}';
}
}
我贴出来的代码里面包含了GreenDao自动生成的代码。。。其实我们要看的重点是ClassEntity里面有注解ToMany的地方:
@ToMany(joinProperties = {@JoinProperty(name = "classId", referencedName = "classId")})
这个。。我习惯的命名一样,所以导致你看起来估计费劲,,,上面的第一个classId 是当前类(ClassEntity)的classId,第二个classId 是StudentEntity 的 calssId,这两个命名你可以随便写的,,,意思就是当前表的classId字段作为外键与另一张表的classId关联起来。
我这个ToMany用法,你可以把外键的类型设置为String类型,如果是另一种用法你的外键只能是Long类型的。用法是:
private Long projectId;
@ToMany(referencedJoinProperty = "projectId")
List<AccessoryDbEntity> accessoryList;
上面定义的那个projectId是下面这两行的AccessoryDbEntity里面的字段,其实这种写法我感觉是默认把下面两行所处的表的主键id当前当前表的外键,如果你的另一张表的那个projectId字段想要值一样的话,只能定义为Long类型啦,不然在生成代码的时候会报错的。。。等你报错了就懂我的意思了。