繁体   English   中英

Grails映射域类自定义列名

[英]Grails map domain class custom column name

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

class Country {
  Integer country
  String countryCode
  String label
}

class Address {
  String countryCode
  ....
}

数据库表:

国家表

ID,版本,国家/地区,国家/地区代码,标签

地址表

ID,版本,国家/地区代码,...

我想要类似的东西:

class Address {
  Country country
}

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

static mapping = {
    country column: 'country'
}

但是我仍然有相同的结果。

我知道将其链接到country.id会更好,但是数据库已存在,因此我无法对其进行修改。

有人可以帮忙吗?

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

static hasMany = [addresses: Address]

并添加

static belongsTo = [country: Country]

您无需添加Country country字段。

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

看看这个 ,并加入到域类Country

static mapping = {
     id name: 'country'
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM