4
votes

I'm using JPA and parts of Spring (like transaction management, JPA repositories) but I don't use Spring for dependency injection, and instead treat the Spring parts as POJO objects. So far I have it working great: I have runtime generated JPA repository classes and transactions are managed by the Spring classes.

However, I can't seem to figure out how to get the JPA auditing listener to work.

For example, here is my BaseEntity that defines the EntityListener class and the audit fields:

@MappedSuperclass
@EntityListeners( { AuditingEntityListener.class } )
public class BaseEntity implements Serializable
{
  @CreatedDate
  @Field( index = Index.YES, store = Store.YES )
  @Column( name = "date_created" )
  @Temporal( TemporalType.TIMESTAMP )
  private Date dateCreated;

  @CreatedBy
  @Column( name = "created_by" )
  private Long createdBy;

  //other stuff
}

You can see that I specify the Spring AuditEntityListener class. All the other entity classes in the app extend this BaseEntity class.

Then, I have a class that implements AuditorAware:

public class JpaAuditConfiguration implements AuditorAware<Long>
{

  @Override
  public Long getCurrentAuditor()
  {
    //pretend there's real logic here...
    return new Long(0);
  }
}

Now, since I'm not using Spring or Spring Data to boot itself, I need a way to register this JpaAuditConfiguration with the AuditingEntityListener.

My question: How do I register the JpaAuditConfiguration with the AuditEntityListener programmatically?

If it helps, I'm using Spring classes like LocalContainerEntityManagerFactoryBean(to programmatically create the EntityManagerFactory) and PersistenceUnitPostProcessor to do the rest of the JPA configuration programmatically. I'm looking for the hook that allows me to do entity audit listener registration I mention above.

I'm not using any orm.xml or persistence.xml JPA configuration files.

How can I do that?

Thank you!!

1

1 Answers

1
votes
@Configuration
@EnableJpaAuditing
class Config {

  @Bean
  public AuditorAware<Long> auditorProvider() {
    return new JpaAuditConfiguration ();
  }
}

http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.auditing