Search code examples
grailsgroovygrails-ormgrails-domain-classgrails-3.0

Grails 3 "MissingMethodException" error on domain class save() method (and other methods)


Domain Class:

package com.myapp.gorm.log

import com.myapp.gorm.security.User
import com.myapp.gorm.system.TransactionJournal

class LogJournal {

    static constraints = {
      message nullable: false
      category nullable: false
      user1 nullable: false
    }

    static mapping = {
      dateCreated column: "ts"
      lastUpdated column: "ts_update"
    }

LogMessage        message
LogCategory       category
User               user1
User               user2
String             value
TransactionJournal transaction

Date dateCreated
Date lastUpdated


boolean equals(o) {
    if (this.is(o)) return true
    if (getClass() != o.class) return false

    LogJournal that = (LogJournal) o

    if (category != that.category) return false
    if (dateCreated != that.dateCreated) return false
    if (id != that.id) return false
    if (lastUpdated != that.lastUpdated) return false
    if (message != that.message) return false
    if (transaction != that.transaction) return false
    if (user1 != that.user1) return false
    if (user2 != that.user2) return false
    if (value != that.value) return false

    return true
}

int hashCode() {
    int result
    result = message.hashCode()
    result = 31 * result + category.hashCode()
    result = 31 * result + user1.hashCode()
    result = 31 * result + (user2 != null ? user2.hashCode() : 0)
    result = 31 * result + (value != null ? value.hashCode() : 0)
    result = 31 * result + (transaction != null ? transaction.hashCode() : 0)
    result = 31 * result + dateCreated.hashCode()
    result = 31 * result + (lastUpdated != null ? lastUpdated.hashCode() : 0)
    result = 31 * result + id.hashCode()
    return result
  }

}

Error:

groovy.lang.MissingMethodException: No signature of method:     c       com.domainboost.gorm.log.LogJournal.save() is applicable for argument types: ()     values: []
Possible solutions: save(), save(boolean), save(java.util.Map), wait(), last(), any()

Or other domain classes work normally, only this one has a problem. I see grails cannot perform any method on this class instance.

There are no any validation errors.

Where I call this domain class (it's logback: appender):

class SystemAppender extends AppenderBase<ILoggingEvent>  {

   static appInitialized = false

  @Override
  public void append(ILoggingEvent event) {
    if (appInitialized) {
        LogMessage logMessage = LogMessage.findByMessage(event.getMessage())
        if (!logMessage) {
            logMessage = new LogMessage()
            logMessage.message = event.getMessage()
            logMessage.save(flash: true)
        }
        String categoryStr = event.argumentArray[0]
        LogCategory logCategory = LogCategory.findByCategory(categoryStr)
        if (!logCategory) {
            logCategory = new LogCategory()
            logCategory.category = categoryStr
            logCategory.save(flash: true)
        }
        User user1 = null
        if (event.argumentArray.contains(1)) {
            user1 = event.argumentArray[1]
        }
        User user2 = null
        if (event.argumentArray.contains(2)) {
            user1 = event.argumentArray[2]
        }
        TransactionJournal tj = null
        if (event.argumentArray.contains(3)) {
            tj = event.argumentArray[3]
        }
        LogJournal logJournal = new LogJournal()
        logJournal.category = logCategory
        logJournal.message = logMessage
        logJournal.user1 = user1
        logJournal.user2 = user2
        logJournal.transaction = tj
        LogJournal.save()

      }
  }
}

And how I test this in grails console

import org.slf4j.Logger
import org.slf4j.LoggerFactory

def Logger logger = LoggerFactory.getLogger("sysLog")
logger.info("message", "category")

Error is on LogJournal.save()

The code which ended with an error is in GormStaticApi.groovy

@CompileStatic(TypeCheckingMode.SKIP)
    def methodMissing(String methodName, Object args) {
    FinderMethod method = gormDynamicFinders.find { FinderMethod f ->   f.isMethodMatch(methodName) }
    if (!method) {
        throw new MissingMethodException(methodName, persistentClass, args)
    }

So it seems that method "save" is not found.. WTF ?


Solution

  • This

    LogJournal.save()
    

    Is looking for a static method on the class. You want a lower case initial letter to call save on the instance variable:

    logJournal.save()