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

标签 mysql grails grails-orm 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}
    }
}

谢谢,我感谢任何帮助。

最佳答案

您需要指定列的 sqlType 而不是 (Java) type。更改您的映射:

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

收件人:

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

关于mysql - Grails:将枚举类型的mysql字段映射到域类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10506659/

相关文章:

mysql - 根据任意列插入或更新

mysql - 在 MySQL 表中存储大量文本?

grails - 域对象中的分组约束

grails - Grails-如果保存时验证为false并且域对象无效,则save()会发生什么?

php - 如何在 MySQL 中存储 bool 数据类型?

带有 gradle 4.x 的 Grails 3.3.x 不起作用?

Grails:一个数据库和一个以上的应用程序

grails - Gorm : how to set datatype of a value in a Map

grails - 如何在 Grails/GORM 中处理空列表?

mysql - Sphinx Search 如何索引/处理 JSON 文件?