# Hibernate envers for runtime polymorphism

**URL:** <https://discourse.hibernate.org/t/hibernate-envers-for-runtime-polymorphism/9289>\
**Category:** Hibernate ORM\
**Created:** [March 29, 2024, 10:45am UTC](https://discourse.hibernate.org/t/hibernate-envers-for-runtime-polymorphism/9289 "2024-03-29T10:45:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![smotifal](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/smotifal/32/1802_2.png) [@smotifal](https://discourse.hibernate.org/u/smotifal)\
**Post date:** [March 29, 2024, 10:45am UTC](https://discourse.hibernate.org/t/hibernate-envers-for-runtime-polymorphism/9289/1 "2024-03-29T10:45:31Z")

</div>

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

```auto
@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; }

}

```

---

<div class="post-metadata">

**Author:** ![mbladel](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/mbladel/32/2128_2.png) [@mbladel](https://discourse.hibernate.org/u/mbladel)\
**Post date:** [March 29, 2024, 11:18am UTC](https://discourse.hibernate.org/t/hibernate-envers-for-runtime-polymorphism/9289/2 "2024-03-29T11:18:28Z")

</div>

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](https://docs.jboss.org/hibernate/orm/current/userguide/html_single/Hibernate_User_Guide.html#envers) for more detailed information.
