Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Deleting a Many to Many entry while keeping both objects in the database

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I currently have a Many to Many relationship between Events and Users. The auto generated table in my DB called event_registrations keeps track of the relationship and which user goes to which event based on their ids.

What I want to do is have a controller method that takes in an Event id together with a list of user IDs so that the given users get removed from the given event.

Here are my model classes:




What I've tried so far in the EventController:




This runs without problems but the the entries still exist in the join table afterwards. When debugging this, the users do get deleted from the Event object but the changes don't get persisted to the database.

Any help is appreciated!
 
Sheriff
Posts: 22796
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Depending on the JPA implementation, you may have to remove from the owning side. I've had problems after migrating from WebLogic + TopLink to Spring Boot + Hibernate where many-to-many links weren't updated correctly if I updated the non-owning side.

In your case, you have two sides: User is the owning side, and Event is the non-owning side (you can see this from the "mappedBy" on Event). That means that you should remove from User.eventRegistrations, not from Event.userList.

A quick attempt without any additional helper methods:
reply
    Bookmark Topic Watch Topic
  • New Topic