Persisting a Collection class with ORMLite in android
@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 collection Collection<Order>
or ForeignCollection<Order>
. Either one will be set with a ForeignCollection
. ORMLite does not support lists or other collection types.
Ryan
Updated on June 21, 2022Comments
-
Ryan almost 2 years
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 ofint
s which get autoboxed intoInteger
s? can I just persist this using a standard@DatabaseField
above theCollection
? A foreign collection, according toORMLite
, 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 annotatemyBList
as a foreign collection as well as makingmyA
a foreign object, but how could you handlemyStringList
?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; } }
-
Ryan almost 13 yearsThank you Gray. Does this mean that you can not persist a type such as Integer since there is no foreign id inside of int, linking it back to its owner? Lastly the documentation states it only supports "Collections" or "ForeignCollections". Does this mean it doesn't support something that implements Collection, like Lists?
-
Ready4Android over 12 yearsI would join Ryans question in wether only Collections are supported. After all Collection is not a concrete type, so there needs to be some concrete type as well (for instancing). The thing is - lists ARE collections (they just extend them), that's what confused me about the comment... Did you mean to say, that only pure Collection implementations are allowed? I think one would be hard-pressed to find a pure Collection implementation (unless you write your own). Set (interface) for instance derives from Collection and Iterable. An implementation is Hashset.
-
Gray over 12 yearsSee the docs @Ready4Android: ormlite.com/javadoc/ormlite-core/doc-files/ormlite_2.html#SEC30 I only support
Collection
orForeignCollection
because am writing the classes which support them.List
andSet
are a lot more complicated interfaces. -
lu yuan almost 10 yearsThen how to store ArrayList<String> with ORMLite?
-
osayilgan almost 9 years@Gray I couldn't see anything in the documentation about
Collection<Integer>
orCollection<String>
. When I use any of these, I get an exception sayingIllegalArgumentException: ORMLite does not know how to store interface java.util.Collection