Android ORMLite:一个对象,两个表
Android ORMLite: one object but two tables
所以我创建了一个名为 Food
的通用 class。
我有一个 FoodStuff 是糖果,所以我创建了一个名为 'sweets' 的数据库表,其中 class 的 3 个属性存储在 class 中,即 foodname, id and 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
现在假设我想通过在 'sweets' 选项卡旁边的顶部添加一个额外的选项卡来扩展我的 android 应用程序,该选项卡将显示所有包含肉类的食物。
我想在我的名为 meats
的 sqlite 数据库中添加一个额外的 databaseTable,它应该只存储 foodname, id and protein levels.
甚至可以在 ORMLite 中使用一个 class 吗?我认为它可能看起来像这样,但这不起作用,因为它混淆了 ORMLite 应该将数据导入哪个table:
@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:
我会考虑使用 subclass。所以你会让 Food
成为 abstract
然后有:
@DatabaseTable(tableName = "sweets")
public class Sweets extends Food {
和
@DatabaseTable(tableName = "meats")
public class Meats extends Food {
如果你必须使用相同的 class 那么你可以 configure the tables programmatically.
所以我创建了一个名为 Food
的通用 class。
我有一个 FoodStuff 是糖果,所以我创建了一个名为 'sweets' 的数据库表,其中 class 的 3 个属性存储在 class 中,即 foodname, id and 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
现在假设我想通过在 'sweets' 选项卡旁边的顶部添加一个额外的选项卡来扩展我的 android 应用程序,该选项卡将显示所有包含肉类的食物。
我想在我的名为 meats
的 sqlite 数据库中添加一个额外的 databaseTable,它应该只存储 foodname, id and protein levels.
甚至可以在 ORMLite 中使用一个 class 吗?我认为它可能看起来像这样,但这不起作用,因为它混淆了 ORMLite 应该将数据导入哪个table:
@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:
我会考虑使用 subclass。所以你会让 Food
成为 abstract
然后有:
@DatabaseTable(tableName = "sweets")
public class Sweets extends Food {
和
@DatabaseTable(tableName = "meats")
public class Meats extends Food {
如果你必须使用相同的 class 那么你可以 configure the tables programmatically.