How do you save the Enums collection in Grails?

Any ideas on how to maintain a collection of listings in Grails?

Groovy listing:

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 enumeration in a Grails domain class. The domain class is as follows:

 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 that I get is when I try to create a new tester, I get 500 error message:

 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 
+4
source share
2 answers

Thus, a simple solution was to simply change the domain class so as not to use the enumeration type MyEnum for the variable myenums. Instead, I changed it to String , and it all started.

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

Upon further reflection, I really did not have to constantly keep the type of enumeration. I just wanted to keep the value of this type.

+2
source

I have not done hasMany before for listing, but if you give your enums the id property, then hibernate will be able to save it in other ways (it can work with hasMany too). Here is an example that I used in the past:

 class Qux { ... BazType baz ... } enum BazType { FOO('foo'), BAR('bar') final String id BazType(String id) { this.id = id } } 

Providing your enum id property can give hibernate enough information to work. See the Grails 1.1 release notes for more information.

+1
source

Source: https://habr.com/ru/post/1302289/


All Articles