Java – the greendao getter of related entities returns an empty list

I have an Android application. I use greendao to model my database. I have a simple scenario, but I don't understand how I make it work. I've followed the documentation, but I have to miss something

I have three entities: user, picture and address. User has picture and address. My getter of picture and address always returns null

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

This is my greendao settings

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

This is my test class for testing relationships

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

resolvent:

Emanuel

I encountered some similar problems when trying to save objects in a 1 - to - 1 relationship

After spending enough time using greendao, I found that all "relationship" objects should have the appropriate mapping ID of their "parents" before saving to DB

So I might suggest that if you look at the setuser method of the image and address entity you generated, you will see something similar:

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

The key is userid = user = = null? null:user.getId();

There is a race condition because the created user object will not get ID before it is actually saved to DB. If it does not have ID, it is possible that setuser of its relationship entity will not work normally

In your case, you can try to change the save order to:

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

I hope my answer will help you

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>