我有一些 POJOS 并为它们创建了一些表。它们都运行良好,这意味着我可以插入它们并加载它们......除了这个:
我的品牌列表包含 6 个元素,我很确定它们是不同的(放置断点并看到它们)但是当我将它们插入 DB 时,greenDao
只插入最后一个元素。我的表是空的,这个语句应该填满它。
代码:
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();
}
}
生成的品牌代码:
* 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;
}
}
创建模式的代码:
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;
}
}
最后我如何插入它们:
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);
}
我设置断点并检查我的brandlist
,它有 6 个元素。
任何帮助,解决方法,调试提示......我真的不知道问题是什么。
非常感谢!!
编辑:
我创建了非常非常简单(相信我真的很简单:-))测试项目,它从文件中读取 json,将其解析为列表并将其插入到 db 中,并且存在问题。谁能告诉我我的错误是什么?非常感谢 :-)。