我可以更改另一个域类的属性值吗? - 圣杯

发布于 2024-12-26 07:33:50 字数 1459 浏览 2 评论 0原文

我是 grails 的新手。我的域类现在遇到问题。我有3个域类,类Patient,类Nurse和类NursePatient,类NursePatient是一个复合键您可以在其中看到谁是患者的主治护士,因此如果您查看其表,您只能看到护士和患者的 ID。这是我的护士课程代码:

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}"

}


}

-->这是我的 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

    }


}

}

-->我被要求打印一份没有病人的护士名单。我在想,当我使用 NursePatient 类中的 saveIt() 方法保存在表中时,一旦 save() 成功,它就会发生变化将Nurse类的属性idle的值从true改为false,这样查询起来就容易多了。我的问题是,我不知道 NursePatient 类中的代码是否正确,或者是否可以更改另一个类的属性值。请帮助我..谢谢!!

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!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千纸鹤带着心事 2025-01-02 07:33:50

更改不同类中域类的属性是可以的。

但是,您实际上并不需要 NursePatient 类。如果您将护士和患者之间的关系声明为多对多,如下所示:

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

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

那么 Grails 将自动创建并更新所需的联接表。然后,您可以使用 Criteria API 查询没有患者的所有护士:

def nursesWithoutPatients = Nurse.withCriteria { isEmpty("patients") }

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") }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文