Deleting Rows in a Hibernate @ManyToMany Child Table

We added Tag Management in NixMash Spring and one of the required tasks was tag deletion. In Hibernate terms, tags are a @ManyToMany Child object of Post, linked in SQL by way of a secondary lookup table.

Here’s the @ManytoMany relationship defined on the Post side.

And in the Tag Entity.

We have a TagRepository where we might be tempted to simply execute a tagRepository.delete(tag), but that would be wrong. And what about the record in our SQL Lookup table, hmmm? We have to handle any Entity relationships first. Once those are released we can remove the tag record, which will also remove the record in the post_tag_id table. Pretty sweet.

Source Code Notes for this Post

All source code discussed in this post can be found in my NixMash Spring GitHub repo and viewed online here.