[英]Grails unidirectional Many-to-Many
我具有Java / JavaEE背景,最近開始了Grails項目。
當我編寫域模型時,有一個要求:單向多對多。 我知道在Jpa / Hibernate中應該如何。 我在grails中對此進行了快速搜索,似乎Grails不支持它,並且有人建議使用映射類(沒有示例應該怎么做)。
任何人都可以舉一個例子,或者我應該怎么做?
例如:
class Teacher {
static hasMany: [students: Student]
}
class Student {
static belongsTo: Teacher
static hasMany: [teachers: Teacher]
}
以上代碼是在Grails文檔中使用雙向多對多完成的。 單向多對多的代碼是什么?
此外,只有學生可以有教師參考,在教師課堂中,它無法獲取學生列表。
謝謝。
我已經使用了三個班級,如老師,學生和中級學生stuteach。從學生那里我們可以訪問老師的參考。在老師方面,我們不能訪問學生。
class Teacher {
String name
}
class Student {
String name
static hasMany = [teachers:StuTeach]
}
class StuTeach {
static constraints = {
}
static belongsTo = [teacher:Teacher,student:Student]
}
在引導程序中
def t1=new Teacher(name:"t1")
t1.save(flush:true)
def t2=new Teacher(name:"t2")
t2.save(flush:true)
def stu=new Student(name:"s1")
stu.save(flush:true)
def stuteach1=new StuTeach()
stuteach1.student=stu
stuteach1.teacher=t1
stuteach1.save(flush:true)
def stuteach2=new StuTeach()
stuteach2.student=stu
stuteach2.teacher=t2
stuteach2.save(flush:true)
stu.addToTeachers(stuteach1)
stu.addToTeachers(stuteach2)
stu.save(flush:true)
println stu.teachers.teacher
休眠支持單向多對多關系,Grails可能支持這種關系:
http://blog.exensio.de/2013/12/unidirectional-many-to-many.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.