Grails 映射域 class 自定义列名

Grails map domain class custom column name

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

class Country {
  Integer country
  String countryCode
  String label
}

class Address {
  String countryCode
  ....
}

DB tables:

国家table

id、版本、国家、country_code、标签

地址table

id, 版本, country_code, ...

我想要这样的东西:

class Address {
  Country country
}

但它似乎在地址 table 中自动寻找名为 country_id 的列,我试过使用

static mapping = {
    country column: 'country'
}

但我仍然得到相同的结果。

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

有人可以帮忙吗?

签出 this 并添加到域 class Country:

static mapping = {
     id name: 'country'
}

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

static hasMany = [addresses: Address]

并添加

static belongsTo = [country: Country]

您不需要添加 Country country 字段。

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