Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate Envers cannot delete entity

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.

like image 777
Flo19 Avatar asked Dec 22 '22 21:12

Flo19


1 Answers

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.

like image 54
Naros Avatar answered May 13 '23 18:05

Naros