If only your class members use the enum it is preferable to declare the enum inside the class. It is more intutive for users of the class, it helps the user to know that the enum will only be used by the class.
A domain class fulfills the M in the Model View Controller (MVC) pattern and represents a persistent entity that is mapped onto an underlying database table. In Grails a domain is a class that lives in the grails-app/domain directory.
An enum is an attribute where the values map to integers in the database and can be queried by name. Enums also give us the ability to change the state of the data very quickly. This makes it easy to use enums in Rails and saves a lot of time by providing dynamic methods.
There are a few examples of ENUM on Grails (here in SO as well), but I am not being able to get the desired results.
Solutions include 1) By having the ENUM in a separate class under the 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 at the domain class issuing an error. Looked like this used to work for Grails prior to version 2.
Am I missing something here? How to use external ENUM class in a domain in Grails?
2) Place ENUM within the domain class.
In this case the grails does not complain while compiling, but the scaffolding does not include any info the ENUM values (it is like the property acceptedPaymentMethod is not included at all in the scaffolding process) 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 DB Table, the field is not an ENUM but a simple VarChar:
| accepted_payment_method | varchar(255) | YES | | NULL | |
Is there support for ENUMs on Grails Gorm at all?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With