Ryan Stull
Ryan Stull

Reputation: 1096

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.

Upvotes: 0

Views: 39

Answers (1)

brwngrldev
brwngrldev

Reputation: 3704

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.

Upvotes: 1

Related Questions