【问题标题】:Grails: Connect to legacy databaseGrails:连接到遗留数据库
【发布时间】:2013-10-16 07:50:40
【问题描述】:

我想连接到旧数据库。我的旧数据库正在使用 postgresql。我使用db-reverse-engineer插件生成域类,生成的域类:

class TableLogin {

    String username
    String password
    Integer userLevel

    static mapping = {
        id name: "username", generator: "assigned"
        version false
    }

    static constraints = {
        username maxSize: 30
        password nullable: true, maxSize: 36
        userLevel nullable: true
    }
}

我运行generate-all,操作列表工作正常。但是,当我单击其中一个数据/用户名(执行显示操作)时,出现此错误:TableLogin not found with id null

我尝试使用此代码进行跟踪:

    def rowLogin = TableLogin.get("supervisor")
    log.error(rowLogin as JSON)

我得到了:

{"class":"webnico.TableLogin","id":null,"password":"2dcc509a6f7584","userLevel":0,"username":"supervisor"}

为什么 id 是 null ? 我认为因为 id 为 null 所以 show 操作不起作用


我更新了我的域类,变成:

class TableLogin {

    String id

    String username
    String password
    Integer userLevel

    static mapping = {
        id name: "username", generator: "assigned"
        version false
    }

    static constraints = {
        username maxSize: 30
        password nullable: true, maxSize: 36
        userLevel nullable: true
    }

    def afterLoad() {
        id = username
    }
}

id 不再为空

{"class":"webnico.TableLogin","id":"supervisor","password":"2dcc509a6f7584","userLevel":0,"username":"supervisor"}

但是,显示操作仍然不起作用。显示 URL 似乎是正确的,http://mylocalhost.com:9000/webnico/tableLogin/show/supervisor 但我仍然遇到同样的错误:TableLogin not found with id null

这是否意味着当id类型不是Long时我们不能使用脚手架(generate-all)?

【问题讨论】:

    标签: grails grails-orm


    【解决方案1】:

    啊哈... show action 无法正常工作,因为默认情况下 show action 的参数是 Long。所以,我们需要将参数类型修改为String。

        def show(Long id) {
    ...
        }
    

    成为

        def show(String id) {
    ...
        }
    

    当然,我们还需要修改其他需要id 参数的操作(编辑、更新、删除)。

    【讨论】:

      猜你喜欢
      • 2011-07-21
      • 2019-02-16
      • 2012-09-30
      • 1970-01-01
      • 2016-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-12
      相关资源
      最近更新 更多