2011-05-05 11 views
3

我想把我張貼到我的groovy控制器的JSONObject。我可以傳遞該對象,查看JSON數據,然後創建一個Domain Object。當我將它保存寫入數據庫時​​,它會執行Select操作。Grails save()域對象實際上做了一個Select?

def save = { 
    def input = request.JSON 
    def instance = new Customers(input) 
    instance.save() 
} 

這裏是我的調試SQL輸出

Hibernate: 
select 
    this_.customers_id as customers1_237_0_, 
    this_.customers_default_address_id as customers2_237_0_, 
    this_.customers_dob as customers3_237_0_, 
    this_.customers_email_address as customers4_237_0_, 
    this_.customers_email_address2 as customers5_237_0_, 
    this_.customers_fax as customers6_237_0_, 
    this_.customers_firstname as customers7_237_0_, 
    this_.customers_gender as customers8_237_0_, 
    this_.customers_lastname as customers9_237_0_, 
    this_.customers_membertype as customers10_237_0_, 
    this_.customers_memo1 as customers11_237_0_, 
    this_.customers_mname as customers12_237_0_, 
    this_.customers_newsletter as customers13_237_0_, 
    this_.customers_password as customers14_237_0_, 
    this_.customers_point_date as customers15_237_0_, 
    this_.customers_telephone as customers16_237_0_, 
    this_.customers_total_points as customers17_237_0_, 
    this_.customers_username as customers18_237_0_ 
from 
    customers this_ 
where 
    this_.customers_username=? 

不知道會是什麼造成這一點。

+0

你能舉一個「輸入」的例子嗎? – 2011-05-05 07:28:19

回答

7

看起來你對用戶名的唯一約束。 Grails做了select來檢查唯一性,因爲假設讀取一行是一個輕量級操作,並且最好觸發一個唯一的約束違例和異常。

另一種方法是刪除域類中的唯一約束並在數據庫中手動添加唯一約束。

+0

是的,這是一個約束customers_point_date列不是null,所以我只是設置一個默認,如果它不存在和中提琴插入工作!謝謝 – JPM 2011-05-05 08:18:23

+0

糾正我,如果我錯了,但這裏的問題是,你會得到儘可能多的「檢查選擇」你模型中的許多'獨特'fileds。所以,如果你有兩個'unique'字段,它會生成兩個查詢,而不是一個'OR'。 – 2011-12-15 15:02:23

+0

沒錯,因爲約束是按順序運行的 - 沒有任何邏輯可以檢測到兩者可以合併爲一個。 – 2011-12-15 17:24:02

0

你有沒有嘗試過這樣的:

Customers cust = new Customers(input); 
println ("cust = "+cust); 
cust.save(); 
相關問題