Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android ormlite - storing string array

I want to persist a class which contain String array. How to do it in ormlite? For example,

class A {
    int age;
    String[] childrenNames = new String[2];
}
like image 698
jrhamza Avatar asked Dec 06 '22 04:12

jrhamza


2 Answers

First you make the class Serializable. You can optionally add the table name at the top of the class by annotation.

then for the variables you have to add the database field annotation. In case of the string array you also have to annotate it as a Serializable datatype. You will get something like this:

@DatabaseTable(tableName = "A")
Class A implements Serializable{

    @DatabaseField
    int age

    @DatabaseField(dataType = DataType.SERIALIZABLE)
    String[] childrenNames = new String[2];
}

Also dont forget to create getters and setters for each of the variables.

like image 170
nikitanl Avatar answered Dec 10 '22 11:12

nikitanl


I want to persist a class which contain String array. How to do it in ormlite? For example,

You could store this as a serialized stream for sure.

However, a better way to do this is to use a ForeignCollection. ORMLite does not do the magic fu that other ORM libraries do to support arrays. Maybe it should. In the meantime, here are the docs on setting up another table for your children-names:

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

One table would be for A. Another table would be for the ChildrenName. Each ChildrenName entity would have a foreign-field of A which would show which A each name corresponded to.

like image 28
Gray Avatar answered Dec 10 '22 12:12

Gray