# Grails 1.1 表名和列名 - 一对多映射

**URL:** <https://www.isharkfly.com/t/grails-1-1/6630>\
**Category:** 程序语言\
**Created:** [November 4, 2012, 3:37pm UTC](https://www.isharkfly.com/t/grails-1-1/6630 "2012-11-04T15:37:35Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![honeymoose](https://www.isharkfly.com/user_avatar/www.isharkfly.com/honeymoose/32/20796_2.png) [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Post date:** [November 4, 2012, 3:37pm UTC](https://www.isharkfly.com/t/grails-1-1/6630/1 "2012-11-04T15:37:35Z")

</div>

在一个双向的一对多关系中，你可以象前节中的一对一关系中那样改变外键列，只需要在多的一端中改变列名即可。

然而，在单向关联中，外键需要在关联自身中（即一的一端）指定。

比如，给定一个单向一对多联系 Person 和 Address 下面的代码会改变address 表中外键:`class Person {
	String firstName
	static hasMany = [addresses:Address]
	static mapping = {
		table 'people'
		firstName column:'First_Name'
		addresses column:'Person_Address_Id'
	}
}`如果你不想在 address 表中有这个列,可以通过中间关联表来完成，只需要使用 joinTable 参数即可:`class Person {
	String firstName
	static hasMany = [addresses:Address]
	static mapping = {
		table 'people'
		firstName column:'First_Name'
	addresses joinTable:[name:'Person_Addresses', key:'Person_Id', column:'Address_Id']
	}
}`
