用來處理Grails Groovy腳本,並希望做一個更新到數據庫中的記錄。我做基本從JSON獲取對象並將其轉換爲Domain類,然後在其上執行save()。從我的理解,因爲我是新手Groovy和grails保存應該更新,如果「ID」已經存在。但我不明白,我得到了關鍵'PRIMARY'的重複輸入'1'的標準SQL錯誤。我該如何解決?JSON聖盃的Groovy更新SQL
def input = request.JSON
def instance = new Recorders(input)
instance.id = input.getAt("id")
instance.save()
和我的域名是:
class Recorders {
Integer sdMode
Integer gsmMode
static mapping = {
id generator: "assigned"
}
static constraints = {
sdMode nullable: true
gsmMode nullable: true
}
}
作爲羅布Hruska的指出,必須從數據庫中獲取對象和更新;不要使用重複的ID創建一個新的。 –