3

I am trying to take a JSONObject I posted to my groovy controller. I can pass the object, see the JSON data and then create a Domain Object out of it. When I save it to write to the database it does a Select instead.

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

here is my debug sql output

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=?

Don't know what would be causing this.

JPM
  • 9,077
  • 13
  • 78
  • 137

2 Answers2

7

Looks like you have a unique constraint on username. Grails does a select to check uniqueness since assumed that reading one row is a lightweight action and it's preferable to triggering a unique constraint violation and exception.

An alternative is to remove the unique constraint in the domain class and add the unique constraint manually in the database.

Burt Beckwith
  • 75,342
  • 5
  • 143
  • 156
  • Yep it was a constraint on the customers_point_date column that was not null and so I just set a default if its not there and viola, inserts work! Thanks – JPM May 05 '11 at 08:18
  • Correct me if I'm wrong but the issue here is that you end up with as many "check selects" as many `unique` fileds you have in your model. So, if you have two `unique` fields, it'll generate two queries instead of one with `OR`. – Marcin Świerczyński Dec 15 '11 at 15:02
  • Right, because constraints are run in order - there's no logic to detect that two can be merged into one. – Burt Beckwith Dec 15 '11 at 17:24
  • Thanks! And what about concurrency issues? Does it block entire table or is it possible that other insert will show up between "check select" and our insert? – Marcin Świerczyński Dec 20 '11 at 17:06
  • There's no locking, so yes there is a small chance that an insert can happen in between. – Burt Beckwith Dec 20 '11 at 18:31
  • Burt, is there no other way around this than your alternative suggestion above? No cleaner way to do it via config? – Vahid Pazirandeh Sep 20 '14 at 00:26
0

Did you tried something like :

Customers cust = new Customers(input);
println ("cust = "+cust);
cust.save();
Nirmal
  • 4,789
  • 13
  • 72
  • 114