DEV Community

Software Development Academy
Software Development Academy

Posted on • Updated on

Entity Relationships Part 2: Many-To-Many

Creating Class Car Club & Many-To-Many

GitHub Repo Tag

Adding Create Mappings for Many-To-Many

Adding Delete Mappings for Many-To-Many

GitHub Repo Tag

Clarification

Many-To-Many deletion

When you try to delete an entity on the non-owning side of a many-to-many relationship that has associations to the other entity you might get a 'violating foreign key constraint' error. What you would have to do then is to manually remove the associations from the non-owning entities' collection. In this case, that would be

// Make sure to have a getter for clubs on Person.java
for (CarClub carClub: person.getClubs()) {
    carClub.getMembers().remove(person);
}
Enter fullscreen mode Exit fullscreen mode


Previous article |
Next article

Top comments (0)