Showing posts from November, 2016

Deleting @ManyToMany parent entity without affecting child on Hibernate

Oh Dear!!

It's a nightmare getting MySQLIntegrityConstraintViolationException when you're designing complex relational database structure for your app. Most of the time hibernate won't complain with your approximation but this won't work when it comes to bidirectional relations.

Let me show an example.

But first consider a scenario that we're making a movie database application. We have two entities and

The use case is,

User can add multiple movies in a list.
And a movie can be added to multiple list.
User can execute CRUD operations on their list. that means user can create, edit and delete his list.
Admin can delete a Movie.

So it's a @ManyToMany relationship right?

Okay lets dig into the example.

Our two entities are:
@Entity(name = "movie")
public class Movie extends BaseEntity {
private String name;
private String type;
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<CustomList&g…