【问题标题】:Grails cannot save objectGrails 无法保存对象
【发布时间】:2014-06-12 18:09:01
【问题描述】:

我在尝试将对象保存到我的数据库时遇到了一个相当奇怪的错误。该代码在尝试保存基类对象时运行良好,但现在我正在尝试保存从基域类继承的域类。

这里是父类:

package com.twc.fatcaone

class Record {

long batchID

}

以及我要保存的子类:

package com.twc.fatcaone

class AccountRecord extends Record {

    long uniqueId
    String accountId
    String type
    String insurance
    String currencyType
    float amount
    String upSerDel
    String generalComments

    static mapping = {
        collection "accountRecords"
        database "twcdb"
}
}

我的服务中尝试保存的代码:

accountRecordList.each { 

                def accountRecord = new AccountRecord(batchID: params.selectedBatch.id,
                                                        uniqueId: it.uniqueId,
                                                        accountId: it.accountId,
                                                        type: it.type,
                                                        insurance: it.insurance,
                                                        currencyType: it.currencyType,
                                                        amount: it.amount,
                                                        upSerDel: it.upSerDel,
                                                        generalComments: it.generalComments)

                       if (!AccountRecord.save(flush: true, failOnError: true)) {
                           println "ERROR: Record could not be saved!"
                           def errorValue = AccountRecord.errors
                           println errorValue
                       }

以及我在尝试保存时遇到的错误:

/FatcaOne_0/customer/upload - parameters: dataTypegrp: 1 fileTypegrp: 1 No signature of method: com.twc.fatcaone.AccountRecord.save() is applicable for argument types: (java.util.LinkedHashMap) values: [[flush:true, failOnError:true]] Possible solutions: save(), save(), save(boolean), save(java.util.Map), save(boolean), save(java.util.Map). Stacktrace follows: Message: No signature of method: com.twc.fatcaone.AccountRecord.save() is applicable for argument types: (java.util.LinkedHashMap) values: [[flush:true, failOnError:true]] Possible solutions: save(), save(), save(boolean), save(java.util.Map), save(boolean), save(java.util.Map) Line | Method ->> 91 | methodMissing in org.grails.datastore.gorm.GormStaticApi - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | 32 | call in org.grails.datastore.gorm.internal.StaticMethodInvokingClosure | 65 | doCall . . . . . . . . . . in com.twc.fatcaone.FileImportService$_$tt__excelAccountFileUpload_closure4 | 53 | $tt__excelAccountFileUpload in com.twc.fatcaone.FileImportService | 119 | upload . . . . . . . . . . in com.twc.fatcaone.CustomerController | 198 | doFilter in grails.plugin.cache.web.filter.PageFragmentCachingFilter | 63 | doFilter . . . . . . . . . in grails.plugin.cache.web.filter.AbstractFilter | 1145 | runWorker in java.util.concurrent.ThreadPoolExecutor | 615 | run . . . . . . . . . . . . in java.util.concurrent.ThreadPoolExecutor$Worker ^ 744 | run in java.lang.Thread /FatcaOne_0/customer/upload - parameters: dataTypegrp: 1 fileTypegrp: 1 No signature of method: com.twc.fatcaone.AccountRecord.save() is applicable for argument types: (java.util.LinkedHashMap) values: [[flush:true, failOnError:true]] Possible solutions: save(), save(), save(boolean), save(java.util.Map), save(boolean), save(java.util.Map). Stacktrace follows: Message: No signature of method: com.twc.fatcaone.AccountRecord.save() is applicable for argument types: (java.util.LinkedHashMap) values: [[flush:true, failOnError:true]] Possible solutions: save(), save(), save(boolean), save(java.util.Map), save(boolean), save(java.util.Map) Line | Method ->> 91 | methodMissing in org.grails.datastore.gorm.GormStaticApi - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | 32 | call in org.grails.datastore.gorm.internal.StaticMethodInvokingClosure | 65 | doCall . . . . . . . . . . in com.twc.fatcaone.FileImportService$_$tt__excelAccountFileUpload_closure4 | 53 | $tt__excelAccountFileUpload in com.twc.fatcaone.FileImportService | 119 | upload . . . . . . . . . . in com.twc.fatcaone.CustomerController | 198 | doFilter in grails.plugin.cache.web.filter.PageFragmentCachingFilter | 63 | doFilter . . . . . . . . . in grails.plugin.cache.web.filter.AbstractFilter | 1145 | runWorker in java.util.concurrent.ThreadPoolExecutor | 615 | run . . . . . . . . . . . . in java.util.concurrent.ThreadPoolExecutor$Worker ^ 744 | run in java.lang.Thread

【问题讨论】:

  • 新类还在/domain/目录下吗?

标签: hibernate grails


【解决方案1】:

在调用保存函数之前不应该是 AccountRecord 单词中的小写“a”吗?

【讨论】:

  • 没错。对AccountRecord.save(flush: true, failOnError: true) 的调用被视为AccountRecord 类的静态方法调用。应该在 accountRecord 变量上调用实例方法。
  • 非常感谢。哇,我想我的眼睛盯着代码太久了。休息时间!
猜你喜欢
  • 2012-03-27
  • 2015-01-06
  • 1970-01-01
  • 2013-10-14
  • 1970-01-01
  • 2017-04-03
  • 2014-06-07
  • 2018-04-26
  • 2021-01-28
相关资源
最近更新 更多