gpt4 book ai didi

android - 使用 ormlite android 存储对象字段

转载 作者:行者123 更新时间:2023-11-29 17:51:43 24 4
gpt4 key购买 nike

我试图在数据库表中存储一个类对象,但 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 数据库中存储来自 Contacts 类的对象。我更喜欢这种逻辑,但我也愿意接受其他建议。

最佳答案

基本上,您必须将其拆分到不同的表中,以便 ORMLite 正确处理它并拥有干净的数据库设计。
您最终会得到 4 个表:Temp_ContactsContactsEmailPhone:

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;

...
}

电话和电子邮件

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 |数据实现 |

联系人:

|编号 |姓名 |

电话和电子邮件

|联系人 ID | 其他领域 |

有关 ForeignObjectsForeignCollections 的更多信息,请参阅 ORMLite documentation

关于android - 使用 ormlite android 存储对象字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22449769/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com