Persisting a Collection class with ORMLite in android

16,796

@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.

http://ormlite.com/docs/foreign-collection


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.

Share:
16,796
Ryan
Author by

Ryan

Updated on June 21, 2022

Comments

  • Ryan
    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 of ints which get autoboxed into Integers? 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;
        }
    }
    
  • Ryan
    Ryan almost 13 years
    Thank 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
    Ready4Android over 12 years
    I 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
    Gray over 12 years
    See the docs @Ready4Android: ormlite.com/javadoc/ormlite-core/doc-files/ormlite_2.html#SE‌​C30 I only support Collection or ForeignCollection because am writing the classes which support them. List and Set are a lot more complicated interfaces.
  • lu yuan
    lu yuan almost 10 years
    Then how to store ArrayList<String> with ORMLite?
  • osayilgan
    osayilgan almost 9 years
    @Gray I couldn't see anything in the documentation about Collection<Integer> or Collection<String>. When I use any of these, I get an exception saying IllegalArgumentException: ORMLite does not know how to store interface java.util.Collection