当尝试使用 ORMLite 持久化类 Goods
时,我不知道如何在此处持久化 componentList
。我发现了一个类似的问题Persisting a Collection class with ORMLite in android , 但似乎没有指出解决问题的方法。
public class Goods extends Object implements Serializable
{
private static final long serialVersionUID = -7560219283718464481L;
...
public ArrayList<String> componentList;
public String getComponentList(){
return componentList;
}
public void setComponentList(ArrayList<String> componentList){
this.componentList = componentList;
}
...
}
最佳答案
I have found a similar question Persisting a Collection class with ORMLite in android, but it seems not pointing the way to solve the problem.
不幸的是,这是 ORMLite 必须存储相关项集合的唯一方法。在您的情况下,您需要一个 Component
类,它具有如下内容:
公共(public)类组件{ @DatabaseField(外国=真) private Goods 商品; ...
然后您的 Goods
类将是:
public class Goods {
@ForeignCollectionField
private Collection<Component> components;
...
要将组件分配给 Goods
表中的条目,您可以创建一个 Component
条目并设置 goods
字段,并且 dao.create(...)
它。
看看 foreign-collection docs这可能会提供更多信息。 引用他们的话:
A foreign collection allows you to add a collection of orders on the account table. Whenever an Account object is returned by a query or refreshed by the DAO, a separate query is made over the order table and a collection of orders is set on the account. All of the orders in the collection have a corresponding foreign object that matches the account. For example:
public class Account {
…
@ForeignCollectionField(eager = false)
ForeignCollection<Order> orders;
…
}
In the above example, the @ForeignCollectionField annotation marks that the orders field is a collection of the orders that match the account. The field type of orders must be either ForeignCollection or Collection – no other collections are supported because they are much heavier with many methods to support.
关于java - 在 Android 中使用 ORMLite 持久化 ArrayList<String>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24350042/