Reputation: 923
I have an android app in which I use greenDAO
to model my database. I have an easy scenario but I don't understand how I can make it work. I've followed the documentation but I must be missing something.
I have 3 entities: User, Picture and Address. A User has Pictures and Addresses. My getters for Picture and Address always return null.
userEntity.getPicture(); -> returns null
userEntity.getAddress(); -> returns null
Here is my GreenDAO setup
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");
Here is my testclass to test the relations
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;
}
Upvotes: 4
Views: 2640
Reputation: 315
Emanuel,
I am facing some similar issues when trying to save objects with 1-to-1 relations.
After spending pretty enough time with greenDAO
I have found, that all "relational" objects should have appropriate mapping IDs of their "parents" before being saved to DB.
So I may suggest, that if you take a look at setUser
methods of your generated Picture
and Address
entities, you will see something like:
public void setUser(User user) {
synchronized (this) {
this.user = user;
userId = user == null ? null : user.getId();
user__resolvedKey = userId;
}
}
Crucial is userId = user == null ? null : user.getId();
There are race conditions, as your created User object will not get ID until it is actually saved to DB. And if it does not have ID, there is a chance, that setUser
of its relational entities will not work normally.
In your case you may try to change save sequence 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);
Hope my answer will be helpful to you.
Upvotes: 3