嗨外鍵約束,我想創建一個域類與外鍵約束到另一個現有的域類 新的域類ArEntitlement定義如下創建域類在常規
package ars
import gra.Resources
class ArEntitlement {
long id
String entitlementname
String entitlementdesc
String entitlementcomm
Integer departmentid
Resources resource
Integer version
static belongsto =[resource : Resources]
static mapping={
table 'ar_entitlement'
id(generator:'increment')
column{
id(column:'id')
}
}
}
資源領域類是定義如下(它是前創建)
package gra
import com.gra.transaction.UserTransaction
class Resources {
Long id=1
String resourceName
Double resourceType
String resourceOwner
Double riskScore
Integer decommissioned
String resourceClass
Long resourceSegment
String targetIp
String resCriticality
Long resourceGroup
Integer disabled
static hasMany=[userTransactions:UserTransaction]
static mapping = {
table 'resource'
version false
id(generator:'increment')
column{
id(column:'id') }
}
static constraints=
{
resourceName(nullable:true,blank:false)
resourceType(nullable:true,blank:false)
resourceOwner(nullable:true,blank:false)
riskScore(nullable:false,blank:false)
decommissioned(nullable:false,blank:false)
resourceClass(nullable:false,blank:false)
resourceSegment(nullable:false,blank:false)
targetIp(nullable:false,blank:false)
resCriticality(nullable:true,blank:false)
resourceGroup(nullable:false,blank:false)
disabled(nullable:false,blank:false)
}
}
創建的生成的表不必須ar_entitlement表的外鍵映射到資源表,它創建一個名爲「RESOURCE_ID」,但沒有外來k個列ey約束。 我需要它指向資源表的id列
我嘗試了幾個選項,沒有使用hasOne(在兩個域類中)的屬性說明符,但沒有獲得所需的外鍵關係。
任何想法這裏的問題是什麼?
日誌錯誤我得到的是
2011-12-21 19:50:17,258 [main] ERROR hbm2ddl.SchemaUpdate - Unsuccessful: alter table ar_entitlement add index FKDCD6161FEFD54E5E (resourceid_id), add constraint FKDCD6161FEFD54E5E foreign key (resourceid_id) references resource (id)
您是否嘗試過運行'改變直接從錯誤信息數據庫table'聲明? – socha23 2011-12-22 10:59:50
嗨,我直接運行alter table語句,它失敗了,但發現資源表的id字段是unsigned - 這就是爲什麼它失敗,當我在ar_entitlements表中的resource_id unsigned並運行查詢再次它是成功的...現在我想知道是如何使域類ArEntitlement中的resource_id字段直接進行無符號簽名,以便我不必手動創建約束。 – 2011-12-23 01:05:55