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

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder(toBuilder = true)
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class BaseAuditEntity {

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

	@CreatedBy
	@Audited
	@Column(updatable = false)
	private String createdBy;

	@LastModifiedDate
	@Audited
	private OffsetDateTime updatedAt;

	@Audited
	@LastModifiedBy
	private String updatedBy;

  }

@Entity
public class EntityToAudited extends BaseAuditEntity { 

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

   //other fields
}

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

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

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

  //other fields
}

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

  @Override
  // 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.