Hello everyone,
I have two classes Article and Tag. An article can have a list of tags and a tag can be present in any article. I will show the relevant part of the code.
Article.java
Code:
public class Article implements Serializable, Auditable {
......
private List<Tag> tagList = null;
......
}
Tag.java
Code:
public class Article implements Serializable, Auditable {
......
private int id = 0;
private String keyword = null;
private int count = 0;
private Timestamp creationDate = new Timestamp(new Date().getTime());
private List<Article> articleList = null;
......
}
And here is the mapping files:
Article.hbm.xml
Code:
....
<bag name="tagList" table="article_tag" cascade="all">
<key column="id_article" not-null="true"/>
<many-to-many column="id_tag" class="com.model.metadata.Tag"/>
</bag>
....
Tag.hbm.xml
Code:
....
<bag name="articleList" table="article_tag" cascade="all" inverse="true">
<key column="id_tag" not-null="true"/>
<many-to-many column="id_article" class="com.model.article.Article"/>
</bag>
....
And this is the part of my code where i get the article data related to tags and insert/update my article
Code:
.....
List<Article> articleList = new ArrayList<Article>();
tagValues = StringUtils.split(value, " ");
for (int i = 0; i < tagValues.length; i++) {
this.tag = new Tag();
this.tag.setKeyword(tagValues[i]);
this.tag.setArticleList(articleList);
this.tagList.add(this.tag);
}
......
this.article.setTagList(this.tagList);
......
When i insert a new article with new tags everything works fine, the problem is when i try to update the article, my code does not update the existing tags, it inserts them again.
Any idea of what could be wrong?
Thanks a lot in advance.