title: Grails null id error on constraints during integration test
body: <p>Grails 2.2.0</p>
  
  <p>I am trying to create a custom constraint to force user to have only one master email. Here is the simplified code causing the error:</p>
  
  <p>User domain class</p>
  
  <pre><code>class User {
  
      static hasMany = [emails: Email]
  
      static constraints = {
      }
  }
  </code></pre>
  
  <p>Email domain class</p>
  
  <pre><code>class Email {
  
      static belongsTo = [user: User]
      String emailAddress
      Boolean isMaster
  
      static constraints = {
  
          emailAddress unique: ['user']
          isMaster validator: { val, obj -&gt;
              return !val || Email.findByUserAndIsMaster(obj.user, true) == null
          }
  
      }
  }
  </code></pre>
  
  <p>Integration test</p>
  
  <pre><code>class EmailTests {
  
      @Before
      void setUp() {
  
      }
  
      @After
      void tearDown() {
          // Tear down logic here
      }
  
      @Test
      void testSomething() {
          def john = (new User(login: 'johnDoe')).save(failOnError: true, flush: true)
          assert (new Email(emailAddress: 'john@gmail.com', user: john, isMaster: true)).save(failOnError: true)
      }
  }
  </code></pre>
  
  <p>Running "grails test-app -integration" will cause:</p>
  
  <blockquote>
    <p>| Failure:  testSomething(webapp.EmailTests)<br>
    |  org.hibernate.AssertionFailure: null id in webapp.Email entry (don't flush the Session after an exception occurs)
        at org.grails.datastore.gorm.GormStaticApi$_methodMissing_closure2.doCall(GormStaticApi.groovy:105)
        at webapp.Email$__clinit__closure1_closure2.doCall(Email.groovy:13)
        at org.grails.datastore.mapping.engine.event.AbstractPersistenceEventListener.onApplicationEvent(AbstractPersistenceEventListener.java:46)
        at webapp.EmailTests.testSomething(EmailTests.groovy:21)</p>
  </blockquote>
  
  <p>If I change the unique constraint to be after the custom constraint the problem will not happen. What is happening here? I want to understand how is the order of the constraints of any relevance here?</p>
  
  <p>To be clear this does NOT cause the problem:</p>
  
  <pre><code>static constraints = {
          isMaster validator: { val, obj -&gt;
              return !val || Email.findByUserAndIsMaster(obj.user, true) == null
          }
          emailAddress unique: ['user']
      }
  </code></pre>