ORMLite不知道如何存储类

时间:2015-10-02 09:13:31

标签: android parent-child ormlite database-table

项目

@DatabaseTable
public  class Item {
    @DatabaseField(generatedId = true, canBeNull = false, columnName = "id")
    public int id;
    @DatabaseField(canBeNull = true, foreign = true, foreignAutoCreate = true, foreignAutoRefresh = true)
        public Enclosure enclosure;
}

附件

@DatabaseTable
public   class Enclosure {
    @DatabaseField(generatedId= true, columnName = "id", canBeNull = false)
    private long id;
    @DatabaseField
    private String url;
    @ForeignCollectionField(eager = true)
    protected ForeignCollection<Item> items;
}

R.raw.ormlite_config.txt

#################################
# --table-start--
dataClass=com.rssproject.Item
tableName=rssitems
# --table-fields-start--
# --field-start--
fieldName=id
generatedId=true
# --field-end--
# --field-start--
fieldName=enclosure
# --field-end--
# --table-fields-end--
# --table-end--
#################################
#################################
# --table-start--
dataClass=com.rssproject.Enclosure
tableName=rssenclosure
# --table-fields-start--
# --field-start--
fieldName=id
generatedId=true
# --field-end--
# --field-start--
fieldName=url
indexName=rssenclosure_string_idx
# --field-end--
# --table-fields-end--
# --table-end--
#################################

DatabaseHelper

public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION, R.raw.ormlite_config);
    }

插入:

 List<Item> list = daoRuntimeItem.queryForAll();
                daoRuntimeEnclosure.create(item.getEnclosure());
                daoRuntimeItem.create(item);

错误是:

java.lang.IllegalArgumentException: ORMLite does not know how to store class com.rssproject.Enclosure for field enclosure. Use another class or a custom persister.

1 个答案:

答案 0 :(得分:2)

正如您在评论中所说,您手动编写了ormlite_config.txt,这是一个坏主意。 ORMLite提供了一个实用程序来为您编写该文件,这要容易得多:)

请查看此页面以获取更多信息:http://ormlite.com/docs/table-config