Having a list of strings represented in a database using ORMLite

ormlite

Solution

First of all, List doesn't implement `Serializable` but ArrayList certainly does as well as most of the common collection implementations. But storing a huge list is probably not the best of doing this from a pure object model standpoint.

So why don't you want to have a table of all tags? That's the best way from a pure model standpoint. It will require a 2nd query if you need them every time. That's the way hibernate would store a list or array of tags.

After reading your comment @creen, I still think you do want a table of tags. Your model class would then have:

@ForeignCollectionField
Collection<Tag> tags;

The `tags` table would not have a single tag named `"red"` with multiple model classes referring to it but multiple `"red"` entries. It would look like:

model_id    name
1           "red"
1           "blue"
2           "red"
3           "blue"
3           "black"

Whenever you are removing the model object, you would first do a `tags.clear();` which would remove all of the tags associated with that model from the tags table. You would not have to do any extra cleanup or anything.

Problem

First of I am new to ORMLite. I would like my model class to have a field which is a list of strings, that would eventually hold a list of tags for my model object. Which ORMLite annotations should I use? Firstly I don't want to have a table of all tags, and then use the `@ForeignCollectionField`. Also I thought of using the `@DatabaseField(dataType=DataType.SERIALIZABLE)` annotation, but it turns out that `List<String>` doesn't implement the `Serializable` interface. What are your suggestions?

Original source