• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Grails / GORM: How to remove a relation from a M:N relation when deleting an entity?

 
Kai Wähner
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have the following situation: A project has several employees. An employee is assigned to several projects. There is no "belongsTo"-cascade because if I delete a project or
an employee, I do not want to delete the other one, of course. Two questions:

1) How do I remove the relations in the table project_employee if I delete the employee? If I use the "belongsTo"-cascade, then the table project_employee would be empty, but the project
would be deleted, too ?!
Do I have to do it manually by getting all projects of the employee,
and then remove the employee from every employee-list of each project? There should be an easier way?

2) A project has sub-projects (self referencing hasMany-relationship). The sub-project also has a M:N relation to employees. I want to realize a constraint, so that
an employee may only be within a sub-project if he is also assigned to the main project. How do I realize this constraint?

Thanks a lot for help...

Best regards, Kai
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I find it kind of difficult to follow you. The best thing would be to show us your domain classes to compliment your description of the problem.
 
Kai Wähner
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I will explain with the domain classes.




These domains create these tables: "projekt", "employee", "project_employee" for the M:N relationship, and "project_subproject"

1) How do I delete an employee and the entries within project_employee, but not the project?

2) Project has Sub-Projects. I need a constraint, so that an employee may only be member of a sub-project, if it also is a member in the main project already.


I hope you can follow me now, viewing the two domains?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic