我使用以下代码来定义两个通过一对多关系相关的实体。
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);
但是外键引用不包含在记录中。
我做错了吗?
答案 0 :(得分:2)
在将ldc插入数据库之前,您正在wcl中设置ClickFourId(作为示例),执行以下操作:
wcl.setClickFourId(ldc4.getId());
ldc4.getId()将返回null,当你插入wcl时,它没有ldc4的外键。
尝试这种方式:
luckyDrawClickDao.insert(ldc4);
wcl.setClickFourId(ldc4.getId());