GreenDao IndsertOrReplaceInTx只插入列表中的最后一项

3

我有一些POJO并为它们创建了一些表。所有的工作都很顺利,这意味着我可以插入它们并加载它们...除了这一个:

我的品牌列表包含6个元素,我非常确定它们是不同的(放置断点并查看它们),但当我要通过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,将其解析为列表,并将其插入到此处的数据库中,但问题仍然存在。有人能告诉我我的错误吗?非常感谢:-)。
1个回答

3

可能你的六个品牌项目共享相同的tableId。因此,greendao认为这是一个项目(由主键标识),并用第二个替换第一个,第三个替换第二个,以此类推...

如果你使用了notNull().primaryKey().autoincrement()

我曾经遇到过同样的问题,并通过更改dao-generator项目中使用的dao-template来“修复”它。

也许如果你在主键属性上没有使用notNull(),它也会起作用。

更新

我再次查看了greendao:

在greendao-generator中,文件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'

因此,这是greendao内部的一个错误!要解决这个问题,您可以在主键列上不指定 notNull 或修改文件 dao.ftl(第126行及以下):

<#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>

这将导致GreenDao在更新或插入语句中不会绑定您的自增主键值,除非它是!= 0null

但要小心第二种方法:它未经测试,因此可能对GreenDao的其他部分产生副作用!


感谢您的帮助(+1)。是的,它们所有的tableId都是0,但是brand.addLongProperty("tableId").notNull().primaryKey().autoincrement();的目的是什么?我的意思是,我认为这是正确的,因为有了autoincrement()。我创建了一个非常简单的测试项目,从文件中读取json,将其解析为列表并将其插入到db中,在此处,问题存在。您能告诉我我的错误在哪里吗?非常感谢:-)。 - mmlooloo
@mmlooloo 我记得在一个项目中曾经遇到过这个问题。不幸的是,我现在无法访问那个项目了... - AlexS
谢谢,我只是想知道这是我的问题还是greendao的问题?我不是数据库方面的专家,但我认为它与“事务概念”有关。无论如何,感谢您的帮助。 - mmlooloo
@mmlooloo 这与变量绑定到INSERT查询以及自增id从数据库中读取的方式有关,绝对不是关于事务的。也许我可以在几小时内给你更多信息。 - AlexS
@mmlooloo,请看我的更新,并且请告诉我哪个建议解决了你的问题。 - AlexS
非常感谢,但愿您一个月前就看到我的问题 :-) 我已经采取了您的第一种方法,现在它可以工作了。非常感谢! - mmlooloo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接