How do you persist a collection of Enums in Grails?

grails, grails-orm, hibernate

Solution

So the easy fix was to just change the domain class to not use the MyEnum enum type for the myenums variable. Instead I changed it to a String and everything started working.

class Tester {
  static hasMany = [myenums:String]
  static constraints = {
  }
}

Upon further reflection, there really was no need for me to persist the enum type at all. I just wanted the value of the that type saved.

Problem

Any ideas on how to persist a collection of enums in Grails? Groovy enum: ``` public enum MyEnum { AAA('Aaa'), BEE('Bee'), CEE('Cee') String description MyEnum(String description) { this.description = description } static belongsTo = [tester:Tester] } ``` I want to use this enum in a Grails domain class. The domain class looks like this: ``` class Tester { static hasMany = [myenums: MyEnum] static constraints = { } } ``` In my create.jsp, I want to be able to select multiple MyEnums and have the following line: ``` <g:select from="${MyEnum?.values()}" multiple="multiple" value="${testerInstance?.myenums}" name="myenums" ></g:select>` ``` The problem I'm getting is when I try to create a new Tester, I get a 500 error saying: ``` Exception Message: java.lang.String cannot be cast to java.lang.Enum Caused by: java.lang.String cannot be cast to java.lang.Enum Class: TesterController ```

Original source