GreenDao IndsertOrReplaceInTx inserts only the last element of the list

I have POJOS and create multiple tables for them. They all work well, which means I can paste them in and load them ... other than this:

The My Brand list contains 6 items and I'm really sure they are different (set a breakpoint and saw them), but when I go to insert them into the DB on greenDao

, only the last item is inserted. My table is empty and this statement should 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();
    }

}

      

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;
    }

}

      

Code to create the 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 a breakpoint and checked mine brandlist

and it has 6 elements.

Any help, work, debugging tips ... I really don't know what the problem is.

Many thanks!

EDIT:

I created a very simple (really simple believe me :-)) test project that reads json from a file, parses it and translates into db for that and the problem exists. Can anyone tell me what is my mistake? many thanks :-).

+3


source to share


1 answer


Probably your 6 Brand-pieces have the same tableId. Thus greendao considers it to be one item (denoted by the primary key) and replaces the first with the second, the second with the third, etc.

If you have used notNull().primaryKey().autoincrement()

I once had the same problem and "fixed" it by changing the dao template that is used for cogeneration in the dao-generator project.

It might also work if you don't use notNull()

primarykey on the property.

UPDATE

I looked at greendao again:

In the greendao-generator file, src-template/dao.ftl

you can find the following lines:

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>);

      



This means that if you use notNull()

in your autoincrement

-property, the corresponding variable will always be required to insert or update statements. This results in always manually setting the value for your primary key and ignoring autoincrement

it since

CREATE TABLE mytable ( id integer primary key autoincrement, details varchar(30));
INSERT INTO mytable (id, details) VALUES (0, 'something');

      

This leads to a db-line: 0 | 'something'

.

Hence this is a bug inside greendao! To fix this problem, you can either omit notNull

the primary key in the column, or you can modify the file dao.ftl

(line 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>

      

This will cause greendao to NOT bind your autoincrement-primarykey value to your update, or insert instructions if it is not != 0

or null

.

Be careful with the second approach: it is untested and therefore may have side effects on other greendao sites!

+3


source







All Articles