title: Why is uniquely constrained field failing on update, in Grails
body: <p>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:</p>
  
  <pre><code>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
    }
  
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>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 <a href="https://stackoverflow.com/questions/4457727/how-to-solve-grails-error-repeated-column-in-mapping-for-entity-on-existing-pos#comment5139677_4654921">here</a>.</p>
  
  <p>This question is exactly like the one discussed <a href="http://grails.1312388.n4.nabble.com/Why-is-uniquely-constrained-field-failing-on-update-td3731538.html" rel="nofollow noreferrer">here</a>, but the solutions proposed didn't work for me.</p>
  
  <p>I am using grails 2.1.2, let me know if anything else is required to understand the problem.</p>