【问题标题】:Domain to Domain inheritance域到域继承
【发布时间】:2019-01-18 11:54:54
【问题描述】:

我无法将属性添加到约束,也无法映射到已扩展我新创建的域的域。

class Person1 {
    String name

    static constraints = { name nullable : true }

    static mapping = { 
        table  'PERSON'
        name column : 'PERSON_NAME' 
    }
}

class Person2 extends Person1 {
    String address

    static constraints = { address nullable : true }

    static mapping = { 
        address column : 'PERSON_ADD' 
    }
}

知道如何正确执行此操作吗?

我遇到了一个错误

消息:ORA-00904:“THIS_”。“CLASS”:标识符无效

【问题讨论】:

  • name address : 'PERSON_ADD' mapping 块内无效。我不知道你的意图是什么,但那是无效的。
  • 将Person2中的约束改为address column: 'PERSON_ADD'
  • 我的意思是改变映射。
  • 大家好,抱歉打错了。它的address column : 'PERSON_ADD' 。我更新了我的问题。但我仍然遇到同样的错误。无论如何,我通过复制 Person 1 中的所有属性并将其放入 Person 2 来解决此问题。只是让您知道我的真实世界问题是 Person1 来自插件,我想添加一个新属性,但我不能编辑它,所以我认为扩展它是可能的。我只关心代码的可重用性。

标签: inheritance grails grails-orm grails-2.5


【解决方案1】:

改用 Groovy Traits:

http://docs.groovy-lang.org/next/html/documentation/core-traits.html

trait Person1 {
   String name

   static constraints = { name nullable : true }

   static mapping = { 
       table  'PERSON'
       name column : 'PERSON_NAME' 
   }
}

class Person2 implements Person1 {
    String address

    static constraints = { address nullable : true }

    static mapping = { 
        name address : 'PERSON_ADD' 
    }
}

【讨论】:

  • 对不起,我没有提到 Person1 来自插件。所以使它成为一个特征是不可能的,因为我不能编辑它。无论如何,谢谢你的回复。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多