为什么在Grails中唯一受限的字段更新失败 [英] Why is uniquely constrained field failing on update, in Grails

查看:150
本文介绍了为什么在Grails中唯一受限的字段更新失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在域类中映射一个自定义标识属性时,为什么hibernate会检查唯一约束?当我更新对象时,尽管事实上发布的字段值与存储在数据库中的值相同,但验证失败!即使我不更改表单(确保dirty:false并且没有属性绑定错误),也会发生这种情况。
我有一个像下面这样的Grails领域类:

When i have a custom identity attribute mapped in a domain class, why does hibernate check for unique constraint? When i update an object, the validation fails despite the fact that the posted field value is the same as that stored in DB! This occurs, even if I make no change to the form (ensuring dirty: false and no property binding errors). I have a Grails domain class like below:

class User {
  Long profileId
  String email
  String username
  String password
  String title
  String firstname
  String lastname
  String zipCode
  Date lastLoginDate

  static constraints = {
      profileId nullable: true, blank: true
      email blank: false, unique: true, email: true
      username blank: false, unique: true
      password blank: false
      lastLoginDate nullable: true

      firstname nullable: true
      lastname nullable: true
      zipCode nullable: true
  }

  static mapping = {
    table 'USER_PROFILE'
    id name:"profileId", column: "profile_id", generator: "sequence", params: [sequence:'userprofile_sequence']
    version false
  }

}

现在,当我创建具有最小属性集的记录的用户被建造。但是当我尝试更新相同的对象,如:
def user = User.findByUsername('akeel')
user.lastLoginDate = new Date()
user.save(flush:true)
没有任何反应,因为独特的验证检查失败。我可以通过执行
user.save(validate:false,flush:true)
来绕过验证但是,这不是一个选项,因为我需要验证zipCode,无论用户何时添加它。

Now, when i create a user with minimum attribute set, a record is created. But when i try to update the same object like: def user = User.findByUsername('akeel') user.lastLoginDate = new Date() user.save(flush: true) Nothing happens, because unique validation check fails. I can bypass the validation by doing user.save(validate: false, flush: true) But, that's not an option, as i need to validate the zipCode, whenever a user adds it.

我必须将自定义标识列profileId的约束条件设置为可为null,以按照建议的方式解决'映射中重复的列'问题here

I had to set the constraint for the custom identity column, profileId, as nullable true, to resolve the 'repeated column in mapping' problem as proposed as suggested here.

这个问题与所讨论的完全相同这里,但提出的解决方案对我无效。

This question is exactly like the one discussed here, but the solutions proposed didn't work for me.

我使用grails 2.1.2,让我知道是否需要其他任何东西来理解这个问题。

I am using grails 2.1.2, let me know if anything else is required to understand the problem.

推荐答案

看来,自定义属性名称因为主键在Grails中不起作用。我已经使用Grails 2.0.4,2.2.4和2.3.4进行了测试,它们都不能识别 profileId 作为主键。 User.get(1)工作正常,但 user.ident()返回 null 。我肯定会在Grails JIRA中提出一个问题。

It seems that a custom property name for the primary key doesn't work in Grails. I've tested with Grails 2.0.4, 2.2.4 and 2.3.4 and they all fail to recognise profileId as the primary key. User.get(1) works fine, but user.ident() returns null. I would certainly raise an issue in the Grails JIRA.

最简单的解决方案是删除 profileId 属性,改为使用内置的 id 。然后,从自定义映射中删除名称:值。换句话说,你应该最终得到

The simplest solution is to remove the profileId property and just use the builtin id instead. Then just remove the name: value from the custom mapping. In other words, you should end up with

id column: "profile_id", generator: "sequence", params: [sequence:'userprofile_sequence']

这篇关于为什么在Grails中唯一受限的字段更新失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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