I have some web application for placement on external sites. This application is a widget for comments like disqus (disqus.com).
In application each widget it’s a entity object with a lot of many attributes.
For instance:
@Entity
class Widget {
private User admin; //same as owner, administrator, creator
private String site; //example.com
private String providers; //google_plus,facebook,twitter,lastfm and others
//... and a lot of many other attributes
}
At the now I develop the list of the bad words. This list will be managed by an administrator widget (owner).
For this task I have two ways for implements:
First way:
@Entity
class Widget {
...
@ElementCollection(fetch = FetchType.LAZY)
@Column(name = "bad_words", length = 20)
private Set<String> badWords= new HashSet<String>();
...
}
that is keep as relation in second table
Second way:
@Entity
class Widget {
...
@Column(name = "bad_words", length = 2000)
private String badWords;
...
}
that is keep as string with words splited by some separator, for instance: “fukc,azz,shitt,…”
So the question is what’s the best way to keep bad words in the each widgets entity? Case 1, 2 or some thing else?
Definitely Option 1. Option 2 violates First Normal Form.
Think about having all the words concatenated as discuss in the Second Way. A lot of difficulties in querying the table will arise. Also, a lot of awful code for updating/deleting/adding words will appear. You don’t want any of this.
Having all the words in a collection (First Way) will permit querying them easily and will make all the CRUD operations simpler.