【问题标题】:Grails addTo* with result of database带有数据库结果的 Grails addTo*
【发布时间】:2013-07-10 20:50:21
【问题描述】:

为什么这段代码不能正常工作?

def classeInstrumento = classeInstrumentoService.getClasseInstrumento("value")

def instrumentoInstance = new Instrumento().addToClasseInstrumento(classeInstrumento)

我在控制台上收到此错误消息:

No signature of method: package.Instrumento.addToClasseInstrumento() is applicable for argument types: (package.ClasseInstrumento) values: [package.ClasseInstrumento : 5]

这就是域结构

class ClasseInstrumento {
    static hasMany = instrumentos: Instrumento
}

class Instrumento {

    ClasseInstrumento idClasseInstrumento

    static hasMany =  [ativoDefs: AtivoDef,
                      futuroDefs: FuturoDef,
                      operacaoDefs: OperacaoDef]

    static belongsTo = [ClasseInstrumento]
}

所以我预计它会起作用,但它没有:(

感谢您的回复!

【问题讨论】:

    标签: grails grails-orm relationship


    【解决方案1】:

    Instrumento 属于ClasseInstrumento

    这意味着ClasseInstrumento 是父级,InstrumentoClasseInstrumento 的子级(由ClasseInstrumento 中的hasMany 表示)

    addTo* 用于从父母到孩子,这意味着

    将父级添加为子级的foreign_key引用”,这意味着

    classeInstrumento.addToInstrumentos(new Instrumento())

    会起作用,而不是您使用的前一种方法。

    【讨论】:

      【解决方案2】:
      instrumentoInstance = new Instrumento().addToClasseInstrumento(classeInstrumento)
      
      classeInstrumento = classeInstrumentoService.getClasseInstrumento("value")
      
      classeInstrumento .addToInstrumentos(instrumentoInstance) 
      

      这只是可能的,或者如果您错过了域要求,您应该更改您属于反之亦然。

      【讨论】:

        猜你喜欢
        • 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
        相关资源
        最近更新 更多