Grails Enum Mapping

后端 未结 4 1117
轮回少年
轮回少年 2020-12-24 15:13

in Grails, Is there a way to limit the size of the column to which the enum is mapped. In the following example, i would like the column type to be char(2)

e         


        
4条回答
  •  情深已故
    2020-12-24 15:28

    I don't think it's directly possible given the way enums are mapped internally in GORM. But changing the code to this works:

    enum FooStatus {
       BAR('br'),
       TAR('tr')
       private FooStatus(String id) { this.id = id }
       final String id
    
       static FooStatus byId(String id) {
          values().find { it.id == id }
       }
    }
    

    and

    class Foo {
       String status
    
       FooStatus getFooStatus() { status ? FooStatus.byId(status) : null }
       void setFooStatus(FooStatus fooStatus) { status = fooStatus.id }
    
       static transients = ['fooStatus']
    
       static constraints = {
          status inList: FooStatus.values()*.id
       }
    
       static mapping = {
          status sqlType: 'char(2)'
       }
    }
    

    Adding the transient getter and setter allows you to set or get either the String (id) or enum value.

提交回复
热议问题