2

Item

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

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

insert:

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

Error is :

java.lang.IllegalArgumentException: ORMLite does not know how to store class com.rssproject.Enclosure for field enclosure. Use another class or a custom persister.
NickUnuchek
  • 11,794
  • 12
  • 98
  • 138
  • 1
    your ormlite_config.txt is not up to date : the fieldName=enclosure is not marked as foreign. Does a clean build fix your problem ? – mithrop Oct 02 '15 at 09:41
  • @mithrop I wrote this file by myself – NickUnuchek Oct 02 '15 at 09:45
  • 1
    Oh ! what a bad idea to do this :) Please take a look here (I am sure you already do this as you are able to write the file) : http://ormlite.com/javadoc/ormlite-core/doc-files/ormlite_4.html#Use-With-Android and please follow the instructions about the `DatabaseConfigUtil` ;) Do you have any particular reasons for not using it (technical constraints, not integrated in build phase...) ? – mithrop Oct 02 '15 at 09:48
  • @mithrop I have error **Could not find raw directory which is typically in the res directory** – NickUnuchek Oct 02 '15 at 09:58
  • @mithrop where should be located file **DatabaseConfigUtil.java** ? – NickUnuchek Oct 02 '15 at 10:04
  • @mithrop I have set "Working directory" to /your_workspace/your_project/app/src/main , it works, thanks – NickUnuchek Oct 02 '15 at 10:09
  • @mithrop A lot of thanks for you, my code works – NickUnuchek Oct 02 '15 at 10:15

1 Answers1

2

As said in your comment, you wrote the ormlite_config.txt manually, which is a bad idea. ORMLite provides an utility to write that file for you, which be a lot easier :)

Please take a look at this page to have more information : http://ormlite.com/docs/table-config

Gray
  • 115,027
  • 24
  • 293
  • 354
mithrop
  • 3,283
  • 2
  • 21
  • 40