6

I really have a weird issue now.

I just want to delete an entity.

I am also using Hibernate envers for auditing. So now I want to delete this entity.

Now I get following message.

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'succeed' cannot be null

So when I removed @Audit from my table all of a sudden I was able to delete the entity.

Now I went to my entity_aud table and deselected NOT NULL for attribute succeed. Then I also again put @Audit above my table.

Now it worked. So why if I want to just delete an entity, I get a NOT NULL error when using Hibernate Envers.

What is the reason for this.

Bill P
  • 3,622
  • 10
  • 20
  • 32
Flo19
  • 81
  • 1
  • 5

1 Answers1

15

When an entity is removed, Envers will also generate an audit entry for that operation. By default, entity data is not captured when a delete audit record is produced, so essentially Envers attempts to insert a row into the audit table that contains the Primary Key, Revision Number, and Revision Type. All the other columns will be inserted with null values.

Since your audit table had the succeed column specified as NOT NULL, the delete throw an exception.

Besides the primary key, revision, and revision type columns in the audit table, all other columns should be created without the NOT NULL specification for this reason, meaning they are allowed to be NULL. If you can reproduce Envers generating tables that do not adhere to this, please report it as a bug by attaching the entity model to the issue.

A configuration setting, org.hibernate.envers.store_data_at_delete when set to true will tell Envers to not only capture the entity's primary key, revision, and revision type, but all audited columns. This is not enabled by default because in general, the prior revision maintains that same state so replicating it is really unnecessary; however, some users prefer to have it.

Steven
  • 441
  • 4
  • 16
Naros
  • 19,928
  • 3
  • 41
  • 71
  • You wrote "Besides the primary key, revision, and revision type columns in the audit table, all other columns should be created with the NOT NULL specification for this reason." Do you really mean "WITH the NOT NULL specification"? I think it should be "without the NOT NULL specification" or just "nullable". -> submitted an edit request for this. – Steven May 29 '20 at 06:48