gpt4 book ai didi

java - 相关实体的 GreenDao getter 返回空列表

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:24:45 24 4
gpt4 key购买 nike

我有一个 android 应用程序,我在其中使用 greenDAO 为我的数据库建模。我有一个简单的场景,但我不明白如何让它发挥作用。我已经按照文档进行操作,但我一定遗漏了一些东西。

我有 3 个实体:用户、图片和地址。用户有图片和地址。我的 Picture 和 Address getters 总是返回 null。

    userEntity.getPicture(); -> returns null
userEntity.getAddress(); -> returns null

这是我的 GreenDAO 设置

    Entity userEntity = schema.addEntity("User");
userEntity.addIdProperty();
userEntity.addStringProperty("firstName");
userEntity.addStringProperty("lastName");

Entity picture = schema.addEntity("Picture");
picture.addIdProperty();
picture.addByteArrayProperty("image");
picture.addStringProperty("imageName");

Entity address = schema.addEntity("Address");
address.addIdProperty();
address.addStringProperty("street");
address.addIntProperty("houseNumber");
address.addIntProperty("zipcode");
address.addStringProperty("city");

// a user can have multiple pictures but a picture is connected to one user
Property pictureIdProperty = picture.addLongProperty("userId").getProperty();
picture.addToOne(userEntity, pictureIdProperty).setName("user");
userEntity.addToMany(picture, pictureIdProperty).setName("picture");

// a user can have multiple addresses but an address is only connected to one user
Property addressIdProperty = address.addLongProperty("userId").getProperty();
address.addToOne(userEntity, addressIdProperty).setName("user");
userEntity.addToMany(address, addressIdProperty).setName("address");

这是我测试关系的测试类

    DevOpenHelper helper = new DaoMaster.DevOpenHelper(getApplication(), "relation_test_db", null);
SQLiteDatabase db = helper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(db);
this.daoSession = daoMaster.newSession();

UserDao userDao = this.daoSession.getUserDao();
PictureDao pictureDao = this.daoSession.getPictureDao();
AddressDao addressDao = this.daoSession.getAddressDao();

// clear all data
userDao.deleteAll();
pictureDao.deleteAll();
addressDao.deleteAll();

/**
* create data
*/
User bill = new User(null);
bill.setFirstName("Bill");
bill.setLastName("Murray");

Picture billsPicture = new Picture(null);
billsPicture.setImage("BillsExamplePictureByteArray".getBytes());
billsPicture.setImageName("BillsPictureName");

Address billsAddress = new Address(null);
billsAddress.setStreet("BillsStreet");
billsAddress.setHouseNumber(42);
billsAddress.setZipcode(12345);
billsAddress.setCity("Wilmette");

billsPicture.setUser(bill);
billsAddress.setUser(bill);

userDao.insert(bill);
pictureDao.insert(billsPicture);
addressDao.insert(billsAddress);

User user = userDao.queryBuilder().list().get(0);
ArrayList<Picture> billsPictureList = (ArrayList<Picture>) user.getPicture();
ArrayList<Address> billsAddressList = (ArrayList<Address>) user.getAddress();

if (billsPictureList == null || billsPictureList.size() == 0) {
// contact Markus
Toast.makeText(this, "Contact Stackoverflow", Toast.LENGTH_SHORT).show();
return;
}

if (billsAddressList == null || billsAddressList.size() == 0) {
// contact Markus
Toast.makeText(this, "Contact Stackoverflow", Toast.LENGTH_SHORT).show();
return;
}

最佳答案

伊曼纽尔,

尝试保存具有一对一关系的对象时,我遇到了一些类似的问题。

greenDAO 上花费了相当多的时间后,我发现所有“关系”对象在保存到数据库之前都应该具有其“父级”的适当映射 ID。

所以我建议,如果您查看生成的 PictureAddress 实体的 setUser 方法,您会看到一些东西喜欢:

public void setUser(User user) {
synchronized (this) {
this.user = user;
userId = user == null ? null : user.getId();
user__resolvedKey = userId;
}
}

关键是 userId = user == null ? null : 用户.getId();

存在竞争条件,因为您创建的用户对象在实际保存到数据库之前不会获得 ID。如果它没有 ID,则它的关系实体的 setUser 有可能无法正常工作。

在您的情况下,您可以尝试将保存顺序更改为:

//1. Save user to DB, this will give it ID  
userDao.insert(bill);

//2. Set user entity with ID to its relational entities
billsPicture.setUser(bill);
billsAddress.setUser(bill);

//3. Save relational entities
pictureDao.insert(billsPicture);
addressDao.insert(billsAddress);

希望我的回答对您有所帮助。

关于java - 相关实体的 GreenDao getter 返回空列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25835038/

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