Grails映射域类自定义列名

时间:2016-02-09 13:41:36

标签: grails gorm

在我的Grails项目中,我连接到现有的数据库,我有以下结构

class Country {
  Integer country
  String countryCode
  String label
}

class Address {
  String countryCode
  ....
}

数据库表:

国家/地区表

id,version,country,country_code,label

地址表

id,version,country_code,...

我希望有类似的东西:

class Address {
  Country country
}

但它似乎会在地址表中自动查找名为country_id的列,我已尝试使用

static mapping = {
    country column: 'country'
}

但我仍然有相同的结果。

我知道最好将它链接到country.id但数据库已存在,我无法修改它。

有人可以提供帮助吗?

2 个答案:

答案 0 :(得分:1)

如果您使用一对多关系,则不需要声明的属性。 在class Country下,您可以添加

static hasMany = [addresses: Address]

并添加

static belongsTo = [country: Country]

您不需要添加Country country字段。

然后,您可以在视图中使用addressInstance.country.countryCode来显示数据。

答案 1 :(得分:0)

结帐this并添加到域类a { color: #222; position: relative; display: inline-block; } a:hover { color: red; background-color: #222; } a::before { content: 'ST'; text-align: right; position: absolute; width: 100%; } a:hover::before { color: #fff; }

Country
相关问题