Grails的保存()的域对象实际上做一个选择? [英] Grails save() Domain Object actually does a Select?

查看:197
本文介绍了Grails的保存()的域对象实际上做一个选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想采取的JSONObject我贴到我的常规控制器。我可以传递对象,看到JSON数据,然后创建一个域对象出来。当我保存它写入数据库,它做了选择,而不是。

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()
}   

这是我调试SQL输出

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.

推荐答案

看起来你对用户名的唯一约束。 Grails的做了选择要检查的独特性,因为假定读一行是一个轻量级的行动,这是preferable来触发唯一约束违规和异常。

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.

这篇关于Grails的保存()的域对象实际上做一个选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆