简体   繁体   中英

Groovy/Grails - Error at run-app with Enums

I'm facing this error

groovy.lang.GroovyRuntimeException: Could not find matching constructor for: AnimalInfoType(java.lang.String, java.lang.Integer, java.lang.Integer) when

starting the app via "run-app" and I've created the constructor for Enum, as seen below:

    package jogoanimais

    public enum AnimalInfoType
    {
      ANIMAL(1), 
      ACTION(2)

        final int value
        private AnimalInfoType(int value) {
          this.value = value
        }

        int value() { value }    
    }

My domain class is like that:

    class AnimaisTreeMap {

       String nodeDescription
       AnimalInfoType nodeInfo
       AnimaisTreeMap yesAnswerNode
       AnimaisTreeMap noAnswerNode

        static constraints = {
            yesAnswerNode nullable:true
            noAnswerNode nullable:true        

        }
        static mappedBy = [ yesAnswerNode: "none", noAnswerNode: "none" ]   
        static mapping = {
            yesAnswerNode cascade: 'delete'
            noAnswerNode cascade: 'delete'
        }
    }

And at my BootStrap.groovy I fill the table like that:

    def noAnswer =  new AnimaisTreeMap(nodeDescription:"macaco", 
                                       nodeInfo: AnimalInfoType.ANIMAL,
                                       noAnswerNode:null, 
                                       yesAnswerNode:null)
    noAnswer.save(failOnError: true)
    def yesAnswer =  new AnimaisTreeMap(nodeDescription:"tubarão", 
                                        nodeInfo: AnimalInfoType.ANIMAL,
                                        noAnswerNode:null, 
                                        yesAnswerNode:null)
    yesAnswer.save(failOnError: true)           

    new AnimaisTreeMap(nodeDescription:"vive na água", 
                       nodeInfo: AnimalInfoType.ACTION,
                       noAnswerNode: noAnswer, 
                       yesAnswerNode:  yesAnswer).
                       save(failOnError: true)                  
}

What am I doing wrong at BootStrap.groovy?

Remove the enum constructor and its public definition. It's not necessary, all classes are public by default.

And edit the class ended by Enum. AnimalInfoTypeEnum.

enum AnimalInfoTypeEnum
{
  ANIMAL(1), 
  ACTION(2)

    final int value

    int value() { value }    
}

Maybe try this:

package jogoanimais

enum AnimalInfoType{
   ANIMAL(1), 
   ACTION(2)

   private final int value

   private AnimalInfoType(int value) {
      this.value = value
   }

   int value() { value }
}

Wouldn't let me comment, don't have enough rep.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM