Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Spring Data audit annotations with na @Embeddable class

I want to use Spring Data automated entity auditing (Spring provides: @EnableJpaAuditing, AuditingEntityListener, @CreatedBy, @CreatedDate, @LastModifiedBy, @LastModifiedDate) and have the fields annotated by these annotations be a part of an @Embeddable class that I can use as a field in entities that I want to have audited. Here's an example - I'm only showing the relevant code, it compiles and runs:

Entity:

@EntityListeners(AuditingEntityListener.class)
public class EntityA {

    {...}

    @Embedded
    private AuditFields audit;
}

Embeddable class with audit fields:

@Embeddable
public class AuditFields {

    {...}     

    @Column(...)
    @CreatedDate
    private LocalDateTime createdDate;
}

I have @EnableJpaAuditing on a configuration class, auditing is on, I have verified that the AuditingEntityListener is being called for all classes annotated with @EntityListeners(AuditingEntityListener.class).

I do not want to use a @MappedSuperclass for the audit fields and then extend it in every entity, because for me this completely violates the idea of what class inheritance represents.

I do not want to create my own interface that the entities implement, which would expose the audit fields to a some custom entity listener (if that were the case, I could even use the Spring's Auditable interface and achieve the same result, since AuditingEntityListener would then easily pick this up). Using this solution makes the four field annotations pointless because I manually expose the fields for Spring to use.

Looking at how it's implemented in Spring, the AuditingHandler scans each entity that's picked up by the AuditingEntityListener for either it implementing Auditable or, through some fancy wrapping etc., for fields annotated by the four audit fields' annotations. It's not looking at embedded objects and their fields.

Annotating the @Embeddable class itself with the @EntityListeners annotation does nothing - predictably, it's not registered as an entity, so why would any entity listener be called for it.

Is my idea of using the Spring's audit functionality even possible? Maybe I'm missing some way of configuring Spring to also take into account embedded objects in an entity when scanning it for the audit annotations?

Or am I stuck with just using my own interface and my own entity listener? Spring's Auditable interface exposes a lot of methods I don't need for my simple use case where I can just get and set the embedded object. I'd fall back on creating my own interface and listener.

like image 840
user2080472 Avatar asked Oct 26 '25 00:10

user2080472


1 Answers

Well I just faced with the same problem using the latest at the moment spring-data-jpa version 2.7.2. And the problem was solved just by initializing audit field at the declaration point. So the working solution would look like this:

@Embedded
private AuditFields audit = new AuditFields();

Everything else described in the initial post seems ok and is working for me.

like image 100
Pavlo Kolesnykov Avatar answered Oct 27 '25 15:10

Pavlo Kolesnykov