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

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'

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值