【问题标题】:Grails save() Domain Object actually does a Select?Grails save() 域对象实际上做了一个选择吗?
【发布时间】:2011-05-05 05:59:46
【问题描述】:

我正在尝试将我发布到我的 groovy 控制器的 JSONObject。我可以传递对象,查看 JSON 数据,然后从中创建一个域对象。当我将其保存以写入数据库时​​,它会改为执行 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=?

不知道是什么原因造成的。

【问题讨论】:

  • 你能举个“输入”的例子吗?

标签: android grails groovy save


【解决方案1】:

看起来您对用户名有唯一的限制。 Grails 会选择检查唯一性,因为假设读取一行是轻量级操作,并且最好触发唯一性约束违规和异常。

另一种方法是删除域类中的唯一约束并在数据库中手动添加唯一约束。

【讨论】:

  • 是的,customers_point_date 列的约束不为空,所以我只是设置了一个默认值,如果它不存在并且中提琴,插入工作!谢谢
  • 如果我错了,请纠正我,但这里的问题是您最终会得到与模型中的 unique 文件一样多的“检查选择”。因此,如果您有两个 unique 字段,它将生成两个查询,而不是一个带有 OR 的查询。
  • 对,因为约束是按顺序运行的——没有逻辑可以检测到两个可以合并为一个。
  • 谢谢!那么并发问题呢?它会阻塞整个表还是有可能在“检查选择”和我们的插入之间出现其他插入?
  • 没有锁定,所以是的,插入发生在两者之间的可能性很小。
【解决方案2】:

您是否尝试过类似的方法:

Customers cust = new Customers(input);
println ("cust = "+cust);
cust.save();

【讨论】:

    猜你喜欢
    • 2011-06-01
    • 2010-12-29
    • 2011-09-12
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    • 1970-01-01
    • 2014-05-19
    • 2013-05-03
    相关资源
    最近更新 更多