Field marked as @Generated not being audited.

Description

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?

I guess that as the caseNumber is not "inserted" but autogenerated, Envers doesn't notice that this field has a value. If this is the case it is actually a bug because is an actual value that should be tracked. Is there any workaround?

Environment

Hibernate 5.1.0.Final, Wildfly 10, mariaDB (mysqldriver 1.3.7)

Activity

Show:
Imanol Bernabeu Aramendi
June 14, 2016, 7:32 AM

Yes. I forgot to mention the problem with the auto_increment fragment being passed to the envers table. I just got rid of it when I created my sql scripts. But this is something you should change because it will never be needed in the audit table.
I have been looking for information about how to "make sure the field is not flagged as insertable=false on the Envers side of the mapping" but I have no clue about it. Could you please tell me how can this be done or send a link with an example. I would appreciate that

Chris Cranford
June 14, 2016, 4:04 PM

, there isn't a workaround that I am aware when you use @Generated and @Column as you have without grabbing either the latest 5.1 or 5.2 (current master) branches and manually building them. Once 5.1.1 and 5.2.1 are released, they'll include the changed functionality.

Imanol Bernabeu Aramendi
June 15, 2016, 8:36 AM

Hi Chris. So how is it going to be possible in 5.1.1 or 5.2.1 to change the value of the param "insertable"??? What do you mean with changed functionality? Or are you just talking about "auto_increment" not being include in the field definition for the _aud table?
By now the only way is to implement it myself in the sources of hibernate-envers or change the JPA-configuration of the field, am I right? Sorry for asking so much but I want to be sure I understood it

Chris Cranford
June 15, 2016, 1:35 PM

, no worries.

I made a code change that will be included in the 5.1.1 and 5.2.1 releases that makes the field in the Envers audit table insertable. Without that code change, there isn't a way to make this work unfortunately. It was simply a bug when using @Generated on a column within an audited entity, nothing more.

The auto_increment portion of the columnDefinition simply causes the audit table column to be created as an auto_increment column too, which makes no sense from an Envers perspective since the audit table is simply meant to be a data capture table. What I would suggest is after the audit table has been created if you use the HBM2DDL feature of Hibernate, simply manually alter the table and change the column from auto_increment to integer since that is what your definition says in your case, so that it's correct. But understand this won't have any influence on the population of the value.

If you urgently need to fix for populating the field, you have 2 options ( short of waiting for 5.1.1 or 5.2.1 to be released ):

  1. Build the 5.1 or 5.2 (currently master) branches manually and use that rather than the published 5.1.0.Final / 5.2.0.Final builds.

  2. Checkout the 5.1 / 5.2 tag from Github and manually make the changes I did to AuditMetadataGenerator (github commit), build and use that instead of 5.1.0.Final / 5.2.0.Final.

Hope that helps.

Imanol Bernabeu Aramendi
June 15, 2016, 2:21 PM

Thank you Chris. You are a hero!! I guess I can wait until the new version. Thank you very much!

Assignee

Chris Cranford

Reporter

Imanol Bernabeu Aramendi

Fix versions

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

Priority

Major
Configure