java 在 android 中使用 ORMLite 保留 Collection 类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6272155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Persisting a Collection class with ORMLite in android
提问by Ryan
I have two classes setup like the following. I am confused as to when I need to annotate something as an foreign collection and when I do not. This may also sound silly, but nowhere in the ORMLite
documentation does it say whether or not a non-foreign collection is allowed. What if I have a List of int
s which get autoboxed into Integer
s? can I just persist this using a standard @DatabaseField
above the Collection
? A foreign collection, according to ORMLite
, must also have back reference for it to work (a reference to the parent, given a one to many realtionship). For the example below, I am assuming you should annotate myBList
as a foreign collection as well as making myA
a foreign object, but how could you handle myStringList
?
我有两个类设置,如下所示。我很困惑什么时候需要将某些东西注释为外国集合,什么时候不需要。这听起来也很愚蠢,但ORMLite
文档中没有任何地方说明是否允许非外国集合。如果我有一个自动int
装箱到Integer
s 的 s 列表怎么办?我可以使用@DatabaseField
高于 的标准来坚持这一点Collection
吗?根据 ,外国集合ORMLite
还必须有反向引用才能工作(对父集合的引用,给出一对多的关系)。对于下面的示例,我假设您应该将注释myBList
作为外部集合并制作myA
外部对象,但是您如何处理myStringList
?
I Have seen sample code here but it doesn't answer my questions: http://ormlite.com/docs/examples
我在这里看到了示例代码,但它没有回答我的问题:http: //ormlite.com/docs/examples
public class A {
private Set<B> myBList = new HashSet<B>();
private List<String> myStringList = new ArrayList<String>();
private long id;
public A(){}
public Set<B> getMyBList() {
return myBList;
}
public void setMyBList(Set<B> myBList) {
this.myBList = myBList;
}
public List<String> getMyStringList() {
return myStringList;
}
public void setMyStringList(List<String> myStringList) {
this.myStringList = myStringList;
}
public void setId(long id){
this.id = id;
}
public long getId(){
return id;
}
}
public class B {
private int myInt;
private String myString;
private A myA;
private long id;
public B(){}
public A getMyA(){
return myA;
}
public A setMyA(A a){
myA = a;
}
public int getMyInt() {
return myInt;
}
public void setMyInt(int myInt) {
this.myInt = myInt;
}
public String getMyString() {
return myString;
}
public void setMyString(String myString) {
this.myString = myString;
}
public void setId(long id){
this.id = id;
}
public long getId(){
return id;
}
}
回答by Gray
@Robert is correct. When hibernate persists a collection (or even an array), it does so with hidden extra tables with foreign ids -- in other words hidden foreign collections. ORMLite tries to adhere to the KISS principle and so has you define the foreign collections "by hand" instead.
@罗伯特是正确的。当 hibernate 持久化一个集合(甚至是一个数组)时,它会使用带有外部 id 的隐藏的额外表——换句话说,隐藏的外部集合。ORMLite 尝试遵守 KISS 原则,因此您“手动”定义了外部集合。
I've added more details about storing collections.
我添加了有关存储集合的更多详细信息。
This means that you cannot persist an Integer
type because there is no foreign-id. Also, your code can define a foreign collection Collection<Order>
or ForeignCollection<Order>
. Either one will be set with a ForeignCollection
. ORMLite does not support lists or other collection types.
这意味着您无法持久化Integer
类型,因为没有外来 ID。此外,您的代码可以定义外部集合Collection<Order>
或ForeignCollection<Order>
. 任何一个都将设置为ForeignCollection
. ORMLite 不支持列表或其他集合类型。
回答by My House
If you want to save a Collection (such as an ArrayList) of objects to ORMLite the easiest way is this:
如果要将对象的集合(例如 ArrayList)保存到 ORMLite,最简单的方法是:
@DatabaseField(dataType = DataType.SERIALIZABLE)
private SerializedList<MyObject> myObjects;
and to get my list of objects:
并获取我的对象列表:
public List<MyObject> getMyObjects() {
return myObjects;
}