Grails:将类型枚举的mysql字段映射到域类

如何将枚举类型的mysql字段映射到grails域类?

我正在使用grails v.2.0.3的现有(遗留)mySQL数据库.错误的列类型我收到错误:

failed; nested exception is org.hibernate.HibernateException: Wrong column type in
facilities.ost_fac_syslog for column log_type. Found: enum, expected: varchar(255)

SQL字段定义为:

mysql> describe ost_fac_syslog;
+------------+---------------------------------+------+-----+--------------------
| Field      | Type                            | Null | Key | Default    
+------------+---------------------------------+------+-----+----------------------+
| log_id     | int(11) unsigned                | NO   | PRI | NULL    auto_increment |
| log_type   | enum('Debug','Warning','Error') | NO   | MUL | NULL    |                |

我的域类是:

class OstFacSyslog {
    static mapping = {
       table 'ost_fac_syslog'
       version false
       id column: 'log_id', name:'logId'
       logType column: 'log_type', type: 'enum', name: 'logType'
    }

    Integer logId
    LogType logType

    enum LogType {
        Debug('Debug'), Warning('Warning'), Error('Error')
            private final String toString
        LogType(String toString) {this.toString = toString}
        String getName() {name()}
        String toString() {toString}
    }
}

谢谢,我感谢任何帮助.

解决方法:

您需要指定列的sqlType而不是(Java)类型.从以下位置更改映射:

static mapping = {
    ...
    logType column: 'log_type', type: 'enum', name: 'logType'
}

至:

static mapping = {
    ...
    logType column: 'log_type', sqlType: 'enum', name: 'logType'
}
上一篇:ALPHACAM | Casebuilder By CABINET VISION


下一篇:golang修仙记之gorm(一)