Question
Is it possible to delete a row into a join table created by @ManyToMany annotation?
Context
With this schema :
- TAG
- TAGS_ARTICLES
- ARTICLE
When a tag is removed from public Set<Tag> tags
(a list into ARTICLE
class) corresponding rows into TAGS_ARTICLES
is removed too, but not the tag into TAG
table.
The only way is to create a SQL script or JPA/Hibernate allow us to do that with annotations?
Code
My current code is : article.getTags().remove(tag);
This line remove the tag from the list, but the change is not done in database.
Conclusion
I saw this post : How to delete a row in join table with JPA , but relative tag must be deleted too (not my case).
Thanks.
Edit 1 : Expected result in database
Before delete
ARTICLE
| article_id |
| a1 |
| a2 |
| a3 |
TAGS_ARTICLES
| article_id | tag_id |
| a1 | t1 |
| a1 | t2 |
| a2 | t2 |
TAG
| article_id |
| t1 |
| t2 |
After delete t1 from a1 tag list
ARTICLE
| article_id |
| a1 |
| a2 |
| a3 |
TAGS_ARTICLES
| article_id | tag_id |
| a2 | t1 |
| a2 | t2 |
TAG
| article_id |
| t1 |
| t2 |
Edit 2 : Join table code
@Entity
public class Article {
...
@ManyToMany
@JoinTable(name = "tags_articles",
joinColumns = @JoinColumn(name = "idarticle"),
inverseJoinColumns = @JoinColumn(name = "idtag")
)
private Set<Tag> tags = new HashSet<>();
...
}