在android中使用ORMLite保存Collection类
我有两个类设置如下。我很困惑什么时候需要将某些东西注释为外国收藏,什么时候不需要。这可能听起来很愚蠢,但 ORMLite 文档中没有任何地方说明是否允许非外部集合。如果我有一个 int
列表,它被自动装箱为 Integer
,该怎么办?我可以使用 Collection
上方的标准 @DatabaseField
来保留它吗?根据 ORMLite
,外部集合还必须具有反向引用才能工作(对父级的引用,给定一对多关系)。对于下面的示例,我假设您应该将 myBList
注释为外部集合,并将 myA
设为外部对象,但是您如何处理 myStringList?
我在这里看到了示例代码,但它没有回答我的问题: http://ormlite.com/docs/examples< /a>
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;
}
}
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
?
I Have seen sample code here but it doesn't answer my questions: 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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
@罗伯特是正确的。当 hibernate 持久化一个集合(甚至是一个数组)时,它会使用带有外部 id 的隐藏额外表来实现这一点——换句话说,就是隐藏的外部集合。 ORMLite 试图遵守 KISS 原则,因此您可以“手动”定义外部集合。
我添加了有关存储集合的更多详细信息。
这意味着您无法保留
Integer type 因为没有foreign-id。此外,您的代码还可以定义外部集合
Collection
或ForeignCollection
。其中任一者都将使用ForeignCollection
设置。 ORMLite 不支持列表或其他集合类型。@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.
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 collectionCollection<Order>
orForeignCollection<Order>
. Either one will be set with aForeignCollection
. ORMLite does not support lists or other collection types.如果您想将对象的集合(例如 ArrayList)保存到 ORMLite,最简单的方法是:
并获取我的对象列表:
If you want to save a Collection (such as an ArrayList) of objects to ORMLite the easiest way is this:
and to get my list of objects: