Mapping the grails/groovy enum to Mysql enum -


here environment

grails version: 3.1.6 groovy version: 2.4.6 jvm version: 1.8.0_51 

the hibernate version in build.gradle

compile "org.hibernate:hibernate-core:5.1.1.final" compile "org.hibernate:hibernate-ehcache:5.1.1.final" 

i expected map enum mysql this:

| field       | type                | null | key | default |         | | logtype     | enum('debug','info'| no   |     | null    |                | 

but didn't work. googled suggestions didn't them working.

this code:

 package xxxx  class template {      enum logtype {         debug("debug"), info("info")         string name         logtype(name){             this.name = name         }     }      integer tplid     logtype lt      static constraints = {         tplid(nullable: true)         lt(nullable: true)     }       static mapping = {         table "template"         id name: "tplid"         tplid column: "tplid", comment: "id"         lt sqltype: "enum", column: "lt", enumtype: "debug"     } } 

i read docs here http://docs.grails.org/3.1.1/ref/database%20mapping/column.html

and not sure usage of enumtype correct, can work?

updated, following code still doesn't work

class template {      enum logtype {         debug("debug"), info("info")         string name          logtype(name) {             this.name = name         }     }      logtype lt      static constraints = {     }      static mapping = {         table "template"         lt sqltype: "enum", enumtype: 'string'     } } 

first question- have explained in docs:

enumtype (optional) - enum type in type-safe enum properties. either ordinal or string. link

that means can use either:

  • enumtype: 'ordinal' store enum using ordinal number

note: ordinal number position in enum declaration, initial constant assigned ordinal of zero. link

  • or enumtype: 'string' store enum enum name

...

to other problems i've noticed in code:

  • you don't need create own name property enum logtype, enums has one, equal declaration name.

  • you don't need explicitly define table/column names if same class/field names.

  • your table uses nullable id

edit:

so edited code comments can improved/removed

class template {      //no need define own name field, remove own implementation     enum logtype {         debug("debug"), info("info")         string name           logtype(name) {             this.name = name         }     }      logtype lt      static constraints = {     }      static mapping = {         table "template" //name can infered domain class name         lt sqltype: "enum", enumtype: 'string'          // sqltype infered variable         // there no 'enum' sql type (i think inherited hibernate)         // enumtype: 'string' default       } } 

existing hibernate types

and can class mentioned changes:

class template {      enum logtype {         debug, info     }      logtype lt }  

edit2:

i not recommend this, can configure underlaying sql type (not tested should work):

static mapping = {     lt sqltype: 'enum(debug, info)' } 

take note application support mysql databases.


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -