File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Groovy and the fly likes Grails / GORM: How to remove a relation from a M:N relation when deleting an entity? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Languages » Groovy
Bookmark "Grails / GORM: How to remove a relation from a M:N relation when deleting an entity?" Watch "Grails / GORM: How to remove a relation from a M:N relation when deleting an entity?" New topic
Author

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

Kai Wähner
Ranch Hand

Joined: Sep 16, 2009
Posts: 76
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

My IT-blog about my experiences with: Java / JEE, IT Conferences and IT Certifications

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

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.


GenRocket - Experts at Building Test Data
Kai Wähner
Ranch Hand

Joined: Sep 16, 2009
Posts: 76
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Grails / GORM: How to remove a relation from a M:N relation when deleting an entity?