title: Grails - beforeInsert throwing exception null id in entry
body: <p>I am trying to use beforeInsert  in my user domain class.</p>
  
  <pre><code> class User {
  
      String reEnterPassword
  
      static constraints = {
  
          password(blank: false, nullable: false, size:5..50, validator: {password, obj -&gt;
           def reEnterPassword = obj.properties['reEnterPassword']
           if(reEnterPassword == null) return true 
           reEnterPassword == password ? true : ['invalid.matchingpasswords']
           })
          reEnterPassword(bindable:true, blank: false);
      }
  
  
      def beforeInsert = {
          password = password.encodeAsSHA()
          }
  
      String toString(){
          name
          }
  
  
      static transients = ['reEnterPassword']
  
      }
  </code></pre>
  
  <p>in my controller i have save method ( generated)</p>
  
  <pre><code>    def save() {
      def userInstance = new User(params)
      if (!`userInstance.save(flush: true)`) {
          render(view: "create", model: [userInstance: userInstance])
          return
      }
  </code></pre>
  
  <p>This is throwing exception 
  Grails runtime exception, org.hibernate.AssertionFailure: null id in entry (don't flush the Session after an exception occurs), when domain objects save method encounters a SQL Exception </p>
  
  <p>I read in the documentation for <a href="http://grails.org/doc/latest/guide/GORM.html#eventsAutoTimestamping" rel="nofollow">auto timestamping</a> that
  Do not attempt to flush the session within an event (such as with obj.save(flush:true)). Since events are fired during flushing this will cause a StackOverflowError. </p>
  
  <p>In this case how to save my <code>userInstance.save(flush: true)</code> I tried to remove <code>flush:true</code> but still i am getting same error. if i remove flus:true..then when i need to call. When hibenate will flush all these records. </p>
  
  <p>I tried the solution defined <a href="http://jira.grails.org/browse/GRAILS-5865" rel="nofollow">this JIRA ticket</a>
   Please help me out. Thank you</p>