繁体   English   中英

无法在ActiveAndroid中保存空列值

[英]Cannot save null column value in ActiveAndroid

为简单起见,我有以下模型:

@Table(name = "Items")
class TItem extends Model {
    @Column(name = "title")
    private String      mTitle;

    public String getTitle() { return mTitle; }

    public void setTitle(String title) { mTitle = title; }
}

我的测试失败了:

    //Create new object and save it to DDBB
    TItem r = new TItem();
    r.save();

    TItem saved = new Select().from(TItem.class).where("id=?", r.getId()).executeSingle();
    //Value for saved.getTitle() = null  --> OK

    r.setTitle("Hello");
    r.save();
    saved = new Select().from(TItem.class).where("id=?", r.getId()).executeSingle();
    //Value for saved.getTitle() = "Hello"  --> OK

    r.setTitle(null);
    r.save();
    saved = new Select().from(TItem.class).where("id=?", r.getId()).executeSingle();
    //Value for saved.getTitle() = "Hello"  --> FAIL

看来我无法在ActiveAndroid中将列值从任何值更改为null。 很奇怪。 是虫子吗? 我没有找到任何关于它的东西,但是看起来很基本。

如果我调试应用程序并遵循保存方法,则它到达的最后一个命令在SQLLiteConnection.java中:

private void bindArguments(PreparedStatement statement, Object[] bindArgs) {
    ....
    // It seems ok, as it is really inserting a null value in the DDBB
    case Cursor.FIELD_TYPE_NULL:
        nativeBindNull(mConnectionPtr, statementPtr, i + 1);
    ....
}

我看不到进一步,因为“ nativeBindNull”不可用

最后,我发现了发生的情况,问题出在ActiveAndroid库中。

空值会正确保存到DDBB,但不会正确检索。 由于ActiveAndroid使用缓存的项目,因此在获取元素时,它会获取“旧版本”并使用新值对其进行更新。 这是库失败的地方,因为正在检查如果不为null则替换该值,否则为空。

为了解决这个问题,我们必须从库中的Model.java类中更改它:

public final void loadFromCursor(Cursor cursor) {

    List<String> columnsOrdered = new ArrayList<String>(Arrays.asList(cursor.getColumnNames()));
    for (Field field : mTableInfo.getFields()) {
        final String fieldName = mTableInfo.getColumnName(field);
        Class<?> fieldType = field.getType();
        final int columnIndex = columnsOrdered.indexOf(fieldName);
        ....

        if (columnIsNull) {
            <strike>field = null;</strike> //Don't put the field to null, otherwise we won't be able to change its content
            value = null;
        }

        ....

        <strike>if (value != null)</strike> {   //Remove this check, to always set the value
            field.set(this, value);
        }
        ....
    }
    ....
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM