使用ormlite android存储对象字段

使用ormlite android存储对象字段,android,ormlite,Android,Ormlite,我试图在DB表中存储一个类对象,但ormlite似乎不接受对象类型,下面是我的DB表: 临时联系人数据库表 public class Temp_Contacts { @DatabaseField(generatedId = true,canBeNull = false) private int id; @DatabaseField private Contacts contacts; @DatabaseField private Date

我试图在DB表中存储一个类对象,但ormlite似乎不接受对象类型,下面是我的DB表:

临时联系人数据库表

public class Temp_Contacts {

    @DatabaseField(generatedId = true,canBeNull = false)
    private int id;

    @DatabaseField
     private Contacts contacts;

    @DatabaseField
    private Date data_actualizare;

    Temp_Contacts(){}

    public Temp_Contacts(Contacts contacts, Date data_actualizare){
            this.contacts=contacts;
            this.data_actualizare=data_actualizare;
    }

    public Contacts getContacts() {
        return contacts;
    }

    public void setContactsList(Contacts contacts) {
        this.contact = contacts;
    }

    public Date getData_actualizare() {
        return data_actualizare;
    }

    public void setData_actualizare(Date data_actualizare) {
        this.data_actualizare = data_actualizare;
    }
}
联系人类别

public class Contacts {

    private int ID;
    private String name;
    private List<Telefon> numbers;
    private List<Email> emails;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Telefon> getNumbers() {
        return numbers;
    }

    public void setNumbers(List<Telefon> numbers) {
        this.numbers = numbers;
    }

    public List<Email> getEmails() {
        return emails;
    }

    public void setEmails(List<Email> emails) {
        this.emails = emails;
    }

    public int getID() {
        return ID;
    }

    public void setID(int ID) {
        this.ID = ID;
    }
}
公共类联系人{
私有int-ID;
私有字符串名称;
私人名单号码;
私人名单电子邮件;
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共列表getNumbers(){
返回号码;
}
公共无效集合编号(列表编号){
这个。数字=数字;
}
公共列表getEmails(){
回复邮件;
}
公共电子邮件(列出电子邮件){
这个。电子邮件=电子邮件;
}
公共int getID(){
返回ID;
}
公共无效集合ID(内部ID){
this.ID=ID;
}
}
其主要目的是将Contacts类中的对象存储在ormlite DB中。
我更喜欢这种逻辑,但我也愿意接受其他建议

基本上,您必须将其拆分为不同的表,以便ORMLite能够正确处理它,并具有干净的数据库设计。
您将拥有4个表:
Temp\u Contacts
Contacts
Email
Phone

public class Temp_Contacts {

    @DatabaseField(generatedId = true,canBeNull = false)
    private int id;

    // declare Contacts as a foreign key
    // automatically fetched when Temp_Contacts is loaded
    @DatabaseField(foreign=true, foreignAutoRefresh=true)
    private Contacts contacts;

    @DatabaseField
    private Date data_actualizare;

    ...
 }
联系人:

public class Contacts {

   @DatabaseField(id = true,canBeNull = false)
   private int ID;

   @DatabaseField
   private String name;

   // Use foreign collections for Telefon and Email
   // that are loaded with Contacts
   @ForeignCollectionField(eager = true)
   private ForeignCollection<Telefon> numbers;

   @ForeignCollectionField(eager = true)
   private ForeignCollection<Email> emails;     

   ...
}
这对应于以下数据库结构:

临时联系人:

|id |联系人| id |数据|现实|

联系人:

|id |名称|

电话和电子邮件

public class Telefon {

    // Reference to the Contact object has to be there
    @DatabaseField(canBeNull = false, foreign = true)
    private Contacs contacts;

    ...

}

public class Email {

    // Reference to the Contact object has to be there
    @DatabaseField(canBeNull = false, foreign = true)
    private Contacs contacts;

    ...

}
|联系人| id |其他字段|


有关
ForeignObjects
ForeignCollections
的更多信息,请参见

这是我的第二个想法,但我认为有一种简单的方法可以做事情(减少编码、减少表等),但感谢您的回答。我想这也可以用
数据类型。可序列化的
完成,但我想这会很麻烦。上述解决方案的好处是,它还为您提供了一个很好的表结构hi@super qua--我可以为ormlite使用与gson解析相同的pojo类吗。。。??我不熟悉orm的概念,所以我有点结巴。。。