2013-10-16 65 views
0

我想连接到遗留数据库。我的遗留数据库正在使用postgresql。我使用DB-逆向工程插件生成域类,生成的域类:Grails:连接到遗留数据库

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动作),我得到这个错误:TableLogin不能与ID为空

发现我尝试使用此代码跟踪:

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

我得到了:

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

为什么id为空? 我想是因为id为空,因此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"} 

但是,演出动作仍然无效。展会网址似乎是正确的,http://mylocalhost.com:9000/webnico/tableLogin/show/supervisor但我仍然得到了同样的错误:TableLogin不能与ID为空发现

是不是就意味着当id类型不是Long我们不能使用支架(generate-all)?

回答

0

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

def show(Long id) { 
... 
    } 

成为

def show(String id) { 
... 
    } 

和关闭过程中,我们需要修改的其他操作(编辑,更新,删除)的需要id参数太多。