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];
}
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With