Grails:连接到旧数据库

时间:2013-10-16 07:50:40

标签: grails gorm

我想连接到旧数据库。我的遗留数据库使用的是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,动作列表运行正常。但是,当我单击其中一个数据/用户名(执行show动作)时,我收到此错误:找不到ID为null的TableLogin

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

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

我得到了:

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

为什么id为空? 我认为因为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"}

但是,节目动作仍然不起作用。 show URL似乎是正确的,http://mylocalhost.com:9000/webnico/tableLogin/show/supervisor但是我仍然遇到了同样的错误:找不到TableLogin,id为null

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

1 个答案:

答案 0 :(得分:0)

Aha ... show动作无法正常运行,因为默认情况下show动作的参数为Long。所以,我们需要修改参数类型成为String。

    def show(Long id) {
...
    }

成为

    def show(String id) {
...
    }

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

相关问题