Grails Enum Mapping

后端 未结 4 1098
轮回少年
轮回少年 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:24

    Grails ships with an undocumented (as far as I can tell anyway) custom Hibernate mapping for enums. The class is org.codehaus.groovy.grails.orm.hibernate.cfg.IdentityEnumType. It won't let you set the column size but does make it easy to change what is stored in the DB for each enum value without having to add transient fields to your model.

    import org.codehaus.groovy.grails.orm.hibernate.cfg.IdentityEnumType
    
    class MyDomainClass {
        Status status
    
        static mapping = {
            status(type: IdentityEnumType)
        }
    
        enum Status {
            FOO("F"), BAR("B")
            String id
            Status(String id) { this.id = id }
        }
    }
    

    You can run an 'alter table' in Bootstrap.groovy to shrink the column:

    DataSource dataSource
    ...
    Sql sql = new Sql(dataSource)
    sql.execute("alter table my_domain_class change column status status varchar(1) not null")
    

提交回复
热议问题