将主键ID更改为Grails中的字符串类型 [英] Changing primary key id to String type in Grails

查看:54
本文介绍了将主键ID更改为Grails中的字符串类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class User {

static}我的Grails 2.0应用程序定义了一个用户域对象: mapping = {
tabledt_user
列{
id列:'user_id',发生器:'assigned',类型:'string'
}
}

当我尝试在我的BootStrap文件中保存新用户时,如下所示:

  def user = new User(id:smith)。save(failOnError:true)

我得到以下错误:

  |错误2012-01-13 10:09:42,659 [main]错误property.BasicPropertyAccessor  - 类中的IllegalArgumentException:用户,setter方法属性:id 
|错误2012-01-13 10:09:42,660 [main]错误property.BasicPropertyAccessor - 预期类型:java.lang.Long,实际值:java.lang.String

我也尝试将User类更改为:

  class用户{
静态映射= {
表dt_user
列{
id列:'user_id',发生器:'已分配',类型:'字符串',名称:' id'
}
}

字符串ID
}

导致上述错误消失。然而,我发现这导致了id自动生成,完全忽略了生成器:'assigned'子句。



我在这里做错了什么?

解决方案

看起来像将它包装在列中

  class User {

字符串id

static mapping = {
tabledt_user
id列:'user_id',generator:'assigned'
}
}


$ b

只要该字段被声明为一个字符串并且它被配置为赋值为它将工作;没有必要告诉GORM这是一个字符串,它可以弄清楚。


My Grails 2.0 app has a User domain object defined:

class User {

static mapping = {
    table "dt_user"
    columns {
      id column:'user_id', generator:'assigned', type:'string'
    }
}

When I try to save a new User in my BootStrap file like so:

def user = new User(id: "smith").save(failOnError:true)

I get the following error:

| Error 2012-01-13 10:09:42,659 [main] ERROR property.BasicPropertyAccessor  - IllegalArgumentException in class: User, setter method of property: id
| Error 2012-01-13 10:09:42,660 [main] ERROR property.BasicPropertyAccessor  - expected type: java.lang.Long, actual value: java.lang.String

I also tried changing the User class to this:

class User {
    static mapping = {
        table "dt_user"
        columns {
            id column:'user_id', generator:'assigned', type:'string', name:'id'
        }
    }

    String id 
}

which made the above errors go away. However I found that this resulted in ids being generated automatically, completely ignoring the generator: 'assigned' clause.

What am I doing wrong here?

解决方案

Looks like wrapping it in the columns block is the culprit. This may have been required at some point (before my time) but it's been optional as long as I've used Grails and apparently is now broken. But you can just declare column mappings directly:

class User {

   String id

   static mapping = {
      table "dt_user"
      id column: 'user_id', generator: 'assigned'
   }
}

As long as the field is declared as a String and it's configured as assigned it will work; there's no need to tell GORM it's a String, it can figure that out.

这篇关于将主键ID更改为Grails中的字符串类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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