Search code examples
grailsgrails-orm

Link Domain Objects in Grails without cascading deletes


How do I link two domain classes in Grails without having the deletes cascade from one to another? I have two domains which are related, but one is not intrinsically superior to the other. This is basically the idea:

class Project{
    static hasMany = [workers:Employe]
}


class Employe{
    static hasMany = [jobs:Project]
}

If a certain project gets closed down all of the workers shouldn't be deleted, and if one worker quits his jobs he shouldn't be deleted either.


Solution

  • You could split up the domains:

    class Project{
    
       def getWorkers() {  
        EmployeeProject.findAll("from EmployeeProject as e where e.project.id=?", [this?.id], [cache: true])
       }
    }
    
    class Employee {
    
        def getProjects() {
          EmployeeProject.findAll("from EmployeeProject as ep where ep.employee.id=?", [this?.id], [cache: true])
       }
    }
    
    class EmployeeProject {    
      Employee employee
      Project project   
    }
    

    Then you can just use project.workers, employee.projects and delete the EmployeeProject objects without effecting the other classes.