我试图在DB表中存储一个类Object,但似乎ormlite不接受对象类型,这里是我的数据库表:
Temp_Contacts数据库表
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;
}
}
这样做的主要目的是在ormlite DB中将对象类存储在Contacts类中。 我更喜欢这种逻辑,但我也对其他建议持开放态度。
答案 0 :(得分:3)
基本上你必须将它拆分成不同的表,以便ORMLite正确处理它并拥有干净的数据库设计。
您最终会有4个表:Temp_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;
...
}
Telefon和电子邮件
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;
...
}
这对应于以下数据库结构:
Temp_Contacts:
| id | contacts_id | data_actualizare |
联系人:
| id |名字|
Telefon和电子邮件
| contacts_id | otherfields |
有关ForeignObjects
和ForeignCollections
的详情,请参阅ORMLite documentation