1
我用下面的代碼來定義兩個通過一對多關係相關的實體。GreenDao一對多外鍵返回不起作用
private static void addLuckyDrawClick(Schema schema) {
Entity luckyDrawClick = schema.addEntity("LuckyDrawClick");
luckyDrawClick.addIdProperty();
luckyDrawClick.addDateProperty("clickDate").notNull();
luckyDrawClick.addStringProperty("user").notNull();
Property userId = luckyDrawClick.addLongProperty("userId").getProperty();
user.addToMany(luckyDrawClick, userId);
Main.luckyDrawClick = luckyDrawClick;
}
private static void addWeeklyClickList(Schema schema) {
Entity weeklyClickList = schema.addEntity("WeeklyClickList");
weeklyClickList.addIdProperty();
Property clickOne = weeklyClickList.addLongProperty("clickOneId").getProperty();
weeklyClickList.addToOne(luckyDrawClick, clickOne, "clickOne");
Property clickTwo = weeklyClickList.addLongProperty("clickTwoId").getProperty();
weeklyClickList.addToOne(luckyDrawClick, clickTwo, "clickTwo");
Property clickThree = weeklyClickList.addLongProperty("clickThreeId").getProperty();
weeklyClickList.addToOne(luckyDrawClick, clickThree, "clickThree");
Property clickFour = weeklyClickList.addLongProperty("clickFourId").getProperty();
weeklyClickList.addToOne(luckyDrawClick, clickFour, "clickFour");
Property clickFive = weeklyClickList.addLongProperty("clickFiveId").getProperty();
weeklyClickList.addToOne(luckyDrawClick, clickFive, "clickFive");
weeklyClickList.addBooleanProperty("latest");
Main.weeklyClickList = weeklyClickList;
}
我用下面的代碼來堅持實體。
weeklyClickListDao = daoSession.getWeeklyClickListDao();
User user = new User();
user.setUsername("usr");
user.setGender(false);
user.setName("Usr");
user.setLocation("loc");
user.setPassword("p");
user.setDateOfBirth(new Date());
user.setDeviceId("id");
user.setContactNumber("contact");
userDao.insert(user);
Log.d("size", "" + userDao.loadAll().size());
currentUser = userDao.loadAll().get(0);
WeeklyClickList wcl = new WeeklyClickList();
wcl.setLatest(true);
LuckyDrawClick ldc1 = new LuckyDrawClick();
GregorianCalendar date1 = new GregorianCalendar();
date1.set(Calendar.DAY_OF_MONTH, date1.get(Calendar.DAY_OF_MONTH) - 4);
date1.set(Calendar.HOUR_OF_DAY, 0);
date1.set(Calendar.MINUTE, 0);
date1.set(Calendar.SECOND, 0);
date1.set(Calendar.MILLISECOND, 0);
ldc1.setClickDate(date1.getTime());
ldc1.setUser(currentUser.getUsername());
wcl.setClickOne(ldc1);
wcl.setClickOneId(ldc1.getId());
luckyDrawClickDao.insert(ldc1);
LuckyDrawClick ldc2 = new LuckyDrawClick();
GregorianCalendar date2 = new GregorianCalendar();
date2.set(Calendar.DAY_OF_MONTH, date1.get(Calendar.DAY_OF_MONTH) - 3);
date2.set(Calendar.HOUR_OF_DAY, 0);
date2.set(Calendar.MINUTE, 0);
date2.set(Calendar.SECOND, 0);
date2.set(Calendar.MILLISECOND, 0);
ldc2.setClickDate(date2.getTime());
ldc2.setUser(currentUser.getUsername());
wcl.setClickTwo(ldc2);
wcl.setClickTwoId(ldc2.getId());
luckyDrawClickDao.insert(ldc2);
LuckyDrawClick ldc3 = new LuckyDrawClick();
GregorianCalendar date3 = new GregorianCalendar();
date3.set(Calendar.DAY_OF_MONTH, date1.get(Calendar.DAY_OF_MONTH) - 2);
date3.set(Calendar.HOUR_OF_DAY, 0);
date3.set(Calendar.MINUTE, 0);
date3.set(Calendar.SECOND, 0);
date3.set(Calendar.MILLISECOND, 0);
ldc3.setClickDate(date3.getTime());
ldc3.setUser(currentUser.getUsername());
wcl.setClickThree(ldc3);
wcl.setClickThreeId(ldc3.getId());
luckyDrawClickDao.insert(ldc3);
LuckyDrawClick ldc4 = new LuckyDrawClick();
GregorianCalendar date4 = new GregorianCalendar();
date4.set(Calendar.DAY_OF_MONTH, date1.get(Calendar.DAY_OF_MONTH) - 1);
date4.set(Calendar.HOUR_OF_DAY, 0);
date4.set(Calendar.MINUTE, 0);
date4.set(Calendar.SECOND, 0);
date4.set(Calendar.MILLISECOND, 0);
ldc4.setClickDate(date4.getTime());
ldc4.setUser(currentUser.getUsername());
wcl.setClickFour(ldc4);
wcl.setClickFourId(ldc4.getId());
luckyDrawClickDao.insert(ldc4);
weeklyClickListDao.insert(wcl);
但是外鍵引用不包含在記錄中。
我做錯了什麼?
昨晚我到達的答案卻忘記上傳的計算器。無論如何,謝謝你的回答。 –