2011-12-22 52 views
0

嗨外键约束,我想创建一个域类与外键约束到另一个现有的域类 新的域类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) 
+0

您是否尝试过运行'改变直接从错误信息数据库table'声明? – socha23 2011-12-22 10:59:50

+0

嗨,我直接运行alter table语句,它失败了,但发现资源表的id字段是unsigned - 这就是为什么它失败,当我在ar_entitlements表中的resource_id unsigned并运行查询再次它是成功的...现在我想知道是如何使域类ArEntitlement中的resource_id字段直接进行无符号签名,以便我不必手动创建约束。 – 2011-12-23 01:05:55

回答

1

移除ArEntitlement '资源的资源' 行。

static belongsTo = [resource : Resources] 

应该覆盖并定义两次可能是问题。此外,您不需要指定表名称,id生成器或id列,因为它显示为使用GORM默认值。尝试删除这些。

下面是修改后的代码...

package ars 
import gra.Resources 
class ArEntitlement { 

    String entitlementname 
    String entitlementdesc 
    String entitlementcomm 
    Integer departmentid 
    Integer version 

    static belongsTo = [resource : Resources] 

    static mapping = { 
    } 
} 
+0

嗨,我直接运行alter table语句并且失败了,但是发现资源表的id字段是无符号的 - 这就是为什么它失败了,当我在ar_entitlements表中使resource_id无符号并运行查询时再次它是成功的...现在我想知道是如何使域类ArEntitlement中的resource_id字段直接进行无符号签名,以便我不必手动创建约束。 – 2011-12-23 06:00:33

+0

你在运行什么数据库?这听起来像是在这里帮不了你。 – 2011-12-26 17:22:24

+0

嗨,我正在使用MySQL数据库.. – 2011-12-27 17:39:50