Grails:将类型为enum的mysql字段映射到域类 [英] Grails: map mysql field of type enum to domain class

查看:226
本文介绍了Grails:将类型为enum的mysql字段映射到域类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何将枚举类型的mysql字段映射到grails域类? 我使用现有(遗留) mySQL数据库与Grails v.2.0.3。我遇到错误的列类型错误:

 失败;嵌套异常是org.hibernate.HibernateException:
列中的列类型错误,列log_type为facility.ost_fac_syslog。发现:enum,expected:varchar(255)

SQL字段定义为:

  mysql>描述ost_fac_syslog; 
+ ------------ + -------------------------------- - + ------ + ----- + --------------------
|字段|类型|空| Key |默认
+ ------------ + ------------------------------- - + ------ + ----- + ---------------------- +
| log_id | int(11)unsigned | NO | PRI | NULL auto_increment |
| log_type |枚举('调试','警告','错误')| NO | MUL | NULL | |

我的域名类是:

  class OstFacSyslog {
static mapping = {
table'ost_fac_syslog'
version false
id列:'log_id',名称:'logId'
logType列:'log_type',类型:'enum',名称:'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)类型。更改您的映射:

 静态映射= {
...
logType列:'log_type' ,输入:'enum',name:'logType'
}

p>

 静态映射= {
...
logType列:'log_type',sqlType:'enum',名称:'logType'
}


How can I map a mysql field of type enum to a grails domain class?

I'm using an existing (legacy) mySQL database with grails v.2.0.3. I'm getting an error for Wrong column type:

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

The SQL field is defined as:

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    |                |

My domain class is:

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}
    }
}

Thanks, I appreciate any help.

解决方案

You need to specify the column's sqlType instead of the (Java) type. Change your mapping from:

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

To:

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

这篇关于Grails:将类型为enum的mysql字段映射到域类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆