I have an entity class named Article, and another entity named ArticleTag. These two entities are manyTomany relationship, meaning that many articles could be linked to the same tag, and that many tags could be associated with the same article.
In my Article entity, I have the following:
@ManyToMany(cascade = {CascadeType.ALL}) @JoinTable(name = "table_articleId_tagId", joinColumns = {@JoinColumn(name ="article_id")}, inverseJoinColumns = {@JoinColumn(name = "tag_id")}) private Set<ArticleTag> tags = new HashSet<ArticleTag>(); In my ArticleTag entity, I have this:
@ManyToMany(mappedBy = "tags") private Set<Article> articleSet = new HashSet<Article>(); When my controller is called to delete an article, I use a method in my ArticleDao class that is essentially doing this:
Transaction tx = session.beginTransaction(); session.delete(article); tx.commit(); However, my article can't be deleted. I am not sure what causes this failure. I am wondering if I should use the Eager Fetch to delete the tags first. But this does not sound right to me, because the same tag could be linked to another article. So what's the right way of deleting article without deleting its associated tags that are still associated with another article?
Edit:
Thanks to @Vlad Mihalcea for the suggestion. With this inspiration, I started to think about reconstructing the ManyToMany relationship between Article and ArticleTag to two OneToMany relationships by creating an intermediate Entity named ArticleAndTagLink.
So my idea is to create OneToMany relationships between Article/ArticleTag with ArticleandTagLink entity. This will enable me to take advantage of the "orphanRemoval = true" annotation on either Article or ArticleTag entity. When I want to delete an Article, this annotation will help me delete the associated ArticleandTagLink object and and I will delete the tag if it does not associate any other ArticleTag objects any more. I think this may work and may probably be more efficient than iterating through tags. If it works, I'll report back tomorrow!