Search code examples
mysqlhibernatejpahibernate-envers

Envers: auto_increment (not PK) column not being audited


I am implementing a JPA-EJB solution, which uses Hibernate-Envers to audit changes. Using MariaDB 5.5.49.

I have a table, which has an auto_increment field (this is not the PK, it is just another field), which I defined this way:

@NotNull
@Generated(GenerationTime.INSERT)
@Column(columnDefinition = "integer auto_increment")
private int caseNumber;

I also created an index and a unique constraint for this field as mysql requires.

Somehow Envers is not recognizing this field and doesn't store the value of caseNumber in the audition table. The insert sentence that is being logged saves all fields of my entity but this one.

Does anyone know why this is happening? Is something wrong in the definition of the field? Is Envers not getting along with auto_increment?


Solution

  • The problem here is the combination of both @Generated and @Column.

    The default behavior of @Column is that the associated field/property is to be mapped as insertable=true, updatable=true. When you combine @Column with @Generated, this changes the column annotation's behavior to be insertable=false, updatable=false since the intent is that the value will be maintained by the database. This influenced the mapping Envers generated, thus the value for your caseNumber field simply was deemed not insertable nor updatable.

    As mentioned in JIRA HHH-10841, the fix was to simply detect this use case and make sure that the Envers mappings maintained a insertable=true state so that changes would be tracked and pushed to the column in the audit tables.