Reputation: 957
I'm not sure if I have a problem in my project or I don't understand how green dao works. Probably the second option will be the right one.
I have the following entity:
Entity review= schema.addEntity("Review");
review.setTableName("reviews");
review.addIdProperty().primaryKey();
Property user_id = review.addLongProperty("user_id").getProperty();
Property object_id= review.addLongProperty("object_id").getProperty();
review.addStringProperty("text");
Index indexUnique = new Index();
indexUnique.addProperty(user_id);
indexUnique.addProperty(object_id);
indexUnique.makeUnique();
review.addIndex(indexUnique);
I use a unique Index over the fields user_id and object_id because I want these two fields to work as a primary key because I only want to have one review per user and per object in the table.
The first time that I insert a review in the database, I do the following:
MyEntity e=new MyEntity();
e.setText("Hello");
e.setUserId(1);
e.setObjectId(9);
myDao.insert(e);
All works right up to there. The next step is that the user updates the entity, and I use the following code:
MyEntity e=new MyEntity();
e.setText("New text");
e.setUserId(1);
e.setObjectId(9);
myDao.insertOrReplaceInTx(e);
And the insertOrReplaceInTx method works without exceptions. The problem appears when I try to load all the reviews with the myDao.loadAll() method. Following exception arises:
Couldn't read row 0, col 0 from CursorWindow
I had a look to the database file, and the problem was that the entity was saved to the table, but all fields are null, I don't know why.
After doing a lot of tests I managed to get it work, doing the following. The first time that the review is created, I do the same:
MyEntity e=new MyEntity();
e.setText("New text");
e.setUserId(1);
e.setObjectId(9);
myDao.insert(e);
Later, If the entity already exists in the database (for an edit action), then I load the entity, and I do;
MyEntity e= do a query with greendao and load the entity with user_id=1 and object_id=9//Yes I know that is pseudocode
e.setText("New text");
myDao.update(e);
And it works, but the strange thing here is that I don't need to call myDao.update(e); I have realized that after the e.setText("New text");, the field in the database has the new value.
Then, the question is, are the changes to a field populated to the table without calling the update method of the dao? How can this be possible?
Upvotes: 0
Views: 535
Reputation: 489
This will help you to solve this problem
https://github.com/infinum/android_dbinspector
Upvotes: 5