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

时间:2012-05-08 21:19:00

标签: mysql grails gorm grails-domain-class

如何将枚举类型的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}
    }
}

谢谢,我感谢任何帮助。

1 个答案:

答案 0 :(得分:6)

您需要指定列sqlType而不是(Java)type。从以下位置更改映射:

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

要:

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