hibernatehibernate-envers

Envers and Hibernate 6.3.1 does not audit fields with annotation @Column(insertable = false, updatable = false)


We have an entity definition with following property:

@Generated(GenerationTime.INSERT)
@Column(insertable = false, updatable = false)
private Integer number;

The entity is annotated with @Audited.

Recently we upgraded from Spring Boot 3.2 (which comes with Hibernate 6.1.3) and the audit table no longer contains the value for this property but instead just NULL.

I tracked this down to Envers not auditing properties that are marked as insertable = false. Once this is set to true it starts working.

Is this an intended behavior or a bug? The insertable attribute is meant for the main table and is set to avoid coding mistakes - the number field should be only set via a sequence. But for the audit table it should be present.

This used to work with Spring Boot 2.7 and Hibernate 5.6. Stopped working in Spring Boot 3.2 and Hibernate 6.3.1.

Edit - I can confirm that it stopped working with Hibernate 6 and Spring boot 3.1 - it's not something that came with 3.2.


Solution

  • I had the same problem and solved it by adding the @AuditMappedBy annotation on the reverse of the relationship

    (from https://docs.jboss.org/envers/docs/) One special case are relations mapped with @OneToMany+@JoinColumn on the one side, and @ManyToOne+@JoinColumn(insertable=false, updatable=false) on the many side. Such relations are in fact bidirectional, but the owning side is the collection (see alse here). To properly audit such relations with Envers, you can use the @AuditMappedBy annotation. It enables you to specify the reverse property (using the mappedBy element). In case of indexed collections, the index column must also be mapped in the referenced entity (using @Column(insertable=false, updatable=false), and specified using positionMappedBy. This annotation will affect only the way Envers works. Please note that the annotation is experimental and may change in the future.