Edit: Working and tested solution.
The Document
class declaration is,
@Entity
@XmlRootElement(name = "html")
@XmlType(propOrder = {"title", "docid", "audience", "lastModified", "visibility", "keywords", "alternateTitles", "bodyElement"})
public class Document implements Serializable {
and our keywords
field is defined as,
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
@OrderColumn
@JoinColumn(name = "docid")
@XmlPath("head/meta/kb:keywords/kb:keyword")
private Set<Keyword> keywords;
The Keyword
class declaration is,
@Entity
@Table(name = "kywd_t")
@XmlAccessorType(XmlAccessType.FIELD)
public class Keyword implements Serializable {
and added an auto-generated id,
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@XmlTransient
private Long id;
and the document
field is,
@ManyToOne
@JoinColumn(name = "docid", insertable = false, updatable = false)
@XmlTransient
private Document document;
This solution works perfectly for our use case. If we have documents like this, documentid(keyword1,keyword2,...)
. For example: abcd(key1,key2)
and efgh(key1,key2)
. Basically two different documents with the same keywords, the database would have a document table and a keyword table. The document table would have a docid
column (and the other columns) containing abcd
and efgh
and the keyword table would have 3 columns: text
(representing the keyword value), the auto-generated id, and the docid
field to join the two tables. It would have 4 rows: (1, key1, abcd)
, (2, key2, abcd)
, (3, key1, efgh)
, (4, key2, efgh)
.
If we remove a keyword from the Keyword
object and save it gets removed from the database. If we update keywords they get updated. Note: The ids
don't remain the same.