jks
jks

Reputation: 1

Grails: Multiple Relationships between same two Objects

How do I define a relationship like the following: A person can belong to many projects. A person can be the technical contact for a project or, they can be the business contact for a project or they can be both. If the person gets deleted the project doesn't get deleted. If a project gets deleted the person doesn't get deleted.

class Project { 
  String name
  Person technicalContact
  Person businessContact

  static constraints = {
  }
}

class Person {
  String firstName 
  String lastName
  String email
  String phone
  String department

  static constraints = {
  }
}

Upvotes: 0

Views: 33

Answers (1)

Angga
Angga

Reputation: 2323

You can have 2 one-to-many in one table like this

class Project { 
  String name
}

class Person {
  String firstName 
  String lastName
  String email
  String phone
  String department

  static hasMany = [technicalContactForProjects: Project ,
                  businessContactForProjects: Project 
]
}

Grails will automatically make 2 relation table from that 2 hasMany, so you can delete its relation without delete the actual person or project.

Upvotes: 1

Related Questions