Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing List of objects using DataType.SERIALIZABLE in ormlite android

How to save an ArrayList using ORMlite in android my model is as follows

class Model{
    @DatabaseField
    public String type = "";

    @DatabaseField
    public String name = "";

    @DatabaseField
    public Date dateTime = null;

    @DatabaseField
    ArrayList<Item> items = null;
}

And Item class has

class Item{
     @DatabaseField
     String itemName;
     ...
}

I am getting the following exception :

java.sql.SQLException: ORMLite can't store unknown class class
      java.util.ArrayList for field 'items'.   Serializable fields must specify
      dataType=DataType.SERIALIZABLE

But when i specify my field as

@DatabaseField(dataType = DataType.SERIALIZABLE)
ArrayList<Item> items = null;

The compiler gives a error called field cannot be resolved please help me with this.

like image 483
Lochana Ragupathy Avatar asked Jul 25 '26 18:07

Lochana Ragupathy


1 Answers

I've just changed the error message to be:

ORMLite does not know how to store class java.util.ArrayList for field 'items'. Use another class, custom persister, or to serialize it use dataType=DataType.SERIALIZABLE

Maybe that makes more sense? ORMLite is trying to say that it doesn't know how to store lists in a field. If you want to store collection of items then maybe you should take a look at the foreign-collection documentation.

like image 164
Gray Avatar answered Jul 28 '26 12:07

Gray



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!