Grails域类中的OneToMany关联

时间:2016-09-27 15:03:54

标签: grails gorm

我希望PersonAddress域类之间的一对多关联更像是:

create table person (id bigint),
               version bigint not null,
               primary key (id))

create table address (id bigint),
               person_id bigint not null,
               primary key (id))

我尝试在这两个表之间建立oneToMany关系,以便one人可以拥有n个地址。

class Person{

static hasMany = [
        addresses: Address
]

}


class Address{

Address address

}

这就是我认为它应该如何工作,我尝试了其他几个选项,但没有一个按预期工作。要么在启动应用程序时遇到交叉错误或错误。

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

您需要在one-to-many Domain class&之间建立Person关系。 Address如下所示 -

Person域类

class Person{

static hasMany = [addresses: Address]
}

Address域类

class Address{

static belongsTo = [person : Person]
}

请浏览grails documentation for gorm以了解相关信息 域类关系的深度。