@Entity
public class TestIndex {
@Id
private Long id;
private String name;
@ToMany(referencedJoinProperty = "indexId")
private List<TestNum> testNumList;
}
@Entity
public class TestNum {
@Id
private Long id;
private String name;
private int age;
private long indexId;
@ToOne(joinProperty = "indexId")
private TestIndex testIndex;
}
使用方法如下
DaoSession daoSession = ((App) getApplication()).getDaoSession();
TestIndex testIndex = new TestIndex(1010L,"测试数据");
TestIndexDao testIndexDao = daoSession.getTestIndexDao();
TestNumDao testNumDao = daoSession.getTestNumDao();
long insert = testIndexDao.insertOrReplace(testIndex);
for (int i = 0; i < 6; i++) {
TestNum testNum = new TestNum((long) i,"test" + i,28+i,insert);
testNum.setTestIndex(testIndex);
testNumDao.insertOrReplace(testNum);
}
List<TestIndex> list = daoSession.queryBuilder(TestIndex.class).build().list();
if (list != null && list.size() > 0) {
TestIndex index = list.get(0);
List<TestNum> testNumList = index.getTestNumList();//需要加上再获取的到
System.out.println(index);
}
?前两个类为生成数据库表的entity及其对应的关系,创建后builder一下greendao就会自动生成相应的代码
|