Pregunta

I've got a legacy schema which I'd like to use Spring Data JPA with, and I'm trying to work out if this scenario is possible.

I've code models like:

@Entity
class Entity1 {
   ...
   @CreatedBy
   private String createdBy;
   ...
}

and

@Entity
class Entity2 {
    ...
    @CreatedBy
    private Long createdBy;
    ...
}

I would like to be able to use Spring Data for this, however I can't find a way (so far) to have the auditing support this.

First thing I tried is having two AuditorAware beans, however this results in a multiple bean exception problem. Does anyone know if this is possible at present, and where I need to look for the solution?

Thanks in advance!

¿Fue útil?

Solución

Very interesting question, thank you for raising it.

Looking through the code, there is no way to plugin a second AuditorAware bean and have the system select between them by return type. Spring builds even its own internal beans based on this assumption.

You would have to (based on a preliminary research):

  • create an interface similar to AnnotationAuditingConfiguration interface that is aware that there are several beans of type AuditorAware
  • create an AuditHandler implementation that can distinguish between AuditorAware instances, based on return type
  • modify/override the AuditingBeanDefinitionRegistrarSupport to construct the type-aware AuditHandler mentioned earlier
  • provide an annotation that imports the overridden registrar

Quite a handful :))

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top