Q:

can i change the value of a property from another domain class? - grails

I'm a newbie in grails. i'm having a problem right now in my domain classes. I have 3 domain classes, class Patient,class Nurse and class NursePatient, the class NursePatient is a composite key where you can see who is the attending Nurse in a Patient, so if you view its table you can only see the id's of nurses and patients. This is my code for  Nurse class: 
class Nurse {

String name
Nurse partner
boolean idle = true

static belongsTo = [hospital: Hospital]

static constraints = {

    name(blank:false)
    partner(nullable:true)
    hospital(nullable:false)

}

String toString(){

    "Nurse ${name}"

}

}

--> and this is my domain class for NursePatient: 
class NursePatient implements Serializable{

Nurse nurse
Patient patient

static mapping = {

    version false
    id composite:['nurse', 'patient']
}

static constraints = {
    patient(nullable:false, validator:{val, obj -> val.hospital == obj.nurse.hospital})
    nurse(nullable:false)
}

String toString(){

    "Nurse ${nurse.name} - ${patient.name}"

}

void saveIt(Nurse x, Patient y){

    def np = new NursePatient(nurse: x, patient: y)

    if(np.save()){

       def n = nurse.get(nurse.id)
       n.idle = false

    }

}

}

--> I was asked to print a list of nurses who doesn't have a patient. I was thinking that the moment I save in table using the saveIt() method from class NursePatient, once the save() is successful it changes the value of the property idle of class Nurse from true to false so that querying is much more easier. My problem is I don't if my code in class NursePatient is correct or is it possible to change the value of a property from another class. Please Help me.. thank You!!

A:

Changing properties of domain classes inside different classes is fine.
However, you don't really need a NursePatient class. If you declare the relationship between Nurses and Patients as many-to-many, like this:
class Nurse {
    static hasMany = [patients: Patient]
    ...
}

class Patient {
    static hasMany = [nurses: Nurse]
    ...
}

then Grails will create and update the needed join table automatically. You can then query for all the nurses without patients using Criteria API:
def nursesWithoutPatients = Nurse.withCriteria { isEmpty("patients") }

