Persisting a Collection class with ORMLite in android

前端 未结 2 377
傲寒
傲寒 2021-02-04 16:29

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 n

相关标签:
2条回答
  • 2021-02-04 17:03

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

    0 讨论(0)
  • 2021-02-04 17:18

    If you want to save a Collection (such as an ArrayList) of objects to ORMLite the easiest way is this:

    @DatabaseField(dataType = DataType.SERIALIZABLE)
    private SerializedList<MyObject> myObjects;
    

    and to get my list of objects:

    public List<MyObject> getMyObjects() {
      return myObjects;
    }
    
    0 讨论(0)
提交回复
热议问题