Using ENUM in a Grails Domain Class

There are a few ENUM examples of Grails (here also in SO), but I cannot get the desired results.

Solutions include 1) Having ENUM in a separate class under src / groovy Domain Class

class Offer { PaymentMethod acceptedPaymentMethod .. } 

src / groovy PaymentMethod

 public enum PaymentMethod { BYBANKTRANSFERINADVANCE('BANKADVANCE'), BYINVOICE('ByInvoice'), CASH('Cash'), CHECKINADVANCE('CheckInAdvance'), PAYPAL('PayPal'), String id PaymentMethod(String id) { this.id = id } } 

In this case, the Enum class is not recognized at all in the domain class that throws an error. It looks like this was used to work with Grails prior to version 2.

Am I missing something? How to use an external ENUM class in a domain in Grails?

2) Put ENUM in the domain class.

In this case, grails does not complain at compile time, but scaffolds do not contain any ENUM value information (this is similar to the acceptPaymentMethod property, which is not included in the forest creation process at all) Example:

 class Offer { PaymentMethod acceptedPaymentMethod .. enum PaymentMethod { BYBANKTRANSFERINADVANCE('BANKADVANCE'), BYINVOICE('ByInvoice'), CASH('Cash'), CHECKINADVANCE('CheckInAdvance'), PAYPAL('PayPal'), String id PaymentMethod(String id) { this.id = id } } } 

Checking the structure of the database table, the field is not ENUM, but simply VarChar:

 | accepted_payment_method | varchar(255) | YES | | NULL | | 

Is there any support for ENUM on Grails Gorm in general?

+7
enums grails gorm
source share
2 answers

Just tried with Grails 2.3.4 and worked with the src / groovy approach:

SIC / groovy / PaymentMethod.groovy

 public enum PaymentMethod { BYBANKTRANSFERINADVANCE('BANKADVANCE'), BYINVOICE('ByInvoice'), CASH('Cash'), CHECKINADVANCE('CheckInAdvance'), PAYPAL('PayPal'), String id PaymentMethod(String id) { this.id = id } } 

Grails app / domain / CustomDomain.groovy

 class CustomDomain { PaymentMethod acceptedPaymentMethod } 

Then I ran grails generate-all CustomDomain , and here _form.gsp it generated:

 <div class="fieldcontain ${hasErrors(bean: customDomain, field: 'acceptedPaymentMethod', 'error')} required"> <label for="acceptedPaymentMethod"> <g:message code="customDomain.acceptedPaymentMethod.label" default="Accepted Payment Method" /> <span class="required-indicator">*</span> </label> <g:select name="acceptedPaymentMethod" from="${custombinds.PaymentMethod?.values()}" keys="${custombinds.PaymentMethod.values()*.name()}" required="" value="${customDomain?.acceptedPaymentMethod?.name()}"/> </div> 

Note that in Grails 2.3.x, the scaffold function was converted to a plugin, so you need to include the following in BuildConfig.groovy :

 compile ":scaffolding:2.0.1" 
+4
source share

Since the handling of GORM 6.1 IdentityEnumType has been changed. To keep enumeration by id, not by name or order of use

 static mapping = { myEnum enumType:"identity" } 
+1
source share

All Articles