所以我创建了一个名为 Food
的通用类。
我有一个 FoodStuff 是糖果,所以我创建了一个名为“sweets”的数据库表,其中类的 3 个属性存储在类中,即糖果的 foodname、id 和 sugar levels
.
@DatabaseTable(tableName = "sweets")
@JsonIgnoreProperties(ignoreUnknown = true)
public class Food implements Serializable {
@DatabaseField(id = true)
@JsonProperty
private String id; //for both sweets and meats
@DatabaseField
@JsonProperty
private String foodname; //for both sweets and meats
@DatabaseField
@JsonProperty
public int sugar; //for sweets only
@DatabaseField
@JsonProperty
public int protein; //for meats only
现在假设我想通过在“糖果”选项卡旁边的顶部添加一个额外的选项卡来扩展我的 Android 应用程序,该选项卡将显示所有包含肉类的食品。
我想在我的名为 meats
的 sqlite 数据库中添加一个额外的 databaseTable,它应该只存储 foodname、id 和蛋白质水平。
这甚至可以在 ORMLite 中使用一个类吗?我认为它可能看起来像这样,但这不起作用,因为它混淆了 ORMLite 应该将数据导入到哪个表:
@DatabaseTable(tableName = "sweets")
@DatabaseTable(tableName = "meats")
@JsonIgnoreProperties(ignoreUnknown = true)
public class Food implements Serializable {
@DatabaseField(id = true)
@JsonProperty
private String id;
@JsonProperty
private String foodname;
@JsonProperty
public int sugar;
@JsonProperty
public int protein;
最佳答案
Is this even possible using one class in ORMLite? I thought maybe it would look something like this but this doesn't work as it confuses ORMLite as to which table it should import the data into:
我会考虑使用子类。所以你会让 Food
成为 abstract
然后有:
@DatabaseTable(tableName = "sweets")
public class Sweets extends Food {
和
@DatabaseTable(tableName = "meats")
public class Meats extends Food {
如果你必须使用相同的类,那么你可以 configure the tables programmatically .
关于Android ORMLite : one object but two tables,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34161980/