【问题标题】:Grails: map mysql field of type enum to domain classGrails:将枚举类型的mysql字段映射到域类
【发布时间】:2012-05-17 09:52:49
【问题描述】:

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

谢谢,感谢您的帮助。

【问题讨论】:

    标签: mysql grails grails-orm grails-domain-class


    【解决方案1】:

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

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

    收件人:

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

    【讨论】:

    • 在文档中没有看到这个。已经尝试了3个多小时。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2018-08-01
    • 2019-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-09
    相关资源
    最近更新 更多