我可以更改另一个域类的属性值吗? - 圣杯
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更改不同类中域类的属性是可以的。
但是,您实际上并不需要
NursePatient
类。如果您将护士和患者之间的关系声明为多对多,如下所示:那么 Grails 将自动创建并更新所需的联接表。然后,您可以使用 Criteria API 查询没有患者的所有护士:
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:then Grails will create and update the needed join table automatically. You can then query for all the nurses without patients using Criteria API: