我有一些POJOS,并为它们创建了一些表。所有这些都运行良好,这意味着我可以插入并加载它们。。。除了这个:
我的品牌列表包含6个元素,我真的很确定它们是不同的(放置断点并看到它们),但当我要通过greenDao
将它们插入DB时,只插入最后一个元素。我的桌子是空的,这句话应该能填满它
代码:
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,将其解析为列表,然后将其插入到数据库中,问题就存在了。有人能告诉我我犯了什么错吗?非常感谢:-)
可能您的6个品牌物品共享相同的表ID。因此,greendao认为这是一个项目(由主键标识),并将第一个项目替换为第二个项目,将第二个替换为第三个项目,依此类推…
如果使用notNull().primaryKey().autoincrement()
我曾经遇到过同样的问题,并通过更改dao生成器项目中用于代码生成的dao模板来"修复"它。
如果您没有在primarykey属性上使用notNull()
,也许它也会起作用。
更新
我又看了一眼绿道:
在greendao生成器文件src-template/dao.ftl
中,您可以找到以下行:
protected void bindValues(SQLiteStatement stmt, ${entity.className} entity) {
stmt.clearBindings();
<#list entity.properties as property>
<#if property.notNull || entity.protobuf>
<#if entity.protobuf>
if(entity.has${property.propertyName?cap_first}()) {
</#if> stmt.bind${toBindType[property.propertyType]}(${property_index + 1}, entity.get${property.propertyName?cap_first}()<#if
property.propertyType == "Boolean"> ? 1l: 0l</#if><#if property.propertyType == "Date">.getTime()</#if>);
这意味着,如果在autoincrement
-属性上使用notNull()
,则相应的变量将始终绑定到插入或更新语句。这导致总是手动设置主键的值,而忽略autoincrement
,因为
CREATE TABLE mytable ( id integer primary key autoincrement, details varchar(30));
INSERT INTO mytable (id, details) VALUES (0, 'something');
结果在此数据库行中:0 | 'something'
。
所以它是绿道里面的虫子!要解决此问题,您可以不在主键列上指定notNull
,也可以修改文件dao.ftl
(第126ff行):
<#list entity.properties as property>
<#if property.notNull || entity.protobuf>
<#if entity.protobuf>
if(entity.has${property.propertyName?cap_first}()) {
</#if>
<#if property.pkAutoincrement>
if(entity.get${property.propertyName?cap_first}() != 0) {
</#if> stmt.bind${toBindType[property.propertyType]}(${property_index + 1}, entity.get${property.propertyName?cap_first}()<#if
property.propertyType == "Boolean"> ? 1l: 0l</#if><#if property.propertyType == "Date">.getTime()</#if>);
<#if entity.protobuf || property.pkAutoincrement>
}
</#if>
<#else> <#-- nullable, non-protobuff -->
${property.javaType} ${property.propertyName} = entity.get${property.propertyName?cap_first}();
if (${property.propertyName} != null) {
<#if property.pkAutoincrement> if (${property.propertyName} != 0) {
</#if> stmt.bind${toBindType[property.propertyType]}(${property_index + 1}, ${property.propertyName}<#if property.propertyType == "Boolean"> ? 1l: 0l</#if><#if property.propertyType == "Date">.getTime() </#if>);
<#if property.pkAutoincrement> }</#if>
}
</#if>
这将导致greendaoNOT将自动递增主键值绑定到更新或插入语句,除非它是!= 0
或null
。
但要小心第二种方法:它未经测试,因此可能会对绿岛的其他部分产生副作用