Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Hibernate: cascade deletion of a many-to-many relationship

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

How do I get Hibernate to delete a many-to-many relation automatically when I have it delete one of the related items?

For example:
I have two entities (domain objects), let's say Employee and Project. One or more employees can be involved in one or more projects.
The domain objects are mapped to the tables EMPLOYEE and PROJECT respectively. A third table, EMPLOYEE_PROJECT, functions as the coupling between the two, holding only a EMPLOYEE_ID (with a constraint to EMPLOYEE.ID) and a PROJECT_ID (with a constraint to PROJECT.ID).

Imagine I delete a Project using session.delete(myProject).
Hibernate complains it can not delete the project, because of the constraint with EMPLOYEE_PROJECT.PROJECT_ID.
I want it to delete that coupling as well, without deleting any Employees of course!

The many-to-many relationship is defined in the mapping file of Employee.
On the internet I found a suggestion to not define a many-to-many in Employee, but to have both Project and Employee refer one-to-many to the coupling table. See:
http://forum.hibernate.org/viewtopic.php?p=2319035&sid=b0f26afec36fb875f485784b8ea2c5d6
Would this really be necessary to solve the problem?

Maybe I could move the many-to-many definition to the Project mapping file instead, but then I would have the same problem when deleting an Employee.

Am I overseeing something? Help is much appreciated! Thanks!










[ August 24, 2006: Message edited by: Kjeld Sigtermans ]
 
Kjeld Sigtermans
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I just solved my own problem yeehaa..
I also created a many-to-many definition in the mapping file for Project, and just like the Employee class has a Set getProjects(), I now added a Set getEmployees() to the Project class.
It now works like I'd expect: when deleting a Project, all couplings with Employees are deleted as well, and the Employees in question just remain.
 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, so what was your solution?

I would assume that you can't use a cascade option because of the unknown Many side knowing if any record on that other side points to other records on your side.

Mark
 
Kjeld Sigtermans
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I tried to explain the solution in my previous message, sorry if I was unclair.
Before, I had only defined a many-to-many in Employee. Before posting my message I assumed that is was impossible to also create a many-to-many in Project... but I tried and it worked out perfectly.

I did not implement a cascade attribute in either one. Thanks!
 
They worship nothing. They say it's because nothing lasts forever. Like this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic