belongsTo multiple Domain

时间:2012-06-18 06:29:39

标签: grails gorm grails-plugin grails-domain-class grails-2.0

我有4个课程,事件,问题和请求,另一个是附件。

每个域看起来都像.........

    Class Incidents
    {
    // other fields
       static hasOne = [attachment: Attachment]

       static constraints = [attachment nullable:true]
    }

    Class Problems
    {
    // other fields
       static hasOne = [attachment: Attachment]

       static constraints = [attachment nullable:true]
    }

    Class Requests
    {
    // other fields
       static hasOne = [attachment: Attachment]

       static constraints = [attachment nullable:true]
    }

    Class Attachment
    {
    // other fields
       static belongsTo= [
                   incident: Incidents, 
                   problem: Problems,
                   requests: Requests
]

   static constraints = {
        incident nullable: true
        problem nullable: true
        requests nullable: true
}

当我保存事件的对象时,它抛出异常,如列'problem_id'不能为null。 该怎么办?

1 个答案:

答案 0 :(得分:5)

尝试删除类别事件,问题,请求上的hasOne并将其替换为

   Attachment attachment
   static constraints = {attachment: unique: true, nullable:true}       
   static mapping = {
    attachment  cascade: "delete"
    }