Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store the arraylist in ormlite database

I am trying to save the arraylist of class objects into the ormlite database , but it is giving the error , java.lang.IllegalArgumentException: No fields have a DatabaseField annotation in class java.util.ArrayList

my code is

@DatabaseTable
public class ManageModelDetails {

@DatabaseField(generatedId = true)
private int id;

@DatabaseField(foreign = true, foreignAutoRefresh = true)
private ArrayList<ModelDetails> listModelDetails;

// ===============================================
public ManageModelDetails() {
    super();
}

// ===============================================

public ManageModelDetails(int id, ArrayList<ModelDetails> listModelDetails) {
    super();
    this.id = id;
    this.listModelDetails = listModelDetails;
}

// ===============================================

public void setId(int id) {
    this.id = id;
}

public int getId() {
    return id;
}

public void setModelList(ArrayList<ModelDetails> listModelDetails) {
    this.listModelDetails = listModelDetails;
}

public ArrayList<ModelDetails> getModelList() {
    return listModelDetails;
}

}
like image 890
mdDroid Avatar asked Aug 26 '13 10:08

mdDroid


2 Answers

I think you need to use Foreign Collections. Take a look here:

  • Foreign Collections
  • Another similar question
like image 52
Uriel Frankel Avatar answered Nov 11 '22 19:11

Uriel Frankel


If you want to save 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;
}

and here is SerializedList:

public class SerializedList<E> extends ArrayList<E> implements Serializable {
}
like image 28
My House Avatar answered Nov 11 '22 20:11

My House