【发布时间】:2018-03-26 18:29:51
【问题描述】:
我有两个域类,它们之间的关系是 1 到 Many。我知道如何将每个类的列分别映射到它们各自的表,但是如何映射我的 MSSQL 数据库中存在的关系?没有连接表,我只有只读访问权限。我查看了 Grails 文档的各个页面,这就是我目前所处的位置(一个学生有很多课程)。在我的表中,将两个表联系在一起的外键位于 Courses 表中。
class StudentHeader { //on the one side
String stuNo
String stuName
String stuStreet
static mappedBy = [refs: "fkCustNo"]
static hasMany = [refs: CourseHeader]
static constraints = {
}
static mapping = {
table name: "[tbl_Students]", schema: "[dbo]", catalog: "[CRD].[CourseTrak]"
version false
id generator: 'assigned', name: 'stuNo'
stuNo column: '[PK_StudentNo]'
stuName column: '[Student_Name]'
stuStreet column: '[Student_Street]'
}
}
class CourseHeader { //on the many side
String courId
String courName
StudentHeader fkCourNo
static constraints = {
}
static mapping = {
table name: "[tbl_Courses]", schema: "[dbo]", catalog: "[CRD].[CourseTrak]"
version false
id generator: 'assigned', name: 'courId'
courId column: '[PK_CourseId]'
courName column: '[Course_Name]'
fkCourNo column: '[FK_CourseNo]'
}
}
这里的测试是我尝试访问学生课程的方式
StudentHeader.first().refs
【问题讨论】:
-
你能分享一个关于你期望结果的例子吗?
-
第8行不应该是“static hasMany = [refs: CourseHeader]”吗?
-
@Kloker 你是对的。编辑代码
-
逆向工程插件可能是一个起点。 grails-plugins.github.io/grails-db-reverse-engineer
标签: grails mapping grails-orm one-to-many hibernate-mapping