Use of ENUMs in Grails Domain Class

给你一囗甜甜゛ 提交于 2019-12-08 19:23:35

问题


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?


回答1:


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

src/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's the _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 feature was transformed into a plugin, so you need to include the following in your BuildConfig.groovy:

compile ":scaffolding:2.0.1"



回答2:


Since GORM 6.1 IdentityEnumType handling changed. In order to store enum by id and not by name or ordial use

static mapping = {
  myEnum enumType:"identity"
}


来源:https://stackoverflow.com/questions/20514575/use-of-enums-in-grails-domain-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!