關系
在greenDAO,實體涉及使用一對一或一對多的關系。例如,如果要模擬一個1:greenDAOñ關系,你將有一個一對一和一對多的關系。但是,請注意,一對一和一對多的關系不是相互連接,所以你必須同時更新。
//學生
@Entity
public class Student {
@Id
private Long id ;
private String name ;
private String number ;
private long PE_Id ;
@ToOne(joinProperty = "PE_Id")
private PEClazz peClazz;
}
//體育課
@Entity
public class PEClazz {
@Id
private Long id;
private int credit;
private String name;
}
建立一對一關系
@ToOne注釋定義的關系向另一個實體(一個實體對象)
當 修改外鍵 時
Student student = studentDao.queryBuilder().where(StudentDao.Properties.Id.eq(1)).unique();
PEClazz peClazz = student.getPeClazz();
System.out.println("peClazz+id:"+peClazz.getId());
System.out.println("peClazz+name:"+peClazz.getName());
//修改體育課的ID
student.setPE_Id(2);
peClazz = student.getPeClazz();
System.out.println("peClazz+id:"+peClazz.getId());
System.out.println("peClazz+name:"+peClazz.getName());
運行結果(修改外鍵,外鍵對象受到改變)
peClazz+id:1
peClazz+name:籃球
peClazz+id:2
peClazz+name:足球
或者里(修改外鍵對象)
Student student = studentDao.queryBuilder().where(StudentDao.Properties.Id.eq(1)).unique();
PEClazz peClazz = student.getPeClazz();
System.out.println("peClazz+id:"+peClazz.getId());
//設置新的體育課
PEClazz newPeClazz = new PEClazz();
newPeClazz.setId(2L);
student.setPeClazz(newPeClazz);
System.out.println("student.PE_ID:"+student.getPE_Id());
運行結果 (修改外鍵對象,外鍵受到改變)
peClazz+id:1
student.PE_ID:2
注意
在一對一關系中,當要第一次加載時,使用的是懶加載。
后續調用將立即返回先前解析的對象。
如果外鍵屬性(例如:PE_Id)受到了改變(或者外鍵對象受到改變(setPeClazz)),那么再次獲取外鍵對象時(獲取的外鍵)就會發生改變。
外鍵和外鍵對象具有關聯.
建立一對多關系
//父親類
@Entity
public class Father {
@Id
private Long id;
private String name;
@ToMany(referencedJoinProperty = "fatherID")
List<Son> sons ;
}
//兒子類
@Entity
public class Son {
@Id
private Long id;
private String name;
private long fatherID ;
}
@ToMany限定了關系到一組其他實體的(多個實體對象)。
referencedJoinProperty參數:指定指向該實體的ID目標實體的“外鍵”屬性的名稱。
先創建幾個數據:
Son son1 = new Son();
son1.setName("兒子1");
Son son2 = new Son();
son2.setName("兒子2");
Son son3 = new Son();
son3.setName("兒子3");
Father father = new Father();
father.setName("父親");
long fatherID = fatherDao.insertOrReplace(father);
son1.setFatherID(fatherID);
son2.setFatherID(fatherID);
son3.setFatherID(fatherID);
sonDao.insertOrReplace(son1);
sonDao.insertOrReplace(son2);
sonDao.insertOrReplace(son3);
更新操作注意
Father father = fatherDao.queryBuilder().where(FatherDao.Properties.Id.eq(1)).unique();
List<Son> sons = father.getSons();
for (Son son: sons) {
System.out.println("son:"+son.getName());
}
Son son = new Son();
son.setFatherID(father.getId());
son.setName("兒子4");
//插入
daoSession.insert(son);
for (Son newSon:
sons) {
System.out.println("newSon:"+newSon.getName());
}
//重新獲取
List<Son> sons1 = father.getSons();
for (Son newSon1:
sons1) {
System.out.println("newSon1:"+newSon1.getName());
}
運行結果
son:兒子1
son:兒子2
son:兒子3
newSon:兒子1
newSon:兒子2
newSon:兒子3
newSon1:兒子1
newSon1:兒子2
newSon1:兒子3
注意
由上可知,雖然插入了一個新的,但是,getSons()獲取的sons 並沒有增加。
因為緩存在里面有列表對象,因此后續的關系get方法調用不查詢數據庫。
所以可以做以下操作。
//以下是getSons的代碼
public List<Son> getSons() {
//為空,則重新查詢
if (sons == null) {
final DaoSession daoSession = this.daoSession;
if (daoSession == null) {
throw new DaoException("Entity is detached from DAO context");
}
SonDao targetDao = daoSession.getSonDao();
List<Son> sonsNew = targetDao._queryFather_Sons(id);
synchronized (this) {
if(sons == null) {
sons = sonsNew;
}
}
}
//否則返回以前的緩存
return sons;
}
要增加新的關聯實體,需要手工將它們添加到源實體的一對多列表
//插入
daoSession.insert(son);
//再插入以后,應該為列表手工添加son
//手工添加
sons.add(son);
for (Son newSon:
sons) {
System.out.println("newSon:"+newSon.getName());
}
//重新獲取
List<Son> sons1 = father.getSons();
for (Son newSon1:
sons1) {
System.out.println("newSon1:"+newSon1.getName());
}
運行結果
son:兒子1
son:兒子2
son:兒子3
newSon:兒子1
newSon:兒子2
newSon:兒子3
newSon:兒子4
newSon1:兒子1
newSon1:兒子2
newSon1:兒子3
newSon1:兒子4
或者
//插入
daoSession.insert(son);
//再插入以后
/使用重置方法來清除緩存列表
father.resetSons();
for (Son newSon:
sons) {
System.out.println("newSon:"+newSon.getName());
}
//重新獲取
List<Son> sons1 = father.getSons();
for (Son newSon1:
sons1) {
System.out.println("newSon1:"+newSon1.getName());
}
運行結果
son:兒子1
son:兒子2
son:兒子3
newSon:兒子1
newSon:兒子2
newSon:兒子3
newSon1:兒子1
newSon1:兒子2
newSon1:兒子3
newSon1:兒子4
由上可知
sons列表中沒有添加上去,但是重新getSons()后,發現新插入的son已經加入了進去.
father.resetSons();由源碼可知:
public synchronized void resetSons() {
sons = null;
}
sons為null了,所以當要getSons()時,因為son==null,所以會重新從數據庫中重新查詢數據。
當添加,更新或刪除許多相關的實體可以使用重置方法來清除緩存列表。那么接下來get會重新查詢相關實體: