Hibernate envers for runtime polymorphism

Hi All,

We are trying to implement hibernate-envers with our current data model and I don’t understand how to address our issue based on the documentation. The relation is a @ManyToOne between ConcreteClassA and EntityToAudited inherited from the abstract super class. We want to audit the relation and also the fullName field in the EntityToAudited. In the documentation, I did read that @AuditJoinTable seems to be the answer. Just for context, to address this issue with hibernate-seach, we used binder with solution provided from Yohan Rodierre (Maintener for Hibernate Search)

Context :

Envers version : 5.5.3-FINAL
Spring boot version : 2.6.7

@SuperBuilder(toBuilder = true)
public abstract class BaseAuditEntity {

	@Column(updatable = false)
	@GenericField(sortable = Sortable.YES)
	private OffsetDateTime createdAt;

	@Column(updatable = false)
	private String createdBy;

	private OffsetDateTime updatedAt;

	private String updatedBy;


public class EntityToAudited extends BaseAuditEntity { 

   @JoinColumn(name = "abstract_id")
   @Audited ???
   @AuditJoinTable ???
   private AbstractEntity abstractEntity;

   //other fields

// No field to audit here
@DiscriminatorColumn(name = "productType")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class AbstractEntity extends  BaseAuditEntity {

  @OneToMany(mappedBy = "abstractEntity")
   private Set<EntityToAudited> entityToAuditeds;

  // Hack for hibernate search runtime polymorphism
  public abstract AbstractEntity getSelf();

  //other fields

public class ConcreteClassA extends AbstractEntity {
  //Field to audit
  private String fullName;

  // Hack for hibernate search runtime polymorphism
  public ConcreteClassA getSelf() { return this; }


Since you’re using a @ManyToOne association on the owning side, you simply have to annotate @Audited on the owning entity and the association should be included in envers’ auditing information.

You can also override audit information specified in a @MappedSuperclass by using the @AuditOverride annotation. Please refer to the Envers user guide chapter for more detailed information.