[英]Grails: Connect to legacy database
我想連接到舊數據庫。 我的舊數據庫正在使用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
,操作列表工作正常。 但是,當我單擊數據/用戶名之一(執行顯示操作)時,出現此錯誤:找不到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
為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
不再為null
{"class":"webnico.TableLogin","id":"supervisor","password":"2dcc509a6f7584","userLevel":0,"username":"supervisor"}
但是,表演動作仍然不起作用。 顯示URL似乎是正確的, http ://mylocalhost.com:9000/webnico/tableLogin/show/supervisor但是我仍然遇到相同的錯誤:找不到ID為null的TableLogin
這是否意味着當id
類型不是Long
時,我們不能使用支架( generate-all
)?
啊... show動作無法正常工作,因為默認情況下show動作的參數為Long。 因此,我們需要將參數類型修改為String。
def show(Long id) {
...
}
成為
def show(String id) {
...
}
當然,我們還需要修改其他需要id
參數的操作(編輯,更新,刪除)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.