gpt4 book ai didi

java - GreenDao 一对多外键引用不起作用

转载 作者:行者123 更新时间:2023-11-30 04:00:44 35 4
gpt4 key购买 nike

我使用以下代码来定义两个通过一对多关系相关的实体。

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);

但外键引用不包含在记录中。

我做错了什么吗?

最佳答案

在将 ldc 插入数据库之前,您正在 wcl 中设置 ClickFourId(作为示例),当您执行以下操作时:

wcl.setClickFourId(ldc4.getId());

ldc4.getId() 将返回 null,当您插入 wcl 时,它没有指向 ldc4 的外键。

试试这个方法:

luckyDrawClickDao.insert(ldc4);
wcl.setClickFourId(ldc4.getId());

关于java - GreenDao 一对多外键引用不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22044047/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com