ORMLite不知道如何存储类

时间:2016-12-29 09:00:06

标签: android sqlite api retrofit ormlite

Person.class

@DatabaseTable(tableName =PERSON_TABLE_NAME)
class Person{
    public final static String PERSON_TABLE_NAME="person";
        @DatabaseField
       private  String name;
        @DatabaseField
       private   String surname;
        @DatabaseField(id = true)
       private  int id;
        @ForeignCollectionField
       private ForeignCollection<Motivations> motivationsData;

    ArrayList<Motivations> motivations;

//getters and setters
    }

我的信封类从API获取对象

    class PersonApiEnvelope{
         private String sign ;

    private int last_call;

    private List<Person> data;

    //getters and setters
        }

Motivation.class

@DatabaseTable(tableName =MOTIVATIONS_TABLE_NAME)
    class Person{
        public final static String MOTIVATIONS_TABLE_NAME="motivations";
            @DatabaseField
           private  String name;
            @DatabaseField
          private   String content;
            @DatabaseField(id = true)
           private  int id;
            @DatabaseField (canBeNull = true,foreign= true)
           private Person person;

    //getters and setters
        }

我将Api的响应映射到PersonEnvelopeClass,然后将数据映射到Person.class,然后当我想创建Dao<Person,Integer> personDAO = databaseHelper.getPersonDAO时出现错误

  

ORMLite不知道如何存储类   com.example.k.Database.Model.Person for field person。使用另一个   类或自定义持久性

没有ForeginCollection可以正常工作。

如何在没有此错误的情况下将ArrayList与其他对象的许多字段一起保存

我想我必须将ArrayList重写为Collection,因为我无法从api中读取集合。

Dao personDAO = databaseHelper.getPersonDAO(); //这里我得到了一个例外

   List<Person> personList;         
         PersonApiEnvelope envelope = response.body();
                                personList = envelope.getData();

                                for(Person m: personList ){
      personDAO .assignEmptyForeignCollection(m,"motivationsData"); 
                                if(m.getMotivations()!=null){ 
           ForeignCollection<Motivations>  temp=m.getMotivationsData();
                                 temp.addAll(m.getMotivations());
                                 m.setMotivationsData(temp);
                                  }

personDAO.createOrUpdate(m);
}

我尝试使用@DatabaseField(dataType = DataType.SERIALIZABLE)代替@ForeignCollectionField,但数据未在ormLite中正确创建,因此我需要使用@ForeignCollectionField

编辑我已将Collection更改为ForeignCollection和assignEmptyForeignCollection但仍然出现此错误

0 个答案:

没有答案