• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

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

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • 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: 21689
101
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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:
 
Not so fast naughty spawn! I want you to know about
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!