I have some POJOS and create some tables for them. All of them work well which means I can insert them and load them ... except this one:
My Brand list contains 6 elements and I am really sure they are different(put break point and saw them) but when I am going to insert them in DB by greenDao
only last element is inserted. My table is empty and this statement suppose to fill it.
Codes:
public class SingletonDatabase {
private static SingletonDatabase mInstance;
private DaoMaster.OpenHelper mHelper;
private DaoSession mDaoSessionForUI;
private DaoMaster mDaoMaster;
private static Context mCtx;
private SingletonDatabase(Context context) {
mCtx = context;
setupDb();
}
public static synchronized SingletonDatabase getInstance(Context context) {
if (mInstance == null) {
mInstance = new SingletonDatabase(context);
}
return mInstance;
}
private void setupDb() {
mHelper = new DaoMaster.OpenHelper(
mCtx.getApplicationContext(), "mydb", null) {
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion) {
// Handle upgrade
}
};
SQLiteDatabase db = mHelper.getWritableDatabase();
mDaoMaster = new DaoMaster(db);
mDaoSessionForUI = mDaoMaster.newSession();
}
public DaoSession getDaoSessionForUI() {
return mDaoSessionForUI;
}
public DaoSession getDaoSeesion(){
return mDaoMaster.newSession();
}
}
The generated Brand code:
* Entity mapped to table BRAND.
*/
public class Brand {
private long tableId;
private String id;
private String name;
private String publicImage;
private String description;
private String lastDownloadedTime;
public Brand() {
}
public Brand(long tableId) {
this.tableId = tableId;
}
public Brand(long tableId, String id, String name, String publicImage, String description, String lastDownloadedTime) {
this.tableId = tableId;
this.id = id;
this.name = name;
this.publicImage = publicImage;
this.description = description;
this.lastDownloadedTime = lastDownloadedTime;
}
public long getTableId() {
return tableId;
}
public void setTableId(long tableId) {
this.tableId = tableId;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPublicImage() {
return publicImage;
}
public void setPublicImage(String publicImage) {
this.publicImage = publicImage;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getLastDownloadedTime() {
return lastDownloadedTime;
}
public void setLastDownloadedTime(String lastDownloadedTime) {
this.lastDownloadedTime = lastDownloadedTime;
}
}
The code for creating schema:
public class DaoGen {
public static void main(String[] args) throws Exception {
Schema schema = new Schema(1, "com.mmlooloo");
Entity brandList = addBrand(schema);
File f = new File("src-gen");
f.mkdir();
new DaoGenerator().generateAll(schema,f.getAbsolutePath());
}
private static Entity addBrand(Schema schema) {
Entity brand = schema.addEntity("Brand");
brand.addLongProperty("tableId").notNull().primaryKey().autoincrement();
brand.addStringProperty("id");
brand.addStringProperty("name");
brand.addStringProperty("publicImage");
brand.addStringProperty("description");
brand.addStringProperty("lastDownloadedTime");
return brand;
}
}
and finally how I insert them:
public class BrandDownloadService extends IntentService {
public BrandDownloadService() {
super("BrandDownloadService");
}
@Override
protected void onHandleIntent(Intent intent) {
....
BrandDao brandDao = SingletonDatabase.getInstance(this).getDaoSeesion().getBrandDao();
brandDao.insertOrReplaceInTx(brandList,true);
}
I set break point and check my brandlist
and it has 6 elements.
Any help, work around, debug tips ... I really do not know what the problem is.
Many thanks!!
EDIT:
I have created very very simple (really simple believe me :-)) test project that reads json from file, parse it to list and insert it to db at this and the problem exists. Can anyone tell me what is my mistake? many many thanks :-).