n92
n92

Reputation: 7592

How to define association relationship in grails

UPDATED

I have a domain classes as below

class Training{
  // has one createdBy object references User domain and 
  // has one course object references Course domain
  // has One Trainer1 and Trainer2 objects refernces Trainer Object.
}

class Trainer{
   String name
  //can have many trainings.
  //If Trainer gets deleted, the trainings of him must be deleted
}
Class User{
  String name
  // can have many trainings. 
}

class Course{
  String name
   //If Course gets deleted, Trainings of this course must be deleted
   // can have many trainings.
}

I have got a training create page, Where I have to populate already saved Course, User, Trainer1 and Trainer2. I am not saving them while Creating the training.

So, How to specify the relationship in grails

Upvotes: 0

Views: 219

Answers (2)

Dopele
Dopele

Reputation: 577

I see some minor flaws in yout initial design: ie why should training be deleted if user is deleted when trainings will obviously tie with many users. Can training exists without trainers or vice versa ?

I would start with something like this:

Class Training {
    static hasMany = [users: User, trainers: Trainer]
    static belongsTo = Course
}

Class Trainer {
    String name
}

Class User {
   String name
}

Class Course {
    String name
    static hasMany = [trainings: Training]   
}

EDIT: I have to agree with Tomasz, you have jumped here too early without searching for answers yourself. Grails.org has good documentation about GORM with examples too.

Upvotes: 1

Tomasz Kalkosiński
Tomasz Kalkosiński

Reputation: 3723

You did not put any effort to searching answer for yourslef. There are plenty basic examples and blog posts how to map relations in Grails. You should start with Grails documentation of GORM - Grails' object relational mapping. You can find it here.

Upvotes: 1

Related Questions