title: Grails domain beforeInsert / beforeUpdate
body: <p>I want to save my domain class to the database without specifying the <code>createdUser</code> or <code>createdDate</code>. I’ve created an object called <code>AuditingInfo</code> and embedded it in the main <code>Person</code> domain class like this:</p>
  
  <p><code>AuditingInfo.groovy</code>:</p>
  
  <pre><code>class AuditingInfo {
      static constraints = {
          createdUser (nullable : true)
          updatedUser (nullable : true)
          createdDate(nullable : true)
          updatedDate(nullable : true)
      }
  
      static mapping = {
          columns {
              createdUsercolumn: 'T_CREATED_USER'
              updatedUsercolumn: 'T_UPDATED_USER'
              createdDatecolumn: 'T_CREATED_DATE'
              updatedDatecolumn: 'T_UPDATED_USER'
          }
      }
  
      User createdUser
      User updatedUser
      Date createdDate
      Date updatedDate
  }
  </code></pre>
  
  <p><code>Person.groovy</code>:</p>
  
  <pre><code>class Person {
      static embedded = ['auditingInfo']
      AuditingInfo auditingInfo
  
      static constraints = { auditingInfo(nullable: true) }
      String name
      Long id
  }
  </code></pre>
  
  <p>I cannot use the <code>beforeInsert</code> and <code>beforeUpdate</code> events in the <code>Person</code> domain or <code>AuditingInfo</code> class, because it always causes a <code>NullPointerException</code> in <code>org.grails.datastore.mapping.engine.event.AbstractPersistenceEventListener</code>.
  Therefore, I want to use the <code>metaClass</code> way, as used below (this action is defined in a <code>*GrailsPlugin.groovy</code> file, but unfortunately my project is a Grails project, not a Grails plugin project):</p>
  
  <pre><code>def doWithDynamicMethods = { ctx -&gt;       
      application.domainClasses.each { org.codehaus.groovy.grails.commons.GrailsDomainClass gc -&gt;
          gc.metaClass.beforeInsert = {
          }
  
          gc.metaClass.beforeUpdate = {
          }
      }
  }
  </code></pre>
  
  <p>How can I apply this method to my project context? Thank you so much.</p>