Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to have Spring Data Auditing populate Long or String depending on field?

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!

like image 791
Innominate Avatar asked Nov 23 '25 08:11

Innominate


1 Answers

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 :))

like image 96
Gabriel Ruiu Avatar answered Nov 24 '25 21:11

Gabriel Ruiu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!