title: Passing getCurrentAuditor Object response to a transient JPA field
body: <p>I am new to Spring Data's Auditor and facing with a peculiar scenario while trying to override the <code>getCurrentAuditor()</code>.</p>
  
  <p>The auditor method returns an object of type <code>AuditData</code> (unlike String in most cases) and needs to be consumed by a transient field annotated with @CreatedBy in <code>AuditPO</code> class.</p>
  
  <pre><code>@CompileStatic
  @ToString
  @JsonIgnoreProperties(ignoreUnknown = true)
  @JsonPropertyOrder(alphabetic = true)
  @EqualsAndHashCode
  class AuditData {
      UUID userId
      UUID profileId
  }
  </code></pre>
  
  <p><code>AuditPO</code> class contains various columns related to persisting audit info. This class is extended by each entity, so that the corresponding entity table has the same set of columns with appropriate audit info.</p>
  
  <p>A sample is provided below with Role &amp; Permission tables. <code>RolePO</code>/<code>PermissionPO</code> extends <code>UuidPO</code> which eventually extends this <code>AuditPO</code>:</p>
  
  <pre><code>@Entity
  @Table(name = 'Role')
  @ToString(includeNames = true, includeSuper = true) 
  class RolePO extends UuidPO {
  
      @NotNull(message = '{role.name.required}')
      @Size(max = 30, message = '{role.name.size}')
      String name
  
      @Size(max = 100, message = '{role.description.size}')
      String description
  }
  </code></pre>
  
  <pre><code>@Entity
  @Table(name = 'Permission')
  @ToString(includeNames = true, includeSuper = true)
  class PermissionPO extends UuidPO {
  
      @NotNull(message = '{permission.name.required}')
      @Size(max = 36, message = '{permission.name.size}')
      String name
  
      @Size(max = 100, message = '{permission.description.size}')
      String description
  }
  </code></pre>
  
  <pre><code>@MappedSuperclass
  @ToString(includeNames = true)
  abstract class UuidPO extends AuditPO {
      @Id
      @GeneratedValue(generator = 'uuid2')
      @GenericGenerator(name = 'uuid2', strategy = 'uuid2')
      @Column(columnDefinition = 'VARCHAR(36)')
      @Type(type = 'uuid-char')
      UUID id
  }
  </code></pre>
  
  <pre><code>@MappedSuperclass
  @EntityListeners(AuditingEntityListener)
  @ToString(includeNames = true)
  abstract class AuditPO {
  
      @CreatedBy
      AuditData create
  
      @LastModifiedBy
      AuditData update
  
      @Type(type = 'uuid-char')
      UUID createdByProfileId
  
      @Type(type = 'uuid-char')
      UUID updatedByProfileId
  
      @Type(type = 'uuid-char')
      UUID createdByUserId
  
      @Type(type = 'uuid-char')
      UUID updatedByUserId
  
      @CreatedDate
      @Column(columnDefinition = "DATETIME(3)")
      Date createDate
  
      @LastModifiedDate
      @Column(columnDefinition = "DATETIME(3)")
      Date updateDate
  }
  
  </code></pre>
  
  <p>The end goal is to map the <code>auditData.userId</code> to <code>auditPO.(created/updated)ByUserId</code>  and <code>auditData.profileId</code> to <code>auditPO.(created/updated)ByProfileId</code>.</p>
  
  <p>One approach that I was exploring was to create something like below: </p>
  
  <pre><code>@Transient 
  @CreatedBy
  AuditData create
  </code></pre>
  
  <p>and then use the <code>@PrePersist</code> option of JPA to act on the values obtained, so that I could map to the respective fields as I need. However, I wasn't very successful there. The other option I explored was around creating a different EntityListener class and using that in the AuditPO, but wasn't very fruitful either (Maybe am missing something in the latter approach).</p>
  
  <p>Also, a lot of samples dealing with handling object type return from <code>getCurrentAuditor()</code> had the AuditPO class with an instance variable for @CreatedBy/@UpdatedBy which would then have JOIN with another table. Our model doesn't go with that approach. Each entity table needs those two columns.</p>
  
  <p>I would really appreciate any help in achieving the goal listed above. </p>
  
  <p>P.S.: Data storage - MySQL 5.7.23, ORM - Hibernate, Spring - 4.2.x</p>