[英]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.